Basic concepts
Object-oriented Universal Object Oriented Programming abbreviation OOP is a kind of programming idea. is to use the object as a basic unit of a program, to encapsulate the data and functions inside, to achieve good reusability, flexibility and extensibility.
Basic concepts:
Object-oriented is an abstraction with two basic concepts: classes and objects.
- A class is an abstract feature that defines a thing,
- The object is an instance of the class.
Example: A programmer is a class, and a specific person, such as me, is an object.
Basic elements:
Properties and methods. Take the programmer as an example. The following properties and features are specific to the programmer:
- Properties: Age, gender and height.
- Function (method): Write code, repair computer.
Inherited:
One of the main benefits of object-oriented programming is the reuse of code, and one way to implement this reuse is through inheritance mechanisms. Inheritance can be fully understood as a type and subtype relationship between classes.
Polymorphic:
A class inherits several different classes, and when the same method is called, it reacts differently.
Defining classes and creating instances
In Python , the class is class defined by the keyword. PersonFor example, define a person class as follows: According to Python the programming habit, the class name begins with an uppercase letter, followed by ( object ), indicating which class the class inherits from. With the Person definition of a class, you can create specific xiaoming , and xiaohong so on instances. Create an instance using the 类名+() form of a function call like:
Example
in [1 ]: person (object ): ...: pass ...: in [2< /span>]: Student = person () in [3 ]: Teacher = Person () in [4 ]: print (student) < __main __. Person object at 0x00000271be5ea0f0 > in [5 ]: print (teacher) < __main__. Person object at 0X00000271BE5EABE0 > in [6 ]: print (Student == Teacher) false In [7 ]:
Python object-oriented-basic