current position:Home>Python notes - day16 - Methods and inheritance

Python notes - day16 - Methods and inheritance

2022-05-15 06:04:00Wandering mage 12

Preface
python Grammar learning , Leave it to those who need it , Understand everything !!

# coding=utf8
# @time:2022/4/26 20:49
# Author  Haoyu 

#  One 、 Method 
#  The methods in the class are divided into : Object methods 、 Class method 、 Static methods 
'''''''''
1. Object methods 
 How to define : Functions defined directly in the class 
 How to call : Call... With objects  ->  object . Object methods ()
 characteristic : With parameters self,self There is no need to pass parameters when calling , Point to current object ( Who calls to whom )
 When to use : If you need to use object attributes to realize the function of the function , Define this function as an object method 

2. Class method 
 How to define : In class , Add... Before defining the function @#classmethod Decorator 
 How to call : Use class to call  ->  Class name . Class method ()
 characteristic : With parameters cls,cls There is no need to pass parameters when calling , Point to the current class ( Who calls to whom )
 When to use : To realize the function of the function, the class is required on the premise of no object attribute ( Class properties ), Just use class methods 


3. Static methods 
 How to define : In class , Add a decorator before defining a function @staticmethod
 How to call : Use class to call   ->   Class name . Static methods ()
 characteristic : There are no default parameters , Equivalent to ordinary functions defined in a class 
 When to use : No object is required to implement the function of the function ( Object properties ) You don't need a class ( Class properties )
'''''''''


'''''''''
class Demo:
    def func1(self):
        pass

    @classmethod
    def func2(cls):
        pass

    @staticmethod
    def func3():
        pass

#  Call object method 
d = Demo
d.func1()

#  Calling class methods 
Demo.func2()

#  Call static methods 
Demo.func3()
'''''''''


#  practice : Realize the function of landlords' licensing with object-oriented ?


#  Two 、 Inherit 
# 1. What is inheritance 
'''''''''
 The process of letting subclasses directly own the properties and methods of the parent class is inheritance 

 Subclass : A successor 
 Parent class : Inherited 
'''''''''

# 2. How to inherit 
'''''''''
 grammar :
class  Class name ( Parent list )
     The content of the class 
    
 explain :
 Parent list     -    Parent class 1, Parent class 2, Parent class 3.....

 Be careful :
 If the inheritance relationship is not written when defining a class , Then this class inherits from by default object class 
class Person: #  amount to class Person(object):
'''''''''

# 3. Inherited those things 
#  All inherited  

'''''''''
#  give an example 
class A:
    x = 100

    def __init__(self):
        self.y = 1
        self.z = 2

    def func1(self):
        print(self.x,self.y)

    @classmethod
    def func2(cls):
        print(' Class method ')

    @staticmethod
    def func3():
        print(' Static methods ')

class B(A):
    pass


b = B()
print(b.z,b.y)
'''''''''

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424040.html

Random recommended