Object-oriented base (III)

# Object-oriented inheritance main role is 
# optimize the code, save the code. 
# Increase the reusability of code. 
# Improve the maintainability of the code. 
# Make relations between classes.

# 例:
class Parent:
    def __init__(self):
        self.func()

    def func(self):
        print('Parent')

class Son(Parent):
    def __init__(self):
        self.func()

    def func(self):
        print('Son')

SON1 = Son ()
 # Result: Son, use priority inheritance derived classes

class A:
    lst = []


p1 = A()
p2 = A()
p1.lst.append(1)

print(p1.lst,p2.lst,A.lst)

# Result: [. 1] [. 1] [. 1], a list of data types is a variable, here the reference function are the same default value of a memory address


class A:
    name = 'Tian'


p1 = A()
p2 = A()
p1.name = ' WUSER '

print(p1.name,p2.name,A.name)

# The Result: WUSER Tian Tian str type is a hash of the object can only access the class static field can not be modified
# Python is not the concept of interfaces 
# interface class, abstract class: the development of a specification. 
# Normalized design

class Alipay:
    def __init__(self,money):
        self.money = money

    DEF Pay (Self):
         Print ( ' pay S% ' % self.money)


class Jdpay:
    def __init__(self,money):
        self.money = money

    DEF Pay (Self):
         Print ( ' pay S% ' % self.money)


Impurity = Alipy (100 )
jpay = Jdpay(200)

apay.pay()
jpay.pay()
# Payment is a unified concept, obviously it can not be unified Here, how to achieve that unity

def pay(obj):
    obj.pay()
pay (pay)

# This is not a unified look, this is the normalized design abstract class also draws on the concept
from abc import ABCMeta,abstractmethod

class Payment (= the metaclass that ABCMeta):
         # decorator develop a specification given time when the derived class will pay deletion method 
        @abstractmethod
         DEF pay (Self): Pass   # developed a specification



class Alipay(Payment):
    def __init__(self,money):
        self.money = money

    DEF Pay (Self):
         Print ( ' pay S% ' % self.money)


class Jdpay(Payment):
    def __init__(self,money):
        self.money = money

    # DEF Pay (Self): 
    #      Print ( 'pay S%'% self.money) 

Apay = Alipay (100 )
jpay = Jdpay(200)

# result:TypeError: Can't instantiate abstract class Jdpay with abstract methods pay
# Python is no concept of polymorphism, but he ducks types

# class Str:
#     def index(self):
#         pass

# class List:
#     def index(self):
#         pass
#
# class Tuple:
#     def index(self):
#         pass

# When the three methods in the same class we call duck type
# Generalized package: instantiate an object, the object space of the package to the attributes. 
# Narrow package: private ownership. 
# Private members: private static fields, private methods, private object properties

class A:
     # This is a Class A private static fields only be accessed when the internal A category called, whether external or instance of the derived class can not access 
    __money = 1000

a1 = A()

# Print (a1 .__ Money) 
# Print (A .__ Money) 
# error, there is a bug because python design time will be preceded by a private field in the class name 
Print (a1._A__money)
 # This is a python design problem that no one would be so use

class Parent:
    def __func(self):
        print('in Parent func')

    def __init__(self):
        self.__func()

class Son(Parent):
    def __func(self):
        print('in Son func')

son1 = Son()

# result:in Parent func
class Person:
    def __init__(self,name,sex,age):
        self.__name = name
        self.__sex = sex
        self.age = age

    def func(self):
        print(self.__name)


p1 = Person(1,2,3)

# Print (p1 .__ name) error 
p1.func ()
 # He adds _Person in front of the class when performed internally __name

 

Guess you like

Origin www.cnblogs.com/tengx/p/11865525.html