Python3 Object-Oriented | Novice Tutorial (16)

Table of contents

1. Introduction to object-oriented technology

(1) Class

(2) Method

(3) Class variables

(4) Data members

(5) Method rewriting

(6) Local variables

(7) Instance variables

(8) Inheritance

(9) Instantiation

(10) Object

2. Class definition

Three, class object

(1) A class object supports two operations: attribute reference and instantiation.

(2) Attribute references use the same standard syntax as all attribute references in Python: obj.name.

(3) After the class object is created, all names in the class namespace are valid attribute names.

(4) The class has a special method (constructor) called __init__(), which is automatically called when the class is instantiated

 (5) The class defines the __init__() method, and the instantiation operation of the class will automatically call the __init__() method.

 (6) The __init__() method can have parameters, and the parameters are passed to the instantiation operation of the class through __init__().

 (7) self represents an instance of a class, not a class

(8) self is not a python keyword, and it can be executed normally if we replace it with runoob:

Fourth, the method of the class

5. Inheritance

(1) The definition of the derived class is as follows:

(2) In addition to classes, expressions can also be used, which is very useful when the base class is defined in another module:

Six, multiple inheritance

 Seven, method rewriting

(1) If the function of your parent class method cannot meet your needs, you can override the method of your parent class in the subclass, examples are as follows:

 (2) Description of Python subclass inheriting parent class constructor

1. If you need the constructor of the parent class in the subclass, you need to explicitly call the constructor of the parent class, or do not override the constructor of the parent class.

2. If you need the constructor of the parent class in the subclass, you need to explicitly call the constructor of the parent class, or do not override the constructor of the parent class.

 3. If the subclass is instantiated when __init__ is rewritten, the __init__ already defined by the parent class will not be called. The syntax format is as follows:

 4. If you rewrite __init__, to inherit the constructor of the parent class, you can use the super keyword:

 5. There is another classic way of writing:

 8. Class properties and methods

(1) Private attributes of a class

(2) Class methods

(3) Private methods of the class

(4) Examples

1. Examples of private properties of a class are as follows:

 2. The private method instance of the class is as follows:

 9. Proprietary methods of classes

10. Operator overloading


1. Introduction to object-oriented technology

(1) Class

Used to describe a collection of objects with the same properties and methods. It defines properties and methods common to every object in the collection. Objects are instances of classes.

(2) Method

Functions defined in the class.

(3) Class variables

Class variables are common across instantiated objects. Class variables are defined within the class and outside the body of the function. Class variables are generally not used as instance variables.

(4) Data members

Class variables or instance variables are used to handle data related to a class and its instance objects.

(5) Method rewriting

If the method inherited from the parent class cannot meet the needs of the subclass, it can be rewritten. This process is called method override, also known as method rewriting.

(6) Local variables

Variables defined in methods only apply to the class of the current instance.

(7) Instance variables

In the class declaration, attributes are represented by variables, which are called instance variables, and instance variables are variables modified with self.

(8) Inheritance

That is, a derived class (derived class) inherits the fields and methods of the base class (base class). Inheritance also allows an object of a derived class to be treated as an object of a base class. For example, there is such a design: an object of type Dog is derived from the Animal class, which simulates the "is-a" relationship (for example, Dog is an Animal).

(9) Instantiation

Creates an instance of a class, a concrete object of the class.

(10) Object

An instance of a data structure defined by a class. Objects consist 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 as much as possible.

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 amount and type of data.

2. Class definition

The syntax format is as follows:

 After a class is instantiated, its properties can be used, in fact, after a class is created, its properties can be accessed by the class name.

Three, class object

(1) A class object supports two operations: attribute reference and instantiation.

(2) Attribute references use the same standard syntax as all attribute references in Python: obj.name .

(3) After the class object is created, all names in the class namespace are valid attribute names.

So if the class definition is like this:

 The above creates a new class instance and assigns the object to the local variable x, where x is an empty object.

The output of executing the above program is:

The attribute i of class MyClass is: 12345
The output of method f of class MyClass is: hello world

(4) The class has a special method ( constructor ) called __init__() , which is automatically called when the class is instantiated

Example:

def __init__(self):

         self.data = [ ]

 (5) The class defines the __init__() method, and the instantiation operation of the class will automatically call the __init__() method.

The class MyClass is instantiated as follows, and the corresponding __init__() method is called:

x = MyClass()

 (6) The __init__() method can have parameters, and the parameters are passed to the instantiation operation of the class through __init__().

Example:

 (7) self represents an instance of a class, not a class

