文章目录
一.类的定义
1.通过句点法直接调用
class Person:
varl = '最开始在中的'
print(Person.varl)
print('===='*15)
Person.val2 = '后来在类中的'
print(Person.val2)
class Person:
varl = '最开始在中的'
print(Person.varl)
print('===='*15)
Person.val2 = '后来在类中的'
print(Person.val2)
eg:这个val2虽然只是后加的但是他已经在类内了
2.通过实例化调用
p = Person()
print(p.varl)
print(p.val2)
p = Person()
print(p.varl)
print(p.val2)
3.实例化和类引用之间的区别
class Person:
pass
one = Person()
two = Person()
one.val = 'zxc最帅'
print(one.val)
print('++++'*15)
print(Person.val)
print('++++'*15)
print(two.val)
class Person:
pass
one = Person()
two = Person()
one.val = 'zxc最帅'
print(one.val)
print('++++'*15)
print(Person.val)
print('++++'*15)
print(two.val)
很明显,类下的是属性,每个实例化的都是特性,实例对象可以访问类下的变量,反之不可以!!!!
二.类的属性
1. 初始化__init__语句
class Cat:
def __init__(self, color, food):
self.color = color
self.food = food
kitty = Cat("xiaohuang", "milk")
print(kitty.color, kitty.food)
class Cat:
def __init__(self, color, food):
self.color = color
self.food = food
kitty = Cat("xiaohuang", "milk")
print(kitty.color, kitty.food)
2.封装(就是把封装到self中)
class Cat:
def __init__(self, color, food):
self.color = color
self.food = food
def print_cat(self):
print("{}-{}".format(self.color, self.food))
kitty = Cat("xiaohuang", "milk")
kitty.print_cat()
class Cat:
def __init__(self, color, food):
self.color = color
self.food = food
def print_cat(self):
print("{}-{}".format(self.color, self.food))
kitty = Cat("xiaohuang", "milk")
kitty.print_cat()
通过self可以把所有的属性放在一起,而且,所有类下的函数都可以调用self
3.私有属性
1.self._sth #这个是外不可访问的私有属性
2.self.__sth #这个是外部不可以访问的私有属性
class Cat:
def __init__(self, color, food, name):
self.color = color
self.food = food
self.__name = name
def print_cat(self):
print("{}-{}-{}".format(self.color, self.food, self.__name))
kitty = Cat("xiaohuang", "milk", "lalal")
kitty.print_cat()
class Cat:
def __init__(self, color, food, name):
self.color = color
self.food = food
self.__name = name
def print_cat(self):
print("{}-{}-{}".format(self.color, self.food, self.__name))
kitty = Cat("xiaohuang", "milk", "lalal")
kitty.print_cat()
两个_这里只能由类内的方法来调用,外部函数都不可以,同时外面直接调用也不行
3.不仅属性可以私有化,方法也可以,但是只能通过,方法调用方法,也就是都只能用self调用
class Cat:
def __init__(self, color, food, name):
self.color = color
self.food = food
self.__name = name
def print_cat(self):
print("{}-{}-{}".format(self.color, self.food, self.__name))
def use(self):
return self.__getname()
def __getname(self):
return "我是私有函数"
kitty = Cat("xiaohuang", "milk", "lalal")
print(kitty.use())
class Cat:
def __init__(self, color, food, name):
self.color = color
self.food = food
self.__name = name
def print_cat(self):
print("{}-{}-{}".format(self.color, self.food, self.__name))
def use(self):
return self.__getname()
def __getname(self):
return "我是私有函数"
kitty = Cat("xiaohuang", "milk", "lalal")
print(kitty.use())
三.类的继承
3.1 单继承
class Animal:
def __init__(self, color, food, name):
self.color = color
self.food = food
self.name = name
def print_(self):
print("{}-{}".format(self.color, self.food))
class Cat(Animal):
def __init__(self, color, food):
self.color = color
self.food = food
kitty = Cat("xiaohuang", "milk")
kitty.print_()
class Animal:
def __init__(self, color, food, name):
self.color = color
self.food = food
self.name = name
def print_(self):
print("{}-{}".format(self.color, self.food))
class Cat(Animal):
def __init__(self, color, food):
self.color = color
self.food = food
kitty = Cat("xiaohuang", "milk")
kitty.print_()
这里的要强调的是初始化的话,会直接使用子类的,不会调用父类的初始化,但是如果没有就会调用父类的,而其他的都是先用自己的,之后才会调用父类,但是可以直接用父类的方法,如果出现重名的话,不会报错,但是会先用子类的再用父类的,下面是实例
class Animal:
def print_(self):
print("父类")
class Cat(Animal):
def __init__(self, color,name):
self.color = color
self.name = name
def print_(self):
print("子类")
kitty = Cat("xiaohuang", "milk")
kitty.print_()
class Animal:
def print_(self):
print("父类")
class Cat(Animal):
def __init__(self, color,name):
self.color = color
self.name = name
def print_(self):
print("子类")
kitty = Cat("xiaohuang", "milk")
kitty.print_()
3.2多继承
1.主要是顺序问题
3.3继承的加强版(子类的方法通过调用父类的方法嵌套)
class A:
def run(self):
print("A")
class D(A):
def run(self):
A.run(self)
print("D")
D = D()
D.run()
class A:
def run(self):
print("A")
class D(A):
def run(self):
A.run(self)
print("D")
D = D()
D.run()
3.4还可以通过super().加函数名来调用
class A:
def run(self):
print("A")
class B:
def run(self):
print("B")
class D(B,A):
def run(self):
super().run()
print("D")
D = D()
D.run()
class A:
def run(self):
print("A")
class B:
def run(self):
print("B")
class D(B,A):
def run(self):
super().run()
print("D")
D = D()
D.run()
这里的调用遵从多继承规则,也能更方便的找到对应函数
3.5通过D.__bases__和D.mro()查看继承关系
print(D.__bases__)
print(D.mro())
前一个是查看继承的类,后一个是查看所有的继承关系
四.类的魔术方法
1.__add__方法(底层方法)
class Test:
def __init__(self, num):
self.num = num
def __add__(self, other):
# res = self.num + other.num
print("lalal")
a = Test(66)
b = Test(88)
a + b
class Test:
def __init__(self, num):
self.num = num
def __add__(self, other):
# res = self.num + other.num
print("lalal")
a = Test(66)
b = Test(88)
a + b
通过输入a + b自动调用方法
2.__str__和__repr__方法
class Test:
def __str__(self):
return "__str__方法"
def __repr__(self):
return "__repr__方法"
a = Test()
print(a)
class Test:
def __str__(self):
return "__str__方法"
def __repr__(self):
return "__repr__方法"
a = Test()
print(a)
如果没有这两种方法就返回地址,而且str优先级高,其次repr
3.call,dict,doc,class等魔术方法
class Test:
'''
啦啦啦
'''
def __init__(self, num):
self.num = num
def __call__(self, *args, **kwargs):
print("ok")
a = Test(66)
a()
print(a.__class__) # <class '__main__.Test'>
print(a.__dict__) # {'num': 66}
print(a.__doc__) # 查看文档
class Test:
'''
啦啦啦
'''
def __init__(self, num):
self.num = num
def __call__(self, *args, **kwargs):
print("ok")
a = Test(66)
a()
print(a.__class__) # <class '__main__.Test'>
print(a.__dict__) # {'num': 66}
print(a.__doc__) # 查看文档