Python class, Python

Source: Internet
Author: User

Python class, Python
1. Create and use classes

The class can simulate almost anything.

1.1 create a Dog class

Each instance created based on the Dog class will store the name and age. We have given each puppy the ability to squat sit () and roll roll_over ().

#! /Usr/bin/env pythonclass Dog: "" A simple attempt to simulate a puppy "def _ init _ (self, name, age ): "initialize attribute name and age" "self. name = name self. age = age def sit (self): "When a simulated puppy is taken command" print (self. name. title () + "is now sitting! ") Def roll_over (self):" rolling when a simulated puppy is called "print (self. name. title () +" roll over! ") My_dog = Dog ('white', 18) print (" My dog name is "+ my_dog.name.title () + ". ") print (" My dog is "+ str (my_dog.age) +" years old! ") My_dog.sit () my_dog.roll_over () ========================================================== ========= My dog name is White. my dog is 18 years old! White is now sitting! Whiteroll over!

1.2 create multiple instances

#! /Usr/bin/env pythonclass Dog: "" A simple attempt to simulate a puppy "def _ init _ (self, name, age ): "initialize attribute name and age" "self. name = name self. age = age def sit (self): "When a simulated puppy is taken command" print (self. name. title () + "is now sitting! ") Def roll_over (self):" rolling when a simulated puppy is called "print (self. name. title () +" roll over! ") My_dog = Dog ('white', 18) your_dog = Dog ('black', 19) my_dog.sit () my_dog.roll_over () your_dog.sit () your_dog.roll_over () ========================================================== ====== White is now sitting! Whiteroll over! Black is now sitting! Blackroll over!

 

2. Usage classes and Instances

① Car

#! /Usr/bin/env pythonclass Car (): "" A simple attempt to simulate a Car "def _ init _ (self, make, model, year ): "initialize the attributes of the car" self. make = make self. model = model self. year = year def get_descriptive_name (self): "return the neat description" long_name = str (self. year) + ''+ self. make + ''+ self. model return long_name.title () my_new_car = Car ('audi ', 'a4', 2017) print (my_new_car.get_descriptive_name ()) ========================================================== ====== 2017 Audi A4

② Specify the default value for the attribute

#! /Usr/bin/env pythonclass Car (): "" A simple attempt to simulate a Car "def _ init _ (self, make, model, year ): "initialize the attributes of the car" self. make = make self. model = model self. year = year self. odometer_reading = 0 def get_descriptive_name (self): "returns the neat description" long_name = str (self. year) + ''+ self. make + ''+ self. model return long_name.title () def read_odometer (self): "print a message indicating the vehicle's mileage" print ("This car has" + str (self. odometer_reading) + "miles on it. ") my_new_car = Car ('audi ', 'a4', 2017) print (my_new_car.get_descriptive_name () my_new_car.read_odometer () ========================================================== ==== 2017 Audi A4This car has 0 miles on it.

③ Modify the attribute value

You can modify the attribute values in three different ways: modifying the attributes directly through the instance, setting the attributes by method, and increasing the attributes by method (adding specific values)

  • Directly modify the attribute value

  

#! /Usr/bin/env pythonclass Car (): "" A simple attempt to simulate a Car "def _ init _ (self, make, model, year ): "initialize the attributes of the car" self. make = make self. model = model self. year = year self. odometer_reading = 23 def get_descriptive_name (self): "returns the neat description" long_name = str (self. year) + ''+ self. make + ''+ self. model return long_name.title () def read_odometer (self): "print a message indicating the vehicle's mileage" print ("This car has" + str (self. odometer_reading) + "miles on it. ") my_new_car = Car ('audi ', 'a4', 2017) print (my_new_car.get_descriptive_name () my_new_car.read_odometer () ========================================================== ====== 2017 Audi A4This car has 23 miles on it.
  • Modifying attribute values using methods
#! /Usr/bin/env pythonclass Car (): "" A simple attempt to simulate a Car "def _ init _ (self, make, model, year ): "initialize the attributes of the car" self. make = make self. model = model self. year = year self. odometer_reading = 0 def get_descriptive_name (self): "returns the neat description" long_name = str (self. year) + ''+ self. make + ''+ self. model return long_name.title () def read_odometer (self): "print a message indicating the vehicle's mileage" print ("This car has" + str (self. odometer_reading) + "miles on it. ") def update_odometer (self, mileage):" sets the mileage table reading to the specified value "self. odometer_reading = mileagemy_new_car = Car ('audi ', 'a4', 2017) print (bytes () my_new_car.update_odometer (23) my_new_car.read_odometer () ========================================================== ====== 2017 Audi A4This car has 23 miles on it.

The only modification to the Car class is that the update_odometer () method is added (). This method accepts a mileage value and stores it in self. odometer_reading.

 

3. Inheritance

When writing a category, it does not always start from the blank. If you want to write a special version of another ready-made class, you can use inheritance. When a class inherits from another class, it automatically obtains all the attributes and methods of the other class. The original class is called the parent class, and the new class is called the subclass. Subclass inherits all attributes and methods of its parent class, and can also define its own attributes and methods.

#! /Usr/bin/env pythonclass Car (): "" A simple attempt to simulate a Car "def _ init _ (self, make, model, year ): "initialize the attributes of the car" self. make = make self. model = model self. year = year self. odometer_reading = 0 def get_descriptive_name (self): "returns the neat description" long_name = str (self. year) + ''+ self. make + ''+ self. model return long_name.title () def read_odometer (self): "print a message indicating the vehicle's mileage" print ("This car has" + str (self. odomet Er_reading) + "miles on it. ") def update_odometer (self, mileage): if mileage> = self. odometer_reading: self. odometer_reading = mileage else: print ("You cant roll back an odometer! ") Def increment_odometer (self, miles): self. odometer_reading + = milesclass ElectricCar (Car): "" unique feature of electric vehicles "def _ init _ (self, make, model, year ): "initialize parent attributes" super (). _ init _ (make, model, year) my_tesla = ElectricCar ('Tesla ', 'model s', 2017) print (my_tesla.get_descriptive_name ()) ========================================================== == 2017 Tesla Model S

  

4. Import class
#!/usr/bin/env pythonfrom car import Carmy_new_car = Car('audi','a4',2017)print(my_new_car.get_descriptive_name())my_new_car.odometer_reading = 23my_new_car.read_odometer()=======================================2017 Audi A4This car has 23 miles on it.

Import multiple classes from one module:

From car import Car, ElectricCar

Related Article

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.