广东专业移动网站建设哪家好,网络推广公司简介模板,企业建站系统还有没有前景可言,吉林省建设工程质监站网站目录
Python继承
一、创建父类
二、创建子类
三、添加 init() 函数
四、使用 super() 函数
五、添加属性
六、添加方法 Python继承 继承允许我们定义一个从另一个类继承所有方法和属性的类。父类是被继承的类#xff0c;也称为基类。子类是从另一个类继承的类#xff…目录
Python继承
一、创建父类
二、创建子类
三、添加 init() 函数
四、使用 super() 函数
五、添加属性
六、添加方法 Python继承 继承允许我们定义一个从另一个类继承所有方法和属性的类。父类是被继承的类也称为基类。子类是从另一个类继承的类也称为派生类。
一、创建父类
任何类都可以是父类因此语法与创建任何其他类相同 这里创建一个名为Person、 firstname和lastname属性的类以及一个printname方法
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)x Person(洋洋, 笨)
x.printname()
返回 二、创建子类
创建从另一个类继承功能的类请在创建子类时将父类作为参数发送。 例如创建一个名为 的类Student它将继承该类的属性和方法Person
class Student(Person):pass注意 pass 当您不想向类添加任何其他属性或方法时请使用关键字。 现在 Student 类具有与 Person 类相同的属性和方法。使用Student类创建对象然后执行printname方法
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)class Student(Person):passx Student(洋洋, 笨)
x.printname()
返回还是一样 三、添加 init() 函数
到目前为止我们已经创建了一个继承父类的属性和方法的子类。我们想将__init__()函数添加到子类而不是pass关键字。注意init()每次使用该类创建新对象时都会自动调用该函数。 例如将__init__()函数添加到 Student类中
class Student(Person):def __init__(self, fname, lname):添加__init__()函数后子类将不再继承父类的__init__()函数。
注孩子的__init__() 功能覆盖父母的继承 init()功能。为了保持父__init__() 函数的继承添加对父函数的调用__init__()
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)class Student(Person):def __init__(self, fname, lname):Person.__init__(self, fname, lname)x Student(洋洋, 笨)
x.printname()这样效果一样 四、使用 super() 函数
Python 还有一个super()函数可以让子类继承其父类的所有方法和属性
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)class Student(Person):def __init__(self, fname, lname):super().__init__(fname, lname)x Student(洋洋, 笨)
x.printname()一样返回 通过使用该super()函数您不必使用父元素的名称它会自动从其父元素继承方法和属性。
五、添加属性
添加一个属性调用graduationyear到 Student类
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)class Student(Person):def __init__(self, fname, lname):super().__init__(fname, lname)self.graduationyear 2022x Student(洋洋, 笨)
print(x.graduationyear)
返回 年份2019应该是一个变量并Student在创建学生对象时传递给 类。为此请在 init() 函数中添加另一个参数。 添加year参数并在创建对象时传递正确的年份
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)class Student(Person):def __init__(self, fname, lname, year):super().__init__(fname, lname)self.graduationyear yearx Student(洋洋, 笨, 2022)
print(x.graduationyear)返回一样为2022
六、添加方法
添加一个调用welcome到 Student类的方法
class Person:def __init__(self, fname, lname):self.firstname fnameself.lastname lnamedef printname(self):print(self.firstname, self.lastname)class Student(Person):def __init__(self, fname, lname, year):super().__init__(fname, lname)self.graduationyear yeardef welcome(self):print(Welcome, self.firstname, self.lastname, to the class of, self.graduationyear)x Student(洋洋, 笨, 2022)
x.welcome()返回