Question about inheritance example of class

Asked 2 years ago, Updated 2 years ago, 88 views

Define the bike class so that the following code works: The terminal tram class inherits the car class.

bicycle = bicycle (2,100)

Bicycle.Price

100

The answer to this question is

class car:

    def__init__(self, wheel, price):
        self.Wheels = Wheels
        self.Price = price

class bicycle (tea):

    def__inif__(self, wheel, price):
        self.Wheels = Wheels
        self.Price = price

Why is the class bicycle inherited here self again?I'd like to know if you get the price of wheels and self. Just

class car:

    def__init__(self, wheel, price):
        self.Wheels = Wheels
        self.Price = price

class bicycle (tea):

    def __inif__(self):
        print(self.price)

I wonder if I can't do it like this.

class inheritance python

2022-09-20 15:31

1 Answers

self in Python's class.Declares in the form XXX correspond to instance variables.

A variable that is unique to an instance of that class regardless of other instances of the same class when it is created.

This variable is not a predetermined value, but a value determined by the value received.

Looking at the code you wrote, the class order receives the values corresponding to the wheels and prices as parameters when declaring an instance and stores each as an instance variable.

If you inherit a class and you create a new class, and you don't pass on the parameters, of course you won't be able to do the part that receives the parameter and stores the value, and naturally you won't be able to do the code that loads the value.

inherits instead of __ init and __ of the class what no need to write over and over it the same process can be performed in the same way.

class car:
    def__init__(self, wheel, price):
        self.Wheels = Wheels
        self.Price = price

class bicycle (tea):
    def__inif__(self, wheel, price):
        super (bicycle, self).__init__ (wheels, prices)


2022-09-20 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.