Inheritance such as Python

Source: Internet
Author: User

# Class of Operation instance # subclass Ecar Inherits parent class car and uses instance battery as property class car ():    def __init__ (self, name, model, year):        self.name = name        Self.model = model        self.year = year        self.odometer = 0  # mileage, the default value is 0    def des_info (self):        print ( Self.name.title (), Self.model, Self.year, Self.odometer)    def set_odometer (self, num):        self.odometer = num    def get_odometer (self):        print (' Vehicle mileage: ', Self.odometer) class Ecar (CAR):    ' Subclass Ecar inherits the parent class car, has a special attribute battery '    def __init__ (self, name, model, year):        ' Initialize the parent class property, and then initialize the Unique property ' '        super (). __INIT__ ( Name, model, year)        self.battery = Battery ()  # Use Instance as attribute    def get_meter (self):        print (' Maximum mileage: ', Self.battery.get_meter ()) class battery ():    def __init__ (self, size=70):        self.size = size    def get_meter (self):        "The maximum mileage the battery can travel" ' Miles        = self.size * 5        return miles

# test Code

My_car = car (' Audi ', ' A4 ', ") My_car.des_info () My_car.set_odometer (+) my_car.get_odometer () Your_car = Ecar (' Tesla ', ' Model S ', Your_car.des_info () Your_car.get_meter ()

  

Inheritance such as Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.