#定义一个中国人的类
class Chinese:
#共同的特征
country='China'
#共同的技能
def talk(self):
print('is talking Chinese')
def eat(self):
print('is eating Chinese food')
#实例化的方式产生一个对象 p1=Chinese() p2=Chinese()
#定义一个中国人的类
class Chinese:
#共同的特征
country='China'
#初始化
def __init__(self,name,age):
self.name=name #每个对象都有自己的名字
self.age=age #每个对象都有自己的年龄
#共同的技能
def talk(self):
print('is talking Chinese')
def eat(self):
print('is eating Chinese food')
#实例化的方式产生一个对象
p1=Chinese('zhang',18)
Chinese.talk(Chinese)
p1=Chinese('zhang',18)
print(Chinese.__dict__)
#{'__module__': '__main__', 'country': 'China', '__init__': <function Chinese.__
# init__ at 0x000002187F35D158>, 'talk': <function Chinese.talk at 0x000002187F35D1E0>,
# 'eat': <function Chinese.eat at 0x000002187F35D268>, '__
# dict__': <attribute '__dict__' of 'Chinese' objects>,
# '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}
print(p1.__dict__)
#{'name': 'zhang', 'age': 18}
p1=Chinese('zhang',18)
p2=Chinese('li',19)
print(Chinese.talk)#<function Chinese.talk at 0x000001B8A5B7D1E0>
print(p1.talk) #<bound method Chinese.talk of <__main__.Chinese object at 0x000001B8A5B7BD68>>
print(p2.talk) #<bound method Chinese.talk of <__main__.Chinese object at 0x000001B8A5B7BDA0>>
class Chinese:
country='China'
def __init__(self,name,age):
self.name=name
self.age=age
def talk(self):
print('%s is talking Chinese'%self.name)
def eat(self):
print('is eating Chinese food')
p1=Chinese('zhang',18)
p2=Chinese('li',19)
Chinese.talk(p1) #zhang is talking Chinese
p1.talk() #zhang is talking Chinese
class People: pass class Student(People):#People称为基类或者父类 pass
class People: pass class Animals: pass class Student(People,Animals): pass print(Student.__bases__)#(<class '__main__.People'>, <class '__main__.Animals'>) print(People.__bases__)#(<class 'object'>,)
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def walk(self):
print('%s is walkig'%self.name)
class Teacher(People):
def __init__(self,name,age,level):
People.__init__(self,name,age)
self.level=level
t1=Teacher('zhang',18,10)
print(t1.level) #10
print(t1.name) #zhang 子类可以用父类定义的属性
t1.walk() #zhang is walking 子类无需定义就可以用父类的方法
print(issubclass(Teacher,People)) #True查看Teacher类是不是People类的子类
class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon=mon
self.day=day
def tell_birth(self):
print('出生于%s年%s月%s日'%(self.year,self.mon,self.day))
class Teacher:
def __init__(self,name,age,year,mon,day):
self.name=name
self.age=age
self.birth=Date(year,mon,day)
t=Teacher('egon',19,2010,10,10)
print(t.birth) #<__main__.Date object at 0x0000017E559380F0>
t.birth.tell_birth() #出生于2010年10月10日
class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon=mon
self.day=day
def tell_birth(self):
print('出生于%s年%s月%s日'%(self.year,self.mon,self.day))
class Teacher:
def __init__(self,name,age,*args):
self.name=name
self.age=age
self.birth=Date(*args)
t=Teacher('egon',19,2010,10,10)
print(t.birth) #<__main__.Date object at 0x0000017E559380F0>
t.birth.tell_birth() #出生于2010年10月10日
class Interface:
def read(self):
pass
def write(self):
pass
class Txt(Interface):
def read(self):
print('文本文档的读取方式')
def write(self):
print('文本文档的写入方式')
class Sata(Interface):
def read(self):
print('硬盘文件的读取方式')
def write(self):
print('硬盘文件的写入方式')
class process(Interface):
def read(self):
print('进程数据的读取方式')
def write(self):
print('进程数据的写入方式')
import abc
class File(metaclass=abc.ABCMeta):
@abc.abstractmethod
def read(self):
pass
@abc.abstractmethod
def write(self):
pass
#父类使用了抽象类,那子类就必须继承父类的方法,而且名字也必须一样
#这样就实现了代码级别的限制
class Txt(File):
def read(self):
print('文本文档的读取方式')
def write(self):
print('文本文档的写入方式')
class Parent(object): def __init__(self,name,age): self.name=name self.age=age class Child(Parent): def __init__(self,name,age,salary): Parent.__init__(self,name,age,salary) self.salary=salary
class Parent(object): def __init__(self,name,age): self.name=name self.age=age class Child(Parent): def __init__(self,name,age,salary): super().__init__(name,age) self.salary=salary
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有