Class methods have only one special difference from ordinary functions - they must have an additional first parameter name , which by convention is called self.

 The execution result of the above example is:

<__main__.Test instance at 0x100771878>
__main__.Test

 It is obvious from the execution result that self represents the instance of the class and represents the address of the current object, while self.class points to the class.

(8) self is not a python keyword, and it can be executed normally if we replace it with runoob:

 The execution result of the above example is:

<__main__.Test instance at 0x100771878>
__main__.Test

Fourth, the method of the class

Inside the class, use the def keyword to define a method. Different from the general function definition, the class method must contain the parameter self, which is the first parameter, and self represents the instance of the class.

Example:

 The output of executing the above program is:

runoob says: I'm 10 years old.

5. Inheritance

Python also supports inheritance of classes. If a language does not support inheritance, classes are meaningless.

(1) The definition of the derived class is as follows:

 The subclass (derived class DerivedClassName) will inherit the properties and methods of the parent class (base class BaseClassName).

BaseClassName (the base class name in the instance) must be defined in the same scope as the derived class.

(2) In addition to classes, expressions can also be used, which is very useful when the base class is defined in another module:

class DerivedClassName(modname.BaseClassName):

 Example:

 The output of executing the above program is:

ken says: I'm 10 years old and I'm in 3rd grade

Six, multiple inheritance

Python also has limited support for multiple inheritance forms. The class definition of multiple inheritance is as follows:

 Pay attention to the order of the parent class in the parentheses. If the parent class has the same method name, but it is not specified when the subclass is used, python searches from left to right, that is, when the method is not found in the subclass, it searches from left to right Whether the method is contained in the parent class.

Example:

 The output of executing the above program is:

My name is Tim, I am a speaker, and the topic of my speech is Python

 Seven, method rewriting

(1) If the function of your parent class method cannot meet your needs, you can override the method of your parent class in the subclass, examples are as follows:

 The super() function is a method used to call the parent class (super class).

The output of executing the above program is:

call subclass method
Call parent class method

 (2) Description of Python subclass inheriting parent class constructor

1. If you need the constructor of the parent class in the subclass, you need to explicitly call the constructor of the parent class, or do not override the constructor of the parent class.

2. If you need the constructor of the parent class in the subclass, you need to explicitly call the constructor of the parent class, or do not override the constructor of the parent class.

 The output is:

name: runoob
Son runoob

 3. If the subclass is instantiated when __init__ is rewritten, the __init__ already defined by the parent class will not be called. The syntax format is as follows:

 The output is:

hi
Son runoob

 4. If you rewrite __init__, to inherit the constructor of the parent class, you can use the super keyword:

super(subclass, self).__init__(parameter1, parameter2, ....)

 5. There is another classic way of writing:

Parent class name.__init__(self, parameter 1, parameter 2,...)

 

 The output is:

name: runoob
hi
Son runoob

 8. Class properties and methods

(1) Private attributes of a class

__private_attrs : At the beginning of two underscores, the attribute is declared private and cannot be used or directly accessed outside the class. When using self.__private_attrs in a method inside a class .

(2) Class methods

Inside the class, use the def keyword to define a method. Different from general function definitions, class methods must contain the parameter self, which is the first parameter, and self represents the instance of the class.

(3) Private methods of the class

__private_method : Start with two underscores, declare the method as a private method, which can only be called inside the class, not outside the class. self.__private_methods .

(4) Examples

1. Examples of private properties of a class are as follows:

 The output of executing the above program is:

1
2
2
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    print (counter.__secretCount) # report an error, the instance cannot access private variables
AttributeError: 'JustCounter' object has no attribute '__secretCount'

 2. The private method instance of the class is as follows:

 Execution results of the above example:

 9. Proprietary methods of classes

(1) __init__: Constructor, called when an object is generated

(2) __del__: Destructor, used when releasing an object

(3) __repr__ : print, convert

(4) __setitem__ : assign according to the index

(5) __getitem__: get the value according to the index

(6) __len__: Get the length

(7) __cmp__: comparison operation

(8) __call__: function call

(9) __add__: addition operation

(10) __sub__: subtraction operation

(11) __mul__: multiplication operation

(12) __truediv__: division operation

(13) __mod__: remainder operation

(14) __pow__: power

10. Operator overloading

Python also supports operator overloading. We can overload the proprietary methods of a class. Examples are as follows:

 The result of executing the above code is as follows:

Vector(7,8)

Guess you like

Origin blog.csdn.net/wuds_158/article/details/131494388