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

Introduction notes of Python classes and objects (basic concept literacy, three characteristics of object orientation, polymorphism and duck type, with demo)

編輯:Python

List of articles

      • 0 Classes and objects
        • Basic introduction
        • objects creating
        • Dynamic binding properties and dynamic binding methods
      • 1 Object-oriented thinking
        • 1.1 encapsulation
        • 1.2 Inherit
        • 1.3 polymorphic
      • 5 Some small knowledge points
        • 5.1 The difference between method and function

0 Classes and objects

Basic introduction

  • Class properties

  • Instance attributes

class Car:
number=4 # The number of passengers 
def __init__(self,name):
self.name=name
car1=Car('audi rs4')
print(car1.name)
print(car1.number)
Car.number=7 # Modify class properties 
print(car1.number)
>>>audi rs4
>>>4
>>>7
  • Example method
def eat(self):
print(' The students are eating ...')
  • Static methods
@staticmethod
def sm(): # No, self
print(' Static methods ')
# call 
Class name .method()
  • Class method
@classmethod
def cm(cls): # Pass in cls
print(' Class method ')
# call 
Class name .method()
  • Instance attributes
def __init___(self,name):
self.name=name #self.name Called instance properties 

objects creating

  • There are two ways to implement the instance method
# Object name . Method name ()
# Class name . Method name ( Object name )
class Car:
number=4 # The number of passengers 
def __init__(self,name):
self.name=name
def show(self): # To call instance properties , Remember to introduce self
print(' The car model is '+self.name)
car1=Car('audi rs4')
car1.show()
Car.show(car1)
>>> The car model is audi rs4
>>> The car model is audi rs4
  • How class properties are used

Dynamic binding properties and dynamic binding methods

# Dynamic binding method 
class Car:
number=4 # The number of passengers 
def __init__(self,name):
self.name=name
def show(self):
print(' The car model is '+self.name)
def wash():
print(' Cleaning completed ')
car1=Car('audi rs4')
car1.wash=wash
car1.wash()
>>> Cleaning completed

1 Object-oriented thinking

What are the functions of the three features

1.1 encapsulation

effect : Improve the security of the program

  • Attributes and methods are encapsulated in class objects
  • Introduction to private properties

1.2 Inherit

  • inherited demo
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(' full name :{0}, Age :{1}'.format(self.name,self.age))
class Student(Person):
def __init__(self,name,age,sex):
super().__init__(name,age)
self.sex=sex
stu=Student('wang',12,'male')
stu.info()
>>> full name :wang, Age :12

super After a ()

  • Multiple inheritance demo

  • Method rewriting

Subclasses can rewrite a property or method inherited from the parent class , When writing, you can also use super() call .

class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(' full name :{0}, Age :{1}'.format(self.name,self.age))
class Student(Person):
def __init__(self,name,age,sex):
super().__init__(name,age)
self.sex=sex
def info(self):
super().info()
print(' Gender :{0}'.format(self.sex))
stu=Student('wang',12,'male')
stu.info()
  • object class

1.3 polymorphic

polymorphic 、 polymorphism 、 The duck type

  • polymorphism : Multiple implementations of an interface

python The understanding of polymorphism in - Yu Wenzhe's article - You know https://zhuanlan.zhihu.com/p/88402677

  • The duck type :

The name of this concept comes from James Whitcomb Riley Proposed duck test ,“ Duck test ” It can be expressed in this way :“ When you see a bird walk like a duck 、 Swimming like a duck 、 It also sounds like a duck , Then this bird can be called a duck .”

class Duck():
def walk(self):
print('I walk like a duck')
def swim(self):
print('i swim like a duck')
class Person():
def walk(self):
print('this one walk like a duck')
def swim(self):
print('this man swim like a duck')

It's obvious ,Person Class has and Duck Class , When there is a function call Duck class , And used two methods walk() and swim(). We passed Person Classes can also run , The function does not check if the object's type is Duck, As long as he has walk() and swim() Method , Can be called correctly .

5 Some small knowledge points

5.1 The difference between method and function

  1. Not bound to classes or instances function They all belong to functions (function)
  2. Bound to classes and instances function They all belong to methods (method)

To be continued !


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