Detailed explanation of how to define and call classes in python

伊谢尔伦
Release: 2017-05-22 23:21:02
Original
2795 people have browsed it

Definition of class methods

1.def fun_name(self,...);

Pass

2. The parameter self represents an instance of the class, which is automatically provided by the system when calling the method

3. The self parameter must be specified when defining the method

Method of the class The call

is similar to the ordinary function call

1. Internal call of the class: self.(Parameter list ).

2. Call outside the class: . (parameter list).

Note: In the above two calling methods, self does not need to be included in the parameter list provided.

Demonstrate a class:

wash.py
class Washer:
 
    def init(self):
        self.water = 0
        self.scour = 0
 
    def add_water(self,water):
        print('Add water:',water)
        self.water = water
 
    def add_scour(self,scour):
        self.scour = scour
        print('Add scour:',self.scour)
 
    def start_wash(self):
        print('Start wash...')
 
if name == 'main':
    w = Washer()
    w.add_water(10)
    w.add_scour(2)
    w.start_wash()
Copy after login

The running result of the program is:

Detailed explanation of how to define and call classes in python

Modify the program as shown in the figure:

washa.py
class Washer:
 
    def init(self):
        self.water = 10
        self.scour = 2
 
    def set_water(self,water):
        self.water = water
 
    def set_scour(self,scour):
        self.scour = scour
 
    def add_water(self):
        print('Add water:',self.water)
 
    def add_scour(self):
        print('Add scour:',self.scour)
 
    def start_wash(self):
        self.add_water()
        self.add_scour()
        print('Start wash...')
 
if name == 'main':
    w = Washer()
    w.set_water(20)
    w.set_scour(4)
    w.start_wash()
Copy after login

The running result of the program is:

Detailed explanation of how to define and call classes in python


## Methods within a class call each other

1. The internal methods of a class can be called accordingly

2. The calling method is the same as described above in the class Internal calling method

Construction method and its function

1. The construction method is the _init_() method mentioned and used in the previous course.

2. The function of the constructor is to initialize the instance when the class is instantiated.

3. The _init_() method is the function automatically called in the first step of class instantiation.

4. Note that the method name is fixed, but its parameters are the same as ordinary methods, at least with the self parameter.

5. Initializing the instance includes: defining and initializing the instance

Attributes: or calling some methods of the class.

6. The constructor can have various parameters other than self (keyword parameters, default parameters, collecting parameters with tuples, collecting keyword parameters with dictionaries, etc.); it can be achieved when instantiating a class , passing in the specified value for the corresponding attribute.

Program demonstration:

washb.py
class Washer:
 
    def init(self,water=10,scour=2):
        self.water = water
        self.scour = scour
 
    def set_water(self,water):
        self.water = water
 
    def set_scour(self,scour):
        self.scour = scour
 
    def add_water(self):
        print('Add water:',self.water)
 
    def add_scour(self):
        print('Add scour:',self.scour)
 
    def start_wash(self):
        self.add_water()
        self.add_scour()
        print('Start wash...')
 
if name == 'main':
    # w = Washer()
    # w.start_wash()
    wb = Washer(100,10)
    wb.set_water(50)
    wb.set_scour(5)
    wb.start_wash()
Copy after login
The running result of the program is:

Detailed explanation of how to define and call classes in python

[Related recommendations]

1.

Python class inheritance explanation

2.

Analyze the example code of dynamic modification of Python class

3.

Detailed explanation of python class example analysis

4.

Detailed explanation of inheritance of Python classes

5.

Introduction to python class methods and object methods

The above is the detailed content of Detailed explanation of how to define and call classes in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!