Classes, inheritance and mro lists in python

  1. kind

class Car:
    def _init_(self, make, model, year):
        """self保存了当前创建的实例在内存中的地址,和形参相关联的值可以通过self关联到当前创建的实例的属性"""
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year}{self.make}{self.model}"
        return long_name.title()
"""利用类可以生成实例,然后将这个实例赋给变量my_new_car,实质是将实例的地址赋给了变量my_new_car"""
my_new_car = Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())

Classes in python are a feature of object-oriented languages. Classes can represent a large category in real life, such as cars. Using classes to generate instances can represent cars of various brands, models, and years. Both self and my_new_car in the above program are references to the instance itself. It's just that self is used inside the Car class, and my_new_car is used outside the Car class.

  1. method_init_()

The _init_() method is a special method in a python class that is automatically run whenever a new instance is generated from the class. In the above code, _init_() is defined to contain 4 formal parameters: self, make, model, year. self is required and must precede other parameters. Because when python calls this method to create a Car instance, the actual parameter self will be automatically passed in. Every method call associated with an instance automatically passes the argument self, which is a reference to the instance itself, giving the instance access to methods and properties in the class.

  1. inherit

  1. Subclass method _init_()

When writing a new class based on an existing class, the method _init_() of the parent class is usually called. This will initialize all properties in the superclass's _init_() method, allowing subclasses to include them. The relationship between the parent class and the subclass can be compared to the relationship between the car and the electric car. Next, we use the inheritance method to create the subclass ElectricCar class of the Car class. When writing the inheritance code, the code of the parent class must appear in front of the subclass .

class Car:
    def _init_(self, make, model, year):
        """self保存了当前创建的实例在内存中的地址,和形参相关联的值可以通过self关联到当前创建的实例的属性"""
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year}{self.make}{self.model}"
        return long_name.title()
"""利用类可以生成实例,然后将这个实例赋给变量my_new_car,实质是将实例的地址赋给了变量my_new_car"""
my_new_car = Car('audi','a4',2019)
print(my_new_car.get_descriptive_name())

"""定义子类时,必须在圆括号内指定父类的名称"""
class ElectricCar(Car):
    """接收创建Car实例所需的信息"""
    def _init_(self, make, model, year):
        """super()是一个特殊函数,让你能够调用父类的方法。这行代码让python调用Car类的方法_init_(),让ElectricCar实例包含这个方法中定义的所有属性"""
        super()._init_(make, model, year)
        """也可以写作super(ElectricCar, self)._init_(make, model, year),这是最完备的写法"""
my_tesla = ElectricCar('tesla','model s','2019')
print(my_tesla.get_descriptive_name())
  1. Common uses of super

The above code shows a common usage of super in python, which is used for the subclass to call the initialization method of the parent class. In the inheritance of the class, if a method is redefined, the method will override the method of the same name of the parent class, but sometimes , we hope to realize the functions of the parent class at the same time. At this time, we need to call the method of the parent class, which can be realized through super(), such as

class Animal(object):
    def __init__(self, name):
        self.name = name

    def greet(self):
        print('the animal name is %s' % self.name)


class Dog(Animal):
    def greet(self):
        super(Dog, self).greet()
        print('wangwang')


dog = Dog('huang')
dog.greet()

The result is

the animal name is huang
wangwang

Guess you like

Origin blog.csdn.net/qq_44327024/article/details/129063899