import abc
#定义一个说话的方式接口类,只能被继承 不能被实例化
class Speak(abc.ABC):
#@abc.abstractmethod
@abc.abstractmethod
def to_speak(self):
pass
#定义学生说话的类
class Student(Speak):
def to_speak(self):
print("老师好")
#定义老师说话的类
class Teacher(Speak):
def to_speak(self):
print("同学们好")
#类的实现
s = Student()
t = Teacher()
#函数实现
s.to_speak()
t.to_speak()
sss=Speak()
try:
pass
except TypeError:
print("hello")
#定义一个求面积的接口类
class get_area():
@abc.abstractmethod
def get_reslut(self, a, b):
pass
#定义求圆的面积的类
class Yuan(get_area):
def get_reslut(self, a, b):
print("圆的面积公式为:%d x %d x 3.14 = %.2f" % (a, b, a * b * 3.14))
return float(a * b *3.14)
#定义一个求正方形面积的类
class square(get_area):
def get_reslut(self, a, b):
print("正方形的面积公式为:%d x %d = %d" % (a, b, a * b *3.14))
return a * b
#初始化变量
m_yuan = Yuan()
m_square =square()
#函数实现
m_yuan.get_reslut(2,3)
m_square.get_reslut(4,4)