学习python 第八天

面向对象
多态:多种形态 :继承-重写
类与类之间的关系:继承 关联(组合,聚合) 依赖
关联:一个类的对象作为另一个类的属性
依赖:一个类的对象作为另一个类的方法的参数
耦合程度:继承>关联>依赖
封装 __XXX

类属性 对象属性

class A():
    name = "张三"  #类属性
    def __init__(self,age):
        self.age = age  #对象属性
    @staticmethod
    def a():
        print("类方法-静态方法")
    def aa(self):
        print("对象方法")
print(A.name)
A.a()
a = A(18)
print(a.age)
a.aa()

print(dir(object))#查看object里的方法
issubclass(A,object)判断A是不是object的子类
isinstance(a,A) a是对象A是类

例题:

class student():
    def __init__(self,name):
        self.name = name
    def study(self):
        print("我爱python")
class teacher():
    def __init__(self,stu):
        self.stu = stu
    def teach(self):
        print("教%spython"%self.stu.name)
s = student("cy")
t = teacher(s)
t.teach()
class student():
    def __init__(self,name):
        self.name = name
    def study(self):
        print("我爱python")
class teacher():
    def teach(self,stu):
        print("教%spython"%stu.name)

s = student("cy")
t = teacher()
t.teach(s)

猜你喜欢

转载自blog.csdn.net/weixin_44446703/article/details/86599259