类和对象
一.封装
(1)成员变量
成员方法
class student:
#成员变量
name=None
age=None
tel=None
#成员方法
def say_hi(self,msg):#self默认一定会有!
print(f"{self.name},{msg}")
stu1=student()
stu1.age=18
stu1.name="wmy"
stu1.tel="136"
print(stu1.name)
print(stu1.age)
print(stu1.tel)
stu1.say_hi("I love you!")
(2)构造方法_ _init_ _函数
class student:
name=None
age=None
tel=None
def __init__(self,name,age,tel):
self.name=name
self.age=age
self.tel=tel
for i in range(0,2):
a=input("输入姓名")
b=input("输入年龄")
c=input("输入电话")
s=student(a,b,c)#_ _init_ _函数为构造方法定义以后可以在创建对象时赋初值,也就是执行__init__函数
print(s.name,s.tel,s.age)
(3)魔术方法
字符串方法:将类信息转为字符串
class student:
name=None
age=None
tel=None
def __init__(self,name,age,tel):
self.name=name
self.age=age
self.tel=tel
def __str__(self):
return f"name={self.name},age={self.age},tel={self.tel}"
for i in range(0,2):#两个人0~1
a=input("输入姓名")
b=input("输入年龄")
c=input("输入电话")
s=student(a,b,c)
print(s)
lt方法:实现<的比较
class student:
name=None
age=None
tel=None
def __init__(self,name,age,tel):
self.name=name
self.age=age
self.tel=tel
def __lt__(self, other):
return self.age<other.age
s1=student("yy",38,123)
s2=student("tt",20,901)
print(s1<s2)#输出False
le方法:实现<=的比较
class student:
name=None
age=None
tel=None
def __init__(self,name,age,tel):
self.name=name
self.age=age
self.tel=tel
def __le__(self, other):#比年龄
return self.age<=other.age
s1=student("yy",20,123)
s2=student("tt",20,901)
print(s1<=s2)#输出True
eq方法:实现==的比较
class student:
name=None
age=None
tel=None
def __init__(self,name,age,tel):
self.name=name
self.age=age
self.tel=tel
def __eq__(self, other):
return self.age==other.age
s1=student("yy",20,123)
s2=student("tt",20,901)
print(s1==s2)#输出True
class phone:
__is_5g_enable=True
def __check_5g(self):
if self.__is_5g_enable==True:
print("5g开启")
else:
print("5g关闭,使用4g网络")
def call_by_5g(self):
self.__check_5g()
print("正在通话中")
s=phone()
s.call_by_5g()
二.继承
(1)单继承
class iphone:
producer="pingguo"#成员变量设置用等号
def call(self):
print("正在通话中")
class iphone15(iphone):
name="iPhone15手机"
price=10000
color=None
i=iphone15()
print(i.producer)
i.color="pink"
i.call()
print(i.color)
继承的做法是子类的()里写父类
(2)多继承
class phone:
pro="小米"
net="5g"
def call(self):
print("能打通")
class nfc:
def fun(self):
print("nfc功能使用")
class web:
net="4g"
def inter(self):
print("5g上网")
class xiaomi(phone,nfc,web):
pass#只要从父类继承来的,自己不用再定义
r=xiaomi()
r.call()
r.fun()
r.inter()
print(r.net)#如果多个父类有同名变量,优先级从左到右递减
(1)父类重写:(更新从父类继承过来的方法/变量)
class iphone:
producer="pingguo"
def call(self):
print("正在通话中")
class iphone15(iphone):
producer = "huawei"#重写父类成员变量
name="iPhone15手机"
price=10000
color=None
def call(self):#重写父类方法
print("hi")
i=iphone15()
print(i.producer)
i.color="pink"
i.call()
print(i.color)
(2)在子类里调用父类变量和父类方法
方式一:
父类名.父类变量名
父类名.父类方法名(self,如果还有参数就传入)
方式二:
super().变量名
super().方法名(如果有参数就传入)#可以省略self
class iphone:
p=6
date=1
producer="pingguo"
def call(self):
print("正在通话中")
def tel(self,msg):
print("电话",msg)
class iphone15(iphone):
producer = "huawei"
name="iPhone15手机"
price=10000
color=None
def call(self):
print("hi")
def com(self):
print(f"producer在父类中是{iphone.producer}")
print(f"p是{super().p}")
iphone.call(self)
super().tel("215")
i=iphone15()
i.com()
三.多态
(1)子类重写父类方法并更改
class Animal:
def speak(self):
pass
class dog(Animal):
def speak(self):
print("汪汪汪")
class cat(Animal):
def speak(self):
print("喵喵喵")
def say(animal:Animal):#利用多态传入,子对象名.父类名
animal.speak()#子对象名.重写方法名()
Dog=dog()#创建子对象
say(Dog)#传入子对象
Cat=cat()
say(Cat)
(2)抽象类
class AC:#抽象类
def cool(self):
pass
def warm(self):
pass
def hot(self):
pass
class media(AC):
def cool(self):
print("美的在降温")
def warm(self):
print("美的在温暖")
def hot(self):
print("美的加热")
class gree(AC):
def cool(self):
print("格力在降温")
def warm(self):
print("格力在温暖")
def hot(self):
print("格力在加热")
def cool_con(ac:AC):
ac.cool()
M=media()
cool_con(M)
def warm_con(ac:AC):
ac.warm()
warm_con(M)
a=gree()
warm_con(a)
标签:tel,name,对象,self,print,age,def From: https://www.cnblogs.com/luckyhappyyaoyao/p/18324192