Home > other >  Help to look at
Help to look at

Time:11-16

Programming design is a base class car type of Vehicle, MaxSpeed contains maximum speed, weight two instances private property; Design a derived subclass bike (Bicycle), 1 example private property increase height (height) and one member function SetMaxSpeed implementation to the parent class instance attributes MaxSpeed assignment,
Requirements:
1) according to the above described design class, and in the main function to create a subclass of instantiation objects, object and set the MaxSpeed value,
2) the use of the property to set the height for readable, can modify the properties of the



The class Vehicle (object) :
Def __init__ (self, MaxSpeed, weight) :
Self. __MaxSpeed=MaxSpeed
Self. __weight=weight

The class Bicycle (Vehicle) :
Def __init__ (self, weight, height) :
super () __init__ (0, weight)
Self. __height=height

Def SetMaxSpeed (self, speed) :
Vehicle. MaxSpeed=speed

Def __get (self) :
Return the self. __height

Def __set (self, h) :
Self. __height=h

Def __del (self) :
Del self. __height

Height=property (__get and __set __del)
Def show (self) :
Print (self. __height)
B=Bicycle (4,0.7)
B.S etMaxSpeed (40)
B.s how ()
B.h eight=1
B.s how ()



Why the super () __init__ (0, weight), can't use the name of the class. The method name () to write, why not add inside the self

CodePudding user response:

super () __init__ (0, weight)
Super () represents the base class, only when you define the class methods, the first parameter to the method the default for the self, the call of the self is no ah, here is similar to the Vehicle base class calls the init method, need not self

CodePudding user response:

If you want to use the name of the class. The method name () to write words, it is the Vehicle. The __init__ (self, 0, weight).
It is called executive, not a definition,
So through the instance method performs word is instance. __init__ (0, weight), is interpreted as class methods Vehicle. __init__ (instance, 0, weight)
  • Related