August 8, 2019 (object-oriented programming Advanced 2)

1, static methods and class methods
   "" "
  If you need non-existence of such a class member, you can use the static method @staticmethod called
   " ""
   # Static methods and class methods are invoked by a message to the class of
  # Can give a message to call the object class method to be passed through, but the received message object as a parameter
class A(object):
    @staticmethod
    def a():
        print('a')
a1 = A()
a1.a()
 
a

Case 1:

# Input four sides, it is determined whether a square

# If yes: calculating the area
class zhengfangxing(object):
    def __init__(self,a,b,c,d):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
    @staticmethod
    def is_valid(a,b,c,d):
        for i in [b,c,d]:
            if i != a:
                return False
        else:
            return True
    def area(self):
            area_ = self.a * self.b
            return Area_
 DEF Amin ():           
    zhengfangxing = zhengfangxing (2,2,2,2 ) 
    RES = zhengfangxing.is_valid (2,2,2,2 )
     IF RES == True:
         Print (zhengfangxing.area ()) 
Amin ()

 """

from time import time,localtime,sleep

# Timestamp, from January 1970 No. 1 00.00.00 seconds to now experienced sec speed.
print (localtime (time ()) )
 """
2, inheritance, and polymorphism
class A (Object):
     DEF  the __init__ (Self): class A (Object):
     DEF  the __init__ (Self): 
        self.a = 100
     DEF A1 (Self):
         Print ( ' A1 ' )
 # class name written on inheritance the type. 
class B (A):
     DEF  the __init__ (Self): 
        A. the __init__ (Self) 
        self.b, = 200 is
         # self.b1 B1 = 
    DEF Bl (Self):
         Print ( ' Bl '  )
B_= B ()
 Print (b_.a) 
b_.A1 () self.a = 100
     DEF A1 (Self):
         Print ( ' A1 ' )
 # inherited class name written in the classes. 
class B (A):
     DEF  the __init__ (Self): 
        A. the __init__ (Self) 
        self.b, = 200 is
         # self.b1 B1 = 
    DEF Bl (Self):
         Print ( ' Bl ' ) 
B_ = B ()
 Print (B_. A) 
b_.A1 ()
 
 100 
A1

Case 2:

# Create a parent class, creating a subclass
# Parent and two numbers denoted SUM_
# Subclasses Print this SUM_
class A(object):
    def __init__(self):
        self.a = 100 self.a1 = 200 def sum_(self): SUM_ = self.a + self.a1 return SUM_ class B(A): def __init__(self): A.__init__(self) def Print(self): res = self.sum_() print(res) b = B() b.Print() 300
class A(object):
    def __init__(self,a1):
        self.a = 100
        self.a1 = a1
    def sum_(self):
        SUM_ = self.a + self.a1
        return SUM_
class B(A):
    def __init__(self,a1,b1):
        A.__init__(self,a1)#supper(A,self),_init(a1)
    def Print(self):
        res = self.sum_()
        print(res)
b = B(1000,'100')
b.Print()

1100

 

# Class method can change the properties of the class

class A(object):
    def __init__(self):
        self.joker = 100
    @classmethod
    def a(cls):
        return cls()
joker = A.a()
print(joker)
joker2 = A()
joker2.joker_1 = 1000
print(joker2)


<__main__.A object at 0x000001FC2D822E10>
<__main__.A object at 0x000001FC2D822F28>

Case 3:

# 3 with functional class encapsulates
wxpy: Play micro-channel with Python
1, auto-reply text and pictures to specific friends
2, and the sex ratio of the number ratio of the total number of micro letter a statistical package, the ratio of male, female, unknown gender
3, the statistics your friends belong to which province and draw a histogram
 
 

 

 
 
3, a list of formula
1) decoupling
test:
def foo():
    l = []
    def bar(i):
        l.append(i)
        return l
    return bar
f1 = foo()
res1 = f1(1)
print(res1)
res2 = f1(2)
print(res2)
  test1:
ef deco(func):
    def warp(H):
        print('Joker')
        return func(H)
    return warp
@deco
def Joker(name):
    print('hello %s'%name)
Joker ( 'huwang')
 
2) decorator
def Joker(func):
​    def warp(n1,n2,n3):
num = n1 + n2
​        return func(0,num,n3)
​    return warp
* The first two digits decorator sum, itself a function of the third parameter multiplied by this and *
@Joker
def SUM(num1,num2,num3):
​    print(num1,num2,num3)
​    print(num2 * num3)
 
SUM(10,2,3)
 
3) a list of formula
a = (x for x in range(100000000000) if x % 2== 0)
for i in range(100):
    print(next(a))
Advantages: fast calculation, as have all-time loaded into memory, the data volume is not too large for the case 10000-2000-
disadvantages: memory for
 
4) Generator
a = (x for x in range (100000000000) if x% 2 == 0)
advantages: saving memory space
disadvantages: slow calculation, as to be generated.
 
 5) function closure
Case 4:
# Create a decorator, three functions (two parameters)
# decorator and treatment of these two parameters, and print
# Each print function of these two parameters
def Joker(func):
    def warp(num1,num2):
        print('num1+num2=',num1 + num2)
        return func(num1,num2)
    return warp
@Joker
def SUM(num1,num2):
    print(num1,num2)
SUM(100,200)


num1+num2= 300
100 200
DEF Joker (FUNC):
     DEF Warp (N1, N2, N3): 
        NUM = N1 + N2
         return FUNC (0, NUM, N3)
     return Warp
 # decorator summing the first two digits, third parameter function itself and multiply this 
@Joker
 DEF the SUM (num1, num2, num3):
     Print (num1, num2, num3)
     Print (num2 * num3) 
the SUM ( 10,2,3 ) 


0 12 is. 3 
36

 

Guess you like

Origin www.cnblogs.com/wangying317/p/11322768.html