Day24_2014 days of the 24th day – object-oriented three major features, duck types, class constraints, depth analysis of Super

day24

Three Object-Oriented Features

Inheritance, encapsulation, polymorphism

Encapsulation: Put a bunch of things (code, data) in one place (space), and you can use it

class Student: def __init__(self, name, sex): self.name = name self.sex = sexliye = Student("李业", "laddy_boy")print(liye.sex)

Polymorphism: One thing It can present a variety of forms

Water: solid, liquid, gaseous

Python supports polymorphism by default. For example, variables can be of different types, and function parameters can also be passed in multiple types.

p>

duck type

python advocates duck type (programming idea) looks like a duck, it is a duck

# class A:## def login(self):# print('Login')## def register(self):# print('Register')## def func1(self):# pass## def func2(self): # pass### class B:## def login(self):# print('Login')## def register(self):# print('Register')## def func3(self):# pass# AB They are ducks each other. #赵迦: 1. 好记.# Although the two classes A and B are not related, I unify the method names of similar methods in the two classes, which is a unified standard in a certain sense. # index ,index , index,

class constraints

version 1# class QQpay:# def pay(self, money):# print( f"Use qq to pay {mon ey}")### class Alipay:# def pay(self, money):# print(f"Use Ali to pay{money}")### obj1 = QQpay()# obj1.pay(100)## obj2 = Alipay()# obj2.pay(200) version 2: To achieve a unified interface# class QQpay:# def pay(self, money):# print(f"Use qq to pay{money}")### class Alipay:# def pay(self, money):# print(f"Use Ali to pay{money}")### def pay(obj, money):# obj.pay(money)### obj1 = QQpay ()# obj2 = Alipay()## pay(obj1, 100)# pay(obj2, 200) Version 3: Improve the payment function# class QQpay:# def pay(self, money):# print(f"Use qq to pay了{money}")### class Alipay:# def pay(self,money):# print(f"Use Alipay{money}")### class Wechat:# def fuqian(self, money): # print(f"Use WeChat to pay {money}")### def pay(obj, money):# obj.pay(money)### obj1 = QQpay()# obj2 = Alipay()## pay( obj1, 100)# pay(obj2, 200)## obj3 = Wechat()# obj3.fuqian(300) Version 4: Customized constraints, common name, not fully compulsory # class Payment:# def pay(self, money ):# pass### class QQpay(Payment):# def pay(self, money):# print(f"Use qq to pay{money}")### class Alipay(P ayment):# def pay(self, money):# print(f"Use Ali to pay {money}")### class Wechat(Payment):# def fuqian(self, money):# print(f"Use WeChat paid {money}")### def pay(obj, money):# obj.pay(money)## obj1 = QQpay()# obj2 = Alipay()# obj3 = Wechat()## pay(obj1 , 100)# pay(obj2, 200)# pay(obj3, 300) Version 5: Do Mandatory Constraint# Method 1: A constraint method that Python language is accustomed to using, and actively throw errors in the parent class # Method 2: Borrowing from the Java language, define the concept of abstract classes to achieve real mandatory constraints. # Method One# The premise is that your project is already online, and the previously completed QQpay, Alipay and pay functions are all formed. # If you add a new one at this time Wechat payment, when other py files refer to payment functions, they still directly refer to pay# class Payment:# def pay(self, money):# raise Exception("Your subclass needs to define the pay method")### class QQpay(Payment ):# def pay(self, money):# print(f"Use qq to pay {money}")### class Alipay(Payment):# def pay(self, money):# print(f"Use Ali {Money}")### class Wechat(Payment):# def fuqian(self, money):# print(f"Use WeChat to pay{money}")## # def pay(self, money): # # print(f"Use WeChat to pay {money}")## def pay(self, money):# self.pay(money)## qq = QQpay()# aa = Alipay()# ww = Wechat( )## pay(qq, 100)# pay(a a, 100)# pay(ww, 100)# Method Two# from abc import ABCMeta,abstractclassmethod### class Payment(metaclass=ABCMeta):# # def pay(self, money):# # raise Exception("error" )## @abstractclassmethod# def pay(self, money):# pass### class QQpay(Payment):# def pay(self, money):# print(f"use qqpayment{money}")## # class Alipay(Payment):# def pay(self, money):# print(f"Use Ali paid {money}")### class Wechat(Payment):# def fuqian(self, money):# print (f"Use WeChat to pay{money}")## # def pay(self, money):# # print(f"Use WeChat to pay{money}")### def pay(self, money):# self.pay(money)### qq = QQpay()# aa = Alipay()# ww = Wechat()## pay(qq, 100)# pay(aa, 100)# pay(ww, 100) 

super's in-depth analysis

super (class, self) strictly follows the order in which the object belongs to the class mro, and executes the next class

< pre class="python">题一# class A:# def f1(self):# print("in A f1")## def f2(self):# print("in A f2")### class Foo(A):# def f1(self):# # Execute the next class of the Foo class in the order in which the self object belongs to the mro of the class# super(Foo, self).f2()# print("in A Foo")### obj = Foo()# obj.f1()# in A f2# in A Foo题二# class A:# def f1 (self):# print("in A")## class Foo(A):# def f1(self):# super(Foo, self).f1()# print("in Foo")### class Bar(A):# def f1(self):# print("in Bar")### class Info(Foo, Bar):# def f1(self):# super(Info, self).f1()# print("in Info f1")### print(Info.mro())# # Info Foo Bar A# obj = Info()# obj.f1()# in Bar# in Foo# in Info f1 question three class A : def f1(self): print("in A")class Foo(A): def f1(self): super().f1() print("in Foo")class Bar(A): def f1(self ): print("in Bar")class Info(Foo, Bar): def f1(self): super(Foo, self).f1() print("in Info f1")obj = Info()obj.f1( )# in Bar# in Info f1

print with color

Fixed head and tail: \033[ \ 033[0m

1;35;0m Specific adjustment parameters

Display mode: 0 (default value), 1 (Highlight), 22 (not bold), 4 (underline), 24 (not underline), 5 (flashing), 25 (non-flashing), 7 (reverse display), 27 (non-reverse display)
Foreground color: 30 (black), 31 (red), 32 (green), 33 (yellow) , 34 (blue), 35 (magenta), 36 (cyan), 37 (white)
Background color: 40 (black), 41 (red), 42 (green), 43 (Yellow), 44 (blue), 45 (magenta), 46 (cyan), 47 (white)

Some functions are invalid

# print(' Font color changes, but no background color')# Fixed header and end: \033[ \033[0m# 1;35;0m Specific adjustment parameters# print('\033[1;32;0m Font color changes, but no background Color\033[0m') # There is highlighting or print('\033[1;35m font is colored, but no background color\033[0m')# print('\033[1;33;0m font changes color, but No background color\033[0m') # There is highlighting or print('\033[1;35m font is colored, but no background color\033[0m')# print('\033[1;45m font does not change color, There is a background color\033[0m') # There is a highlight# print('\033[1;35;46m The font is colored, and there is a background color\033[0m') # There is a highlight print('\033[0; 35;0m font is colored with background color\033[0m') print('\033[1;35;0m font is colored with background color\033[0m')print('\033[4;35; 0m font is colored with background color\033[0m')# print('\033[5;35;0m font is colored with background color\033[0m') # no highlight

Today’s summary

Polymorphism, encapsulation, duck type interview questions

Class constraints: a development idea to follow when writing code

Abstract class, interface class: interview questions

super: development, interviews may be involved

Leave a Comment

Your email address will not be published.