程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Advanced Python Programming - object oriented 2

編輯:Python

Catalog

  • 1. Inherit : seeing the name of a thing one thinks of its function Here you go Accept according to law
    • 1.1 Multiple inheritance
  • 2. polymorphic : Is not obvious There is no need to inherit
  • 3. All kinds of methods
    • 3.1 Example method
    • 3.2 Class method
    • 3.3 Static methods


1. Inherit : seeing the name of a thing one thinks of its function Here you go Accept according to law

# Three characteristics of object oriented : encapsulation 、 Inherit 、 polymorphic 
# Encapsulated in a class Use it every time Instantiation Object name = class () Method attribute 
# In life : Children inherit their parents' genes Children inherit their parents' property 
# Object oriented : Subclasses inherit the properties and methods of the parent class son Father is rich 
# Single inheritance Multiple inheritance 
# effect : Achieve code reuse , The same code does not need to be written repeatedly 
# Two classes 
# Animal species : eat sleep 
# Dogs : eat sleep run It's called 
# encapsulation Write Animal species Dogs Animal species Eat, sleep, run and cry 
class Animal:
def eat(self):
print(' Animals have ways to eat ')
def sleep(self):
print(' Animals have ways to sleep ')
# Don't write about eating and sleeping Inherit 
class Dog(Animal):
def run(self):
print(' Animals have ways to run ')
def bark(self):
print(' Animals have ways to bark ')
# Sure Normal logic 
ergou=Dog()
ergou.eat()
ergou.sleep()
ergou.run()
ergou.bark()
# Inheritance can directly enjoy the methods of the parent class , No need to redevelop 
# Subclasses according to their own needs , Encapsulate your own methods 
# Expand terminology 
# Dog Class is Animal Class Subclass Animal Class is Dog The parent of a class Dog Class from Animal class Inherit 
# Dog Class is Animal A derived class of the Animal Class is Dog Class base class Dog Class from Animal Class 
# Can there be any subclasses under dogs ? There can be subclasses 
# Inheritance can continue 
# A-B-C-D d It has the characteristics of all parent classes Single inheritance 
class Animal:
def eat(self):
print(' Animals have ways to eat ')
def sleep(self):
print(' Animals have ways to sleep ')
# Don't write about eating and sleeping Inherit 
class Dog(Animal):
def run(self):
print(' Animals have ways to run ')
def bark(self):
print(' Animals have ways to bark ')
class GodDog(Dog):
def fly(self):
print(' Xiao Tian dog can fly ')
# The howler dog has all the characteristics of its parent 
xtq = GodDog()
xtq.eat()
xtq.sleep()
xtq.run()
xtq.bark()
xtq.fly()
class CommonDog(Dog):
def catch(self):
print(' Can catch mice ')
# problem : Define an ordinary dog to inherit dogs What methods do ordinary dogs have ?
# Besides being able to fly 
# Why can't you fly ? No inheritance Dog class 2 A son 1 A son Tiangou 1 A son Ordinary dogs 
pt = CommonDog()
pt.run()
pt.sleep()

1.1 Multiple inheritance

