9.面向对象、类

# 面向对象
class Student():
    # (1)类变量
    sum1 = 0
    name = 'qiyue'
    age = 0    

    # 构造函数
    # 初始化类的各种特征和行为
    def __init__(self,name,age):
        # 初始化对象的属性
        # (2)实例变量
        self.name = name
        self.age = age
        self.__score = 0

        # self.__class__.sum1 +=1
        # print('当前班级学生总数为:' + str(self.__class__.sum1))

        # print('student')
        # print(age)

        # print(name)
        # print(self.name)

        # print(sum1)                # NameError: name 'sum1' is not defined
        # print(Student.sum1)        # 0
        # print(self.__class__.sum1) # 0

    # 行为与特征
    # 1.实例方法
    def do_homework(self):
        # 内部调用类方法
        self.do_english()
        print(self)
        # print('homework')

    def do_english(self):
        print('内部调用')

    def marking(self,score):
        if score < 0:
            return '分数不能为负数'
        self.__score = score
        print(self.name + '同学本次考试分数为:' +str(self.__score))
    
    # 2.类方法
    @classmethod
    def plus_sum(cls):
        cls.sum1 +=1
        print('类方法--当前班级学生总数为:' + str(cls.sum1))
    
    # 3.静态方法
    @staticmethod
    def add(x,y):
        print(Student.sum1)  # 0
        # print(name)        # NameError: name 'name' is not defined
        print('this is static method')

# class Printer():
#     def print_file(self):
#         print('name:' + self.name)
#         print('age:' + str(self.age))

# student = Student()
# student.print_file()

# student1 = Student()
# student2 = Student()
# student3 = Student()
# print(id(student1))  #79438224
# print(id(student2))  #79438192
# print(id(student3))  #79439440

# student1.do_homework()
# a=student1.__init__()
# print(type(a))

# student1 = Student('zouke',18)
# student2 = Student('喜小乐',22)
# print(student1.name)     # zouke    打印实例变量
# print(student2.name)     # 喜小乐    打印实例变量
# print(Student.name)      # qiyue    打印类变量
# print(student1.__dict__) # {'name': 'zouke', 'age': 18}
# print(Student.__dict__)  # {'__module__': '__main__', 'name': 'qiyue', 'age': 0, '__init__': <function Student.__init__ at 0x055C5150>, 'do_homework': <function Student.do_homework at 0x055C5108>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}

# student1.do_homework()   # <__main__.Student object at 0x04B02170>
# student2.do_homework()   # <__main__.Student object at 0x04B02270>

# print(Student.sum1)      # 0

student1 = Student('zouke',18)      # 当前班级学生总数为:1
student2 = Student('喜小乐',22)      # 当前班级学生总数为:2
student3 = Student('zhangsan',23)   # 当前班级学生总数为:3

# 类方法的调用
# student1.plus_sum() # 类方法--当前班级学生总数为:1
# student2.plus_sum() # 类方法--当前班级学生总数为:2
# student3.plus_sum() # 类方法--当前班级学生总数为:3

# student1.add(1,2)   # this is static method
# Student.add(1,2)    # this is static method
# student1.plus_sum()
# Student.plus_sum()

# result = student1.marking(59)  # zouke同学本次考试分数为:59
# print(result)                  # 分数不能为负数

# 成员的可见性
# student1.__score = -1
# print(student1.__dict__) # {'name': 'zouke', 'age': 18, '_Student__score': 59, '__score': -1}
# print(student1.__score)  # -1

# print(student2.__dict__) # {'name': '喜小乐', 'age': 22, '_Student__score': 0}
# print(student2.__score)  # AttributeError: 'Student' object has no attribute '__score'
# print(student2._Student__score) # 0


# 面向对象的3大特性
# 继承、封装、多态

n3.py

class Human():
    sum = 0

    def __init__(self,name,age):
        self.name = name
        self.age = age
    
    def get_name(self):
        print(self.name)

    def do_homework(self):
        print('this is parent homework')

n4.py

from n3 import Human

class Student(Human):
    def __init__(self,school,name,age):  
        self.school = school

        # 方法1--推荐
        # Human.__init__(self,name,age)

        # 方法2--不推荐
        super(Student,self).__init__(name,age)

    def do_homework(self):
        print('this is child homework')
        super(Student,self).do_homework()

student1 = Student('中国矿业大学','zouke',18)
# print(student1.sum)
# print(Student.sum)
# print(student1.name)  # zouke
# print(student1.age)   # 18
# student1.get_name()   # zouke

# Student.do_homework()         # TypeError: do_homework() missing 1 required positional argument: 'self'
# Student.do_homework(student1) # homework
# Student.do_homework('')       # homework

student1.do_homework()  # this is child homework
原文地址:https://www.cnblogs.com/zouke1220/p/8875726.html