原文地址https://blog.csdn.net/weixin_42134789/article/details/80194788
https://blog.csdn.net/happyjxt/article/details/50760467
类(Class)与对象(Object)
# 创建一个学生类 class Student: # 定义学生属性,初始化方法 def __init__(self, name, score): self.name = name self.score = score # 定义打印学生信息的方法 def show(self): print("Name: {}. Score: {}".format(self.name, self.score))
在这个案例中,我们只定义了一个抽象的类,电脑并没有创建什么存储空间。只有当我们完成类的实例化(Instance)时,电脑才会创建一个具体的对象(Object),并为之分配存储空间。所以对象(Object)是类(Class)的一个实例。
要创建一个具体的学生对象(Object),我们还需要输入:
student1 = Student("John", 100) student2 = Student("Lucy", 99)
这个新Student类看上去应该是这样的:
# 创建一个学生类 class Student: # number属于类变量,定义在方法外,不属于具体实例 number = 0 # 定义学生属性,初始化方法 # name和score属于实例变量,定义在方法里 def __init__(self, name, score): self.name = name self.score = score # 此处有错误 number = number + 1 # 定义打印学生信息的方法 def show(self): print("Name: {}. Score: {}".format(self.name, self.score))
# 创建一个学生类 class Student: # number属于类变量,不属于某个具体的学生实例 number = 0 # 定义学生属性,初始化方法 # name和score属于实例变量 def __init__(self, name, score): self.name = name self.score = score Student.number = Student.number + 1 # 定义打印学生信息的方法 def show(self): print("Name: {}. Score: {}".format(self.name, self.score)) # 实例化,创建对象 student1 = Student("John", 100) student2 = Student("Lucy", 99) print(Student.number) # 打印2 print(student1.__class__.number) # 打印2
class Student: # number属于类变量,不属于某个具体的学生实例 number = 0 # 定义学生属性,初始化方法 # name和score属于实例变量 def __init__(self, name, score): self.name = name self.score = score Student.number = Student.number + 1 # 定义打印学生信息的方法 def show(self): print("Name: {}. Score: {}".format(self.name, self.score)) # 定义类方法,打印学生的数量 @classmethod def total(cls): print("Total: {0}".format(cls.number)) # 实例化,创建对象 student1 = Student("John", 100) student2 = Student("Lucy", 99) Student.total() # 打印 Total: 2
# 创建一个学生类 class Student: # 定义学生属性,初始化方法 # name和score属于实例变量, 其中__score属于私有变量 def __init__(self, name, score): self.name = name self.__score = score # 定义打印学生信息的方法 def show(self): print("Name: {}. Score: {}".format(self.name, self.__score)) # 实例化,创建对象 student1 = Student("John", 100) student1.show() # 打印 Name: John, Score: 100 student1.__score # 打印出错,该属性不能从外部访问。
# 创建一个学生类 class Student: # 定义学生属性,初始化方法 # name和score属于实例变量, 其中score属于私有变量 def __init__(self, name, score): self.name = name self.__score = score # 利用property装饰器把函数伪装成属性 @property def score(self): print("Name: {}. Score: {}".format(self.name, self.__score)) # 实例化,创建对象 student1 = Student("John", 100) student1.score # 打印 Name: John. Score: 100
# 创建父类学校成员SchoolMember class SchoolMember: def __init__(self, name, age): self.name = name self.age = age def tell(self): # 打印个人信息 print('Name:"{}" Age:"{}"'.format(self.name, self.age), end=" ") # 创建子类老师 Teacher class Teacher(SchoolMember): def __init__(self, name, age, salary): SchoolMember.__init__(self, name, age) # 利用父类进行初始化 self.salary = salary # 方法重写 def tell(self): SchoolMember.tell(self) print('Salary: {}'.format(self.salary)) # 创建子类学生Student class Student(SchoolMember): def __init__(self, name, age, score): SchoolMember.__init__(self, name, age) self.score = score def tell(self): SchoolMember.tell(self) print('score: {}'.format(self.score)) teacher1 = Teacher("John", 44, "$60000") student1 = Student("Mary", 12, 99) teacher1.tell() # 打印 Name:"John" Age:"44" Salary: $60000 student1.tell() # Name:"Mary" Age:"12" score: 99
# 创建子类学生Student class Student(SchoolMember): def __init__(self, name, age, score): SchoolMember.__init__(self, name, age) self.score = score def tell(self): super().tell() # 等同于 SchoolMember.tell(self) print('score: {}'.format(self.score))