class Student:
def __init__(self, name, age):
self.name = name
self.__age = age # 年龄不希望在类的外部被使用,所以加了两个_
def show(self):
print(self.name, self.__age)
stu = Student('张三', 19)
stu.show() # 张三 19
print(stu.name) # 张三 正常
# print(stu.__age) # 报错
print(stu._Student__age) # 在类的外部可以通过_Student__age进行访问
calss 子类类名( 父类1,父类2... ):
pass
如果一个类没有继承任何类,则默认继承object
Python支持多继承
定义子类时,必须在其构造函数中调用父类的构造函数
class Person(object): # Person默认继承object,也可以不写object
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name, self.age)
# 定义子类
class Student(Person):
def __init__(self, name, age, stu_no):
super().__init__(name, age)
self.stu_no = stu_no
class Teacher(Person):
def __init__(self, name, age, teacherofyear):
super().__init__(name, age)
self.teacherofyear = teacherofyear
stu = Student('张三', 20, '100001')
tea = Teacher('李四', 30, '200001')
stu.info() # 张三 20
tea.info() # 李四 30
class A():
pass
class B():
pass
class C(A, B):
pass
class Person(object): # Person默认继承object,也可以不写object
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name, self.age)
# 定义子类
class Student(Person):
def __init__(self, name, age, stu_no):
super().__init__(name, age)
self.stu_no = stu_no
def info(self): # 重写
super().info()
print(self.stu_no)
class Teacher(Person):
def __init__(self, name, age, tea_no):
super().__init__(name, age)
self.tea_no = tea_no
def info(self): # 重写
super().info()
print(self.tea_no)
stu = Student('张三', 20, '100001')
tea = Teacher('李四', 30, '200001')
stu.info() # 张三 20
# 100001
tea.info() # 李四 30
# 200001
简单地说,多肽就是“具有多种形态”,它指的是:即便不知道一个变量所引用的对象到底是什么类型,乃然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法。
class Animal(object):
def eat(self):
print('动物吃东西')
class Dog(Animal):
def eat(self):
print('狗吃屎')
class Cat(Animal):
def eat(self):
print('猫吃鱼')
class Person(object):
def eat(self):
print('人吃饭')
#定义函数
def fun(obj):
obj.eat()
#调用函数
fun(Dog())
fun(Cat())
fun(Animal())
fun(Person())
静态语言实现多态的三个必要条件
动态语言的多态崇尚“鸭子类型” 当看到一只鸟走起来像鸭子、游泳起来像鸭子、收起来也像鸭子,那么这只鸟就可以被成为鸭子。在鸭子类型中。不需要关心对象是什么类型,到底是不是鸭子,只关心对象的行为。
class CPU:
pass
class DISK:
pass
class PC:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
# 变量的赋值
cpu1 = CPU()
cpu2 = cpu1
print(cpu1, id(cpu1))
print(cpu2, id(cpu2))
print("-----------------")
# (1)类浅拷贝
disk = DISK()
pc1 = PC(cpu1, disk)
# 浅拷贝
import copy
pc2 = copy.copy(pc1)
print(pc1, pc1.cpu, pc1.disk)
print(pc2, pc2.cpu, pc2.disk)
print("-----------------")
# 深拷贝
pc3 = copy.copy(pc1)
print(pc1, pc1.cpu, pc1.disk)
print(pc3, pc3.cpu, pc3.disk)
以上代码运行的结果
<__main__.CPU object at 0x7f784278ffd0> 140154488029136
<__main__.CPU object at 0x7f784278ffd0> 140154488029136
-----------------
<__main__.PC object at 0x7f784278fe50> <__main__.CPU object at 0x7f784278ffd0> <__main__.DISK object at 0x7f784278fe80>
<__main__.PC object at 0x7f784278fd60> <__main__.CPU object at 0x7f784278ffd0> <__main__.DISK object at 0x7f784278fe80>
-----------------
<__main__.PC object at 0x7f784278fe50> <__main__.CPU object at 0x7f784278ffd0> <__main__.DISK object at 0x7f784278fe80>
<__main__.PC object at 0x7f784278f670> <__main__.CPU object at 0x7f78427b8460> <__main__.DISK object at 0x7f78427b83a0>