class Person:
def set_name(self,name):
self.name=name
def get_name(self):
return self.name
def greet(self):
print("Hello,world!I am {}.".format(self.name))
class MemberCounter:
members=0
def init(self):
MemberCounter.members +=1
class Filter:
def init(self):
self.blocked=[]
# 过滤器
def filter(self,sequence):
# 迭代+判断
return [x for x in sequence if x not in self.blocked]
class SPAMFilter(Filter): #SPAMFilter是Filter的子类
def init(self):#重写init方法
self.blocked=['SPAM']
前者是不是后者的实例
返回对象的类两种方法
class Calculator:
def calculator(self,expression):
self.value=eval(expression)
class Talker:
def talk(self):
print('Hi , my value is',self.value)
class TalkingCalculator(Calculator,Talker):
pass
from abc import ABC,abstractmethod
class Talker(ABC):
@abstractmethod
def talk(self):
pass
class Knigget(Talker):
def talk(self):
print("Ni!")
虽然没有继承关系,但是方法一致,就可以通过register注册
没有继承的空类并不能直接使用其他类的方法
from abc import ABC,abstractmethod
class Talker(ABC):
@abstractmethod
def talk(self):
pass
class Knigget(Talker):
def talk(self):
print("Ni!")
class Herring:
def talk(self):
print("Blub.")
class Clam:
pass