■ __sub__ 함수를 사용해 – 연산자 오버로딩을 하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class StringHelper: def __init__(self, source = None): self.__source = source def __sub__(self, removeCharacterList): for removeCharacter in removeCharacterList: self.__source = self.__source.replace(removeCharacter, "") return StringHelper(self.__source) def Remove(self, removeCharacterList): return self.__sub__(removeCharacterList) def GetString(self): return self.__source helper = StringHelper("ABCDEFabcdefg") print(helper.GetString()) helper - "ACE" print(helper.GetString()) """ ABCDEFabcdefg BDFabcdefg """ |