# Can you have two dads ? Multiple inheritance There is a father There is a mother 
# You can inherit more , A subclass can have more than one parent , And it has all the properties and methods of the parent class 
# Multiple inheritance Class name ( Parent class , Parent class )
class A:
def demoA(self):
print(' This is the parent class A Methods ')
class B:
def demoB(self):
print(' This is the parent class A Methods ')
class C(A,B):
def demoC(self):
print(' This is the parent class A Methods ')
c=C()
c.demoA()
c.demoB()
c.demoC()
# Multiple inheritance will not be used in the project 
# If you use multiple inheritance Which will I focus on 
# Your father can sew clothes , Your mother can also sew clothes Want to inherit sewing clothes First inherit your father's skill in sewing clothes Or your mother 
# I want to learn cake technology now , One is to worship others as teachers , Learn others' ancestral secret recipe 
# Another one Go to training school to train cake skills 
class Master:
def __init__(self):
self.peifang = ' Master's cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology ')
class School:
def __init__(self):
self.peifang = ' School cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology , Learning cake ')
class Tudi(Master,School):
pass
# Which formula is the main one The master Nearby 
xiaxia = Tudi()
xiaxia.make_cake()
# Conclusion : A class inherits multiple parent classes , Will inherit the properties and methods of the first class first 
# Over time , Apprentice cake learned . I invented a cake technology . Apprentice, he also has a way to make cakes 
class Master:
def __init__(self):
self.peifang = ' Master's cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology ')
class School:
def __init__(self):
self.peifang = ' School cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology , Learning cake ')
class Tudi(Master,School):
def __init__(self):
self.peifang=' Own cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology , Learning cake ')
xiaxia = Tudi()
xiaxia.make_cake()
# Conclusion : Subclasses and superclasses have methods and properties with the same name , Will override the methods and properties of the parent class 
# The plot continues to develop , After the apprentice learned it , I opened a shop myself , Combined with three flavors 
# Take out the master's formula , Take out the school formula , Take out the recipe you created Sorting out 3 A taste 
# super() Points to the parent class Get the properties and methods of the parent class 
class Master:
def __init__(self):
self.peifang = ' Master's cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology ')
class School(Master):
def __init__(self):
self.peifang = ' School cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology , Learning cake ')
super().__init__()
super().make_cake()
class Tudi(School):
def __init__(self):
self.peifang=' Own cake recipe '
def make_cake(self):
print(f' Use {
self.peifang} technology , Learning cake ')
super().__init__()
super().make_cake()
xiaxia = Tudi()
xiaxia.make_cake()

2. polymorphic : Is not obvious There is no need to inherit

# It refers to the same kind of things , There are different forms 
# Pass in different objects , It produces different results Animals eat The food is different 
# Pigs eat , Cats eat cat food , Dogs eat dog food , Animals have ways to eat , Simply create a way to eat 
# Create a good object and use the method of eating Pigs eat Cats eat Dogs eat How to eat Different forms 
class Pig:
def eat(self):
print(' Pigs eat ')
class Cat:
def eat(self):
print(' Cats eat cat food ')
class Dog:
def eat(self):
print(' Dogs eat dog food ')
# Pass in different objects 
def eat(a):
a.eat()
p = Pig()
c = Cat()
d = Dog()
eat(p)
eat(c)
eat(d)
# You can pass a class object into the method 
# Call the same method name of different class objects , It produces different results 
class Dog:
def __init__(self,name):
self.name = name
def work(self):
print(' Guard the house ')
class BigDog(Dog):
def work(self):
print(' Catch the thief ')
class SmartDog(Dog):
def work(self):
print(' Anti drug ')
class Person:
def with_dog(self,obj):
print(' Police officers and %s go together --'%(obj.name))
obj.work()
xiaoqi = BigDog(' Dog seven ')
erha = SmartDog(' Two ha ')
p = Person()
p.with_dog(xiaoqi)
# Call the same method name of different class objects , It produces different results 

3. All kinds of methods

# Methods can be passed into objects 
# Class method : Example method Static methods 
# Example method : Functions in the class have self
# Class method : Need to use @classmethod decorate , Parameter named cls
# Static methods : Need to use @staticmethod decorate , The parameters are not self either cls Parameters 
class Stu:
school = ' Code measurement class '
def __init__(self,name,age):
self.name = name
self.age = age
# Example method 
def play_ball(self):
print(f'{
self.name} To play basketball ')
@classmethod
def sport(cls):
print(f'{
cls.school} All my classmates like playing badminton ')
# There is no class related content inside the static method , You can position this function as a static method 
# You can define static methods without objects . Method name . It can be classified as . Method name 
# Static method requirements , You want to define your ordinary methods as static methods , Ask your method , Don't refer to external things 
@staticmethod
def clean():
print(' cleaning ')

3.1 Example method

# Object name . Method name () class . Method name ( Object name )
stu1 = Stu('cxk',18)
stu1.play_ball()
Stu.play_ball(stu1)

3.2 Class method

# Class method : Object name . Method name () class . Method name 
# A method that your entire class can have , It can be designed as a class method 
stu1 = Stu('cxk',18)
stu1.sport()
Stu.sport()

3.3 Static methods

# Will not access any properties or other methods of the class , It can be designed as a static method 
Stu.clean()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved