1、https://py.eastlakeside.cn/book/DataStructures/classes.html 2、https://mofanpy.com/tutorials/python-basic/interactive-python/class/

通常约定类的首字母要大写
self是作为类自己的一个索引,不管你在定义类的时候,想要获取这个类的什么属性或功能,都可以通过self来获取

实例变量与类变量

实例变量用于每个对象是唯一的数据
类变量用于在类的不同实例之间共享的数据

  1. class Cal(object):
  2. # pi 是类变量
  3. pi = 3.142
  4. def __init__(self, radius):
  5. # self.radius 是实例变量
  6. self.radius = radius
  7. def area(self):
  8. return self.pi * (self.radius ** 2)
  9. a = Cal(32)
  10. a.area()
  11. # Output: 3217.408
  12. a.pi
  13. # Output: 3.142
  14. a.pi = 43
  15. a.pi
  16. # Output: 43
  17. b = Cal(44)
  18. b.area()
  19. # Output: 6082.912
  20. b.pi
  21. # Output: 3.142
  22. b.pi = 50
  23. b.pi
  24. # Output: 50
  1. class SuperClass(object):
  2. superpowers = []
  3. def __init__(self, name):
  4. self.name = name
  5. def add_superpower(self, power):
  6. self.superpowers.append(power)
  7. foo = SuperClass('foo')
  8. bar = SuperClass('bar')
  9. foo.name
  10. # Output: 'foo'
  11. bar.name
  12. # Output: 'bar'
  13. foo.add_superpower('fly')
  14. bar.superpowers
  15. # Output: ['fly']
  16. foo.superpowers
  17. # Output: ['fly']

魔术方法

__init__

是一个类初始化器,每当类创建一个类实例时,都会调用其__init__方法,例如:

  1. class GetTest(object):
  2. def __init__(self):
  3. print('Greetings!!')
  4. def another_method(self):
  5. print('I am another method which is not'
  6. ' automatically called')
  7. a = GetTest()
  8. # Output: Greetings!!
  9. a.another_method()
  10. # Output: I am another method which is not automatically
  11. # called

在创建实例后会立即调用**__init__**,还可以在初始化期间将参数传递给类

  1. class GetTest(object):
  2. def __init__(self, name):
  3. print('Greetings!! {0}'.format(name))
  4. def another_method(self):
  5. print('I am another method which is not'
  6. ' automatically called')
  7. a = GetTest('yasoob')
  8. # Output: Greetings!! yasoob
  9. # Try creating an instance without the name arguments
  10. b = GetTest()
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. TypeError: __init__() takes exactly 2 arguments (1 given)

__getitem__

  1. class GetTest(object):
  2. def __init__(self):
  3. self.info = {
  4. 'name':'Yasoob',
  5. 'country':'Pakistan',
  6. 'number':12345812
  7. }
  8. def __getitem__(self,i):
  9. return self.info[i]
  10. foo = GetTest()
  11. foo['name']
  12. # Output: 'Yasoob'
  13. foo['number']
  14. # Output: 12345812

继承

定义文本文件和视频文件,如果分开定义两个类,可以这样写:

  1. class Video:
  2. def __init__(self, name, window_size=(1080, 720)):
  3. self.name = name
  4. self.window_size = window_size
  5. self.create_time = "today"
  6. class Text:
  7. def __init__(self, name, language="zh-cn"):
  8. self.name = name
  9. self.language = language
  10. self.create_time = "today"

这两个文件其实是有共性的,比如都有name
它们可以抽象出一个更底层的类,也就是“文件类”。这个类包含了属于文件所具备的共同属性和功能
可以通过继承的方式,将细分类嵌入到抽象类中,减少共有属性/功能的重复开发

  1. class File:
  2. def __init__(self,name,create_time="today"):
  3. self.name = name
  4. self.create_time = create_time
  5. def get_info(self):
  6. return self.name + "is create at " + self.create_time
  7. class Video(File): #继承了File的属性和方法
  8. def __init__(self,name,window_size=(1080,720)):
  9. #将公用属性的设置导入
  10. super.__init__(name=name,create_time="today")
  11. self.window_size = window_size
  12. class Text(File):
  13. def __init__(self,name,language="zh-cn"):
  14. super().__init__(name=name,create_time="today")
  15. self.language = language
  16. def get_more_info(self):
  17. return self.get_info()+", using language of " + self.language
  18. v = Video("my_video")
  19. t = Text("my_text")
  20. print(v.get_info()) #调用父类的功能
  21. print(t.create_time) #调用父类的属性