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

Object oriented programming in python (class programming) from simple to complex sample code

編輯:Python

About... In the code in this article self The meaning of , You can refer to the following blog post :
https://blog.csdn.net/wenhao_ir/article/details/125384347

Catalog

  • 01- The simplest Python object-oriented programming ( Class programming ) Sample code
  • 02- The member function in the class refers to the member variable of the class
  • 03- With constructor ( Initialization function ) The situation of
  • 04- Member function with parameters ( Excluding constructors )
  • 05- Class inheritance ( Parents and children )
  • Some suggestions related to code normalization

01- The simplest Python object-oriented programming ( Class programming ) Sample code

class Fruit:
color = 'red'
def taste(self):
return 'delicious'
apple = Fruit()
a_character = apple.color
b_character = apple.taste()
print('\na_character:{}'.format(a_character))
print('b_character:{}'.format(b_character))

The results are shown in the following figure :

02- The member function in the class refers to the member variable of the class

class Staff: # Staff For the employees 
bonus = 30000 # bonus For allowance 、 Bonus means 
def salary(self): # salary It means salary 
salary = 10000+self.bonus
return salary
zhang_san = Staff()
zhang_san_salray = zhang_san.salary()

The running results are as follows :

03- With constructor ( Initialization function ) The situation of

class Staff: # Staff For the employees 
def __init__(self, bonus): # bonus For allowance 、 Bonus means 
self.bonus = bonus
def salary(self): # salary It means salary 
salary = 10000+self.bonus
return salary
zhang_san = Staff(3000)
zhang_san_salray = zhang_san.salary()

The operation results are as follows :

04- Member function with parameters ( Excluding constructors )

class AnimalBaseClass:
def __init__(self, num):
self.animallegs = num
self.head_num = 0
def walk(self):
print(' go ')
def cry(self):
print(' It's called ')
def get_legs_num(self):
print(self.animallegs)
def input_head_num(self, num2):
self.head_num = num2
def get_head_num(self):
print(self.head_num)
animal = AnimalBaseClass(4)
animal.input_head_num(1)
animal.get_legs_num()
animal.get_head_num()

In the above code , Member functions input_head_num() With parameters .
The operation results are as follows :

05- Class inheritance ( Parents and children )

class AnimalBaseClass:
def __init__(self, num):
self.animallegs = num
def walk(self):
print(' go ')
def cry(self):
print(' It's called ')
def get_legs_num(self):
print(self.animallegs)
class BirdClass(AnimalBaseClass):
head_num = 1
def cry(self):
print(' Squeak ')
def run(self):
print(' run ')
def get_head_num(self):
print(self.head_num)
piyo_suke = BirdClass(2)
piyo_suke.cry()
piyo_suke.run()
piyo_suke.get_legs_num()
piyo_suke.get_head_num()

In the code above , class BirdClass Inherited from class AnimalBaseClass, It has classes AnimalBaseClass Variables and member functions of .
The operation results are as follows :

When both parent and child classes have member functions with the same name , When a subclass calls this member function, it will call the member function in the subclass first , This function is called rewriting ( Cover ), In English, it is called “override” So the statements in the above code

piyo_suke.cry()

The run result of is “ Squeak ”, instead of “ It's called ”.
It is not just general member functions that can be rewritten , Constructors can also be overridden in subclasses , Consider the following code :

class AnimalBaseClass:
def __init__(self, num):
self.animallegs = num
def walk(self):
print(' go ')
def cry(self):
print(' It's called ')
def get_legs_num(self):
print(self.animallegs)
class BirdClass(AnimalBaseClass):
def __init__(self, num1, num2):
self.animallegs = num1
self.head_num = num2
def cry(self):
print(' Squeak ')
def run(self):
print(' run ')
def get_head_num(self):
print(self.head_num)
piyo_suke = BirdClass(3, 1)
piyo_suke.cry()
piyo_suke.run()
piyo_suke.get_legs_num()
piyo_suke.get_head_num()

In the above code , Subclass BirdClass Constructor and superclass of AnimalBaseClass Dissimilarity .
The operation results are as follows :

it is to be noted that , What member variables are used in your code , You should pay attention to initializing the constructor , Otherwise, an error will occur during the call , For example, the following is not enough :

class BirdClass(AnimalBaseClass):
def __init__(self, num2):
self.head_num = num2
def cry(self):
print(' Squeak ')
def run(self):
print(' run ')
def get_head_num(self):
print(self.head_num)
piyo_suke = BirdClass(1)
piyo_suke.cry()
piyo_suke.run()
piyo_suke.get_legs_num()
piyo_suke.get_head_num()

In the above code , Member functions get_legs_num() Member variables are used animallegs, But no initialization , So in the call statement piyo_suke.get_legs_num() The following mistakes will be reported :

Some suggestions related to code normalization

Pay attention to the following points in the writing format of the code ( Not necessary , But the code written in this way is beautiful and standard ):

  • There should be a blank line between the member variable and the member function ;
  • There should be a blank line between member functions ;
  • There should be two blank lines between the class and the body code ;
  • There should also be two blank lines between classes ;
  • The class name is named by hump method ( About naming rules , Please refer to the blog https://blog.csdn.net/wenhao_ir/article/details/123117194);

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