How to use classes and methods in Python
Apr 21, 2023 pm 02:28 PMConcepts and examples of classes and methods
Class (Class): Used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes.
Method: Function defined in the class.
Constructor method of class __init__(): The class has a special method (constructor) named init(), which is used when the class is instantiated will be called automatically.
Instance variable: In the declaration of a class, attributes are represented by variables. This type of variable is called an instance variable. An instance variable is a variable using self Modified variables.
Instantiation: Create an instance of a class, a specific object of the class.
Inheritance: That is, a derived class inherits the fields and methods of the base class. Inheritance also allows an object of a derived class to be treated as a base class object. 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).
1. python class: class
Python’s class (class) is equivalent to a family composed of multiple functions. If there is a person named Myclass in this big family f, if this f has the function of printing the weather, then if one day I need this f to print today's weather, then I must call his full name MyClass.f before he can print it to me, that is, after calling his Time needs to bring his family name his name.
Attributes: Attributes are variables in this class. If the variable is an item, then the different attributes are different items in this big family
Method: The method is the function in this class. If functions are people, then different methods are different people in this big family.
MyClass instance:
1 2 3 4 5 6 7 8 9 10 11 |
|
Output result:
2. Class construction method__init__()
If init() is also a person, but he is the liaison between the family and the outside world, when people from the outside want to call people from their own family, You must tell him first, so as long as a family member is called, init() will be executed first, and then he will tell the called person to execute the called one.
init() example:
1 2 3 4 5 6 |
|
Output result:
self
Inside the class, use the def keyword to define a method. Different from general function definitions, class methods must contain the parameter self, and it is the first one. Parameters, self represents an instance of the class. self: There is only one special difference between class methods and ordinary functions - there must be an additional first parameter name. By convention it The name is self.
Instance of class: It is to apply the class in the instance scenario. For example, there is a function in the class f. If this f has print at a certain time The ability of weather conditions, then if I need this f to print the weather at 12 o'clock today, then the action of letting him print the weather at 12 o'clock today is the instantiation of the class, making the capabilities of the functions in the class real. Actions.
Instantiated instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Output result:
1 |
|
BaseClassName (the base class name in the example) must be defined in the same scope as the derived class. In addition to classes, you can also use expressions, which is useful when the base class is defined in another module.
- Python also supports
Multiple inheritance, that is, you can inherit multiple parent classes. The inheritance method is the same as the single inheritance method, and the method is as follows:
1 |
|
Inheritance example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Running result:
5.方法重写
如果你的父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法。即如果B家族继承了A家族,但是B家族有个佣人只会扫地,于是A家族给这个人洗脑,让他啥都不会,然后再教这个佣人洗碗、擦桌子的技能,那么这个佣人就只会洗碗和擦桌子了。
super()函数是用于调用父类(超类)的一个方法。
方法重写实例:
1 2 3 4 5 6 7 8 9 10 11 |
|
输出结果:
类的特殊属性与方法
类的私有属性
_private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。
私有属性实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
输出结果:
类的私有方法
__private_method:两个下划线开头,声明该方法为私有方法,只能在类的内部调用 ,不能在类的外部调用。self.__private_methods。
私有方法实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
输出结果:
The above is the detailed content of How to use classes and methods in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

A complete guide to golang function debugging and analysis

Share several .NET open source AI and LLM related project frameworks
