diff options
Diffstat (limited to 'oop')
| -rw-r--r-- | oop/MethodTest.py | 42 | ||||
| -rw-r--r-- | oop/TestProperrty.py | 22 | ||||
| -rw-r--r-- | oop/__init__.py | 0 | ||||
| -rw-r--r-- | oop/dp/Decorator.py | 59 | ||||
| -rw-r--r-- | oop/dp/__init__.py | 0 |
5 files changed, 123 insertions, 0 deletions
diff --git a/oop/MethodTest.py b/oop/MethodTest.py new file mode 100644 index 0000000..67fdf57 --- /dev/null +++ b/oop/MethodTest.py @@ -0,0 +1,42 @@ + +from datetime import datetime + +class Test: + alias = 'wudaokou nash' + + @staticmethod + def static_mode(language): + print(f'static method, {Test.alias} codes in {language}') + + @classmethod + def class_mode(cls, language): + print(f'class method, {cls.alias} codes in {language}') + + +class Repr: + + def __init__(self, name): + self.name = name + + def __repr__(self): + return f'{self.__class__.__module__}.{self.__class__.__qualname__}(name={self.name})' + + def __str__(self): + return f'{self.name}' + + +if __name__ == '__main__': + t = Test() + t.static_mode('chinese') + t.class_mode('english') + + r = Repr('zhang') + print(r) + print(repr(r)) + print(str(r)) + + now = datetime.now() + print(now.__str__()) + print(now.__repr__()) + + diff --git a/oop/TestProperrty.py b/oop/TestProperrty.py new file mode 100644 index 0000000..de43e17 --- /dev/null +++ b/oop/TestProperrty.py @@ -0,0 +1,22 @@ + +class Screen(object): + @property + def width(self): + return self._width + + @width.setter + def width(self, value): + self._width = value + + @property + def height(self): + return self._height + + @height.setter + def height(self, value): + self._height = value + + @property + def resolution(self): + return self._height*self._width + diff --git a/oop/__init__.py b/oop/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/oop/__init__.py diff --git a/oop/dp/Decorator.py b/oop/dp/Decorator.py new file mode 100644 index 0000000..390935a --- /dev/null +++ b/oop/dp/Decorator.py @@ -0,0 +1,59 @@ + +from abc import ABCMeta, abstractmethod + + +class Person(metaclass=ABCMeta): + def __init__(self, name): + self._name = name + + @abstractmethod + def wear(self): + print('着装: ') + + +class Engineer(Person): + + def __init__(self, name, skill): + super().__init__(name) + self._skill = skill + + def wear(self): + print(f'我是 {self._name} 工程师, 我会 {self._skill}') + super().wear() + + +class ClothingDecorator(Person): + def __init__(self, person: Person): + self._decoratored = person + + def wear(self): + self._decoratored.wear() + self.decorate() + + @abstractmethod + def decorate(self): + pass + + +class CasualPantDecorator(ClothingDecorator): + def __init__(self, person: Person): + super().__init__(person) + def decorate(self): + print('一条卡其色裤子') + + +class BeltDecorator(ClothingDecorator): + def __init__(self, person: Person): + super().__init__(person) + + def decorate(self): + print('一条黑色腰带') + +if __name__ == '__main__': + tony = Engineer('Tony', '算法') + pant = CasualPantDecorator(tony) + belt = BeltDecorator(pant) + belt.wear() + + + diff --git a/oop/dp/__init__.py b/oop/dp/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/oop/dp/__init__.py |
