1 封装
1.1 理解
属性和方法书写到类里面的操作即为封装
封装可以理解成私有化属性和私有化方法
1.2 示例代码
class Person():
p_num = 0
def __init__(self, name, age, sex, job):
self.__name = name
self.age = age
self.sex = sex
self.job = job
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def eat(self):
print("吃饭")
def __sleep(self):
print("睡觉")
def show_sleep(self):
self.__sleep()
chinese_teacher = Person("张三", 24, "男", "语文老师")
print(f"name的值为: {chinese_teacher.get_name()}")
chinese_teacher.show_sleep()
1.3 运行截图
2 继承
2.1 理解
子类可以继承父类的所有非私有属性和方法
为了使代码更加简洁
2.2 分类
2.2.1 常规继承
a 语法
class 类A():
class 类B(类A):
b 示例代码
class Animal():
def __init__(self, name, sex, age):
print("父类的构造方法")
self.name = name
self.sex = sex
self.__age = age
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
def behavior(self):
print("动物的叫声")
class Cat(Animal):
def __init__(self, name, sex, age):
print("子类的构造方法")
super().__init__(name, sex, age)
def behavior(self):
print("喵喵喵")
cat = Cat("小金", "雄性", 5)
cat.behavior()
c 运行截图
2.2.2 继承传递
a 语法
class 类A():
class 类B(类A):
class 类c(类B):
b 示例代码
class Animal():
def __init__(self, name, sex, age):
print("父类的构造方法")
self.name = name
self.sex = sex
self.__age = age
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
def behavior(self):
print("动物的叫声")
def __str__(self):
return f"姓名为: {self.name},性别为:{self.sex},年龄为:{self.__age}"
def describe(self):
return self.__str__()
class Cat(Animal):
num = 0
def __init__(self, name, sex, age):
print("子类的构造方法")
super().__init__(name, sex, age)
self.num += 1
def behavior(self):
print("喵喵喵")
class DragonLi(Cat):
def __init__(self, name, sex, age):
print("孙类的构造方法")
super().__init__(name, sex, age)
def behavior(self):
print("出去玩耍了")
cat = DragonLi("小金", "雄性", 5)
print("修改前: ", cat.describe())
cat.set_age(6)
print("修改后: ", cat.describe())
cat.behavior()
print("当前猫的数量为:", cat.num)
c 运行截图
2.2.3 多继承(继承多个不同父类)
a 语法
class 类A():
class 类B():
class 类C(类A,类B):
b 示例代码
class Animal():
def __init__(self, name, sex, age):
print("父类的构造方法")
self.name = name
self.sex = sex
self.__age = age
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
def behavior(self):
print("动物的叫声")
def __str__(self):
return f"姓名为: {self.name},性别为:{self.sex},年龄为:{self.__age}"
def describe(self):
return self.__str__()
def eat_food(self):
print("以有机物为食")
class Fly():
def __init__(self,tool):
self.tool=tool
def behavior(self):
print("芜湖起飞")
def fly_reason(self):
print("符合空气动力学原理")
class Bird(Animal,Fly):
num = 0
def __init__(self, name, sex, age):
print("子类的构造方法")
super().__init__(name, sex, age)
self.num += 1
bird = Bird("小红", "雄性", 5)
print("修改前: ", bird.describe())
bird.set_age(6)
print("修改后: ", bird.describe())
bird.behavior()
bird.eat_food()
bird.fly_reason()
c 运行截图
3 多态
3.1 语法
对象=父类(子类实例化对象)
3.2 示例代码
class Cat():
def __init__(self, name, sex):
self.name = name
self.sex = sex
def behavior(self):
print("猫的行为")
class DragonLi(Cat):
def __init__(self, name, sex):
super().__init__(name, sex)
def behavior(self):
print(f"品种为狸花猫的{self.name}喜欢出去打架")
class OrangeCat(Cat):
def __init__(self, name, sex):
super().__init__(name, sex)
def behavior(self):
print(f"品种为橘猫的{self.name}喜欢干饭")
class Person():
def search_attitude(self, obj):
obj.behavior()
d = DragonLi("小花", "雄性")
o = OrangeCat("小黄", "雌性")
p = Person()
p.search_attitude(d)
p.search_attitude(o)
3.3 运行截图