|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> class Animal:
... def __init__(self, name, age):
... self.name = name
... self.age = age
...
>>> class Animal:
... def __init__(self, name, age):
... self.name = name
... self.age = age
...
>>> class Animal:
... def __init__(self, name, age):
... self.name = name
... self.age = age
... def say(self):
... print(f"我叫{self.name}, 今年{self.age}岁。")
...
>>> class Pig(Animal):
... def special(self):
... print("我的技能是拱大白菜~~")
...
>>> P = Pig("大肠", 5)
>>> P.say()
我叫大肠, 今年5岁。
>>> P.special()
我的技能是拱大白菜~~
>>> class FlyMixin:
... def fly(self):
... print("哦豁,我还会飞")
...
>>> class Pig(FlyMixin, Animal):
... def special(self):
... print("我的技能是拱大白菜~~")
...
>>> p = Pig("大肠", 5)
>>> p.say()
我叫大肠, 今年5岁。
>>> p.fly()
哦豁,我还会飞
>>> p.special()
我的技能是拱大白菜~~
>>> class Displayer:
... def display(self, message):
... print(message)
...
>>> class LoggerMixin:
... def log(self, message, filename="logfile.txt"):
... with open(filename, "a") as f:
... f.write(message)
... def display(self, message):
... super().display(message)
... self.log(message)
...
>>> class MySubClass(LoggerMixin, Displayer):
... def log(self, message):
... super().log(message, filename="subclasslog.txt")
...
>>> subclass = MySubClass()
>>> subclass.display("This is a test.")
This is a test. |
|