- Class: used to describe a collection of objects with the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
- Method: a function defined in a class.
- Class variables: class variables are common to the entire instantiated object. Class variables are defined in the class and outside the function body. Class variables are not normally used as instance variables.
- Data member: class variables or instance variables are used to process data related to classes and their instance objects.
- Method override: if a method inherited from a parent class cannot meet the needs of a child class, it can be overridden. This process is called method override, also known as method override.
- Local variable: the variable defined in the method, which only acts on the class of the current instance.
- Instance variable: in the class declaration, attributes are represented by variables, which are called instance variables. Instance variables are variables decorated with self.
- Inheritance: that is, a derived class inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is such a design: an object of Dog type is derived from the Animal class, which simulates the "is-a" relationship (for example, Dog is an Animal).
- Instantiation: create an instance of a class and a concrete object of the class.
- Object: an instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
Compared with other programming languages, Python adds a class mechanism without adding new syntax and semantics.
Classes in Python provide all the basic functions of object-oriented programming: the inheritance mechanism of classes allows multiple base classes, derived classes can override any method in the base class, and methods with the same name in the base class can be called.
Objects can contain any number and type of data.
1. packaging
Select cars and buses as objects, use classes to implement their attributes and methods (vehicle weight attributes, driving methods), and print attributes
# Select cars and buses as objects, use classes to implement their attributes and methods (vehicle weight attributes, driving methods), and print attributes class car: # Define basic attributes name = '' wight = 0 # Define construction method def __init__(self, n, w): self.name = n self.wight = w # Driving method def drive(self): print("The car is on the road") # Print properties def print(self): print("Weight is%d Ton%s" % (self.wight, self.name)) # Instanced object smallCar = car('a car', 1) smallCar.print() smallCar.drive() truck = car('big truck', 4) truck.print() truck.drive()
Operation results:
2. succession
Inherit the attributes and methods of the bus, transform it into a bus, input the name as "Dongfeng Road No. 28 bus", and print it (also including attributes and driving methods)
# Rewrite the first question: inherit the attributes and methods of passenger cars, transform them into buses, input the name as "Dongfeng Road No. 28 bus", and print them (also including attributes and driving methods) from first.Class import car class bus(car): def __init__(self, name, wight): super().__init__(name, wight) if __name__ == '__main__': # Instanced object bus1 = bus("Dongfeng Road No. 28 bus", 2) # Call the method of the parent class bus1.print() bus1.drive()
Operation results:
3. polymorphic
Use polymorphism to realize the driving method of 28, 906 and B32 buses and print them
# Use polymorphism to realize the driving method of bus No. 28, 906 and B32 and print it from first.Class import car # Realizing the driving method of No. 28 bus class bus28(car): def __init__(self, name, wight): super().__init__(name, wight) def driveCar(self): print("{}Driving on the road".format(self.name)) # Driving method of bus 906 class bus906(car): def __init__(self, name, wight): super().__init__(name, wight) def driveCar(self): print("{}Driving on the road".format(self.name)) # Driving method of bus B32 class busB32(car): def __init__(self, name, wight): super().__init__(name, wight) def driveCar(self): print("{}Driving on the road".format(self.name)) if __name__ == '__main__': # Instanced object bus28 = bus28("Dongfeng Road No. 28 bus", 2) # Call the method of the parent class bus28.driveCar() bus906 = bus906("Dongfeng Road No. 906 bus", 2) bus906.driveCar() busB32 = busB32("Dongfeng Road B32 Bus No", 2) busB32.driveCar()
Operation results:
Relevant learning websites: Python3 object-oriented | rookie tutorial (runoob.com)