美文网首页
Python入门与进阶(9-14)

Python入门与进阶(9-14)

作者: buaishengqi | 来源:发表于2018-05-07 19:36 被阅读11次

9-14 继承.
本节课重点讲解继承性。看思维导图


Python类.png

test1.py内容如下:

class Human():
    sum = 0
    def __init__(self,name,age): 
        self.name = name
        self.age = age  

    def get_name(self):
        print(self.name)
    

test.py内容如下:

from  test1 import Human
class Student(Human):       #student子类继承human父类
    # sum = 0  
    # def __init__(self,name,age): 
        # self.name = name
        # self.age = age  
        # self.__score = 0
        # self.__class__ .sum += 1 

    def do_homework(self):
        print('english homework')

student1 = Student('石敢当',18)
print(student1.sum)
print(Student.sum)
print(student1.name)
print(student1.age)
student1.get_name()


# 看下面的代码
from  test1 import Human
class Student(Human):       #student子类继承human父类
    # sum = 0  
    def __init__(self,school,name,age): 
        self.school = school  #school 属于子类,name,age属于父类,那么如何在子类里把name和age传给父类?
        Human.__init__(self,name,age)#这样就实现了在子类里调用父类的构造函数
        # self.name = name
        # self.age = age  
        # self.__score = 0
        # self.__class__ .sum += 1 

    def do_homework(self):
        print('english homework')

student1 = Student('人民路小学','石敢当',18)
# print(student1.sum)
# print(Student.sum)
print(student1.name)
print(student1.age)
# student1.get_name()
1.jpg 父类.png

相关文章

网友评论

      本文标题:Python入门与进阶(9-14)

      本文链接:https://www.haomeiwen.com/subject/slqvrftx.html