[python] class (12)


  • Difference Between Object Oriented and Process Oriented
  • objects and classes
  • create class
  • define object
  • The three elements of a class (objects, properties, methods)

    1 classes, objects, methods

# 定义一个类
class Car:
    def drive(self):
        print('我正在开车') 
    def turnover(self):
        print('翻车了')
# 创建一个对象
momo = Car()
# 调用类方法
momo.drive()
momo.turnover()

The result is

我正在开车
翻车了

2 types of properties


You can define properties in a class, or you can add properties to an object

class Car:
    def drive(selt):
        print('我正在开车')

    def turnover(self):
        print('翻车了')

#创建一个对象
xiao_jie_jie=Car()
xiao_jie_jie.drive()#调用xiao_jie_jie指向的对象的方法
xiao_jie_jie.turnover()

#添加属性,属性就是变量
xiao_jie_jie.name = '莫莫'
xiao_jie_jie.age = 18
print ("%s的年龄是%d"%(xiao_jie_jie.name,xiao_jie_jie.age))

The result is

我正在开车
翻车了
莫莫的年龄是18

__init__

class Car:
    def drive(self):
        print ("driving")
    def turnover(self):
        print ("turnover")
    def __init__(self,name,age):  
        self.age = age
        self.name  = name
    def feature(self):
        print("%s的年龄是%d"%(self.name,self.age))
momo = Car("MM",18)
momo.drive()
momo.turnover()
momo.feature()

The result is

driving
turnover
MM的年龄是18

3 Examples (cooked noodles)

class Cook_instant_noodles:

    def __init__(self):
        self.cookedState = '生的'
        self.cookedLevel = 0

    def __str__(self):#相当于打印
        return '泡面状态:%s(%d)'%(self.cookedState,self.cookedLevel)

    def cook(self,cooked_time):
        if self.cookedLevel >= 0 and self.cookedLevel < 3:
            self.cookedState = '还没熟'
        elif self.cookedLevel >= 3 and self.cookedLevel < 5:
            self.cookedState = '半生不熟'
        elif self.cookedLevel >= 5 and self.cookedLevel < 8:
            self.cookedState = '煮熟了'
        elif self.cookedLevel >= 8:
            self.cookedState = '煮糊了'

#创建了一个泡面对象
instant_noodles = Cook_instant_noodles()
print(instant_noodles,"\n")

#开始煮泡面
instant_noodles.cook(2)
print(instant_noodles)

instant_noodles.cook(4)
print(instant_noodles)

instant_noodles.cook(6)
print(instant_noodles)

instant_noodles.cook(9)
print(instant_noodles)

The result is

泡面状态:生的(0) 

泡面状态:还没熟(0)
泡面状态:还没熟(0)
泡面状态:还没熟(0)
泡面状态:还没熟(0)

something wrong

class Cook_instant_noodles:

    def __init__(self):
        self.cookedState = '生的'
        self.cookedLevel = 0

    def __str__(self):
        return '泡面状态:%s(%d)'%(self.cookedState,self.cookedLevel)

    def cook(self,cooked_time):
        self.cookedLevel = cooked_time
        if cooked_time >= 0 and cooked_time < 3:
            self.cookedState = '还没熟'
        elif cooked_time >= 3 and cooked_time < 5:
            self.cookedState = '半生不熟'
        elif cooked_time >= 5 and cooked_time < 8:
            self.cookedState = '煮熟了'
        elif cooked_time >= 8:
            self.cookedState = '煮糊了'

#创建了一个泡面对象
instant_noodles = Cook_instant_noodles()
print(instant_noodles,"\n")

#开始煮泡面
instant_noodles.cook(2)
print(instant_noodles)

instant_noodles.cook(4)
print(instant_noodles)

instant_noodles.cook(6)
print(instant_noodles)

instant_noodles.cook(9)
print(instant_noodles)

The result is

泡面状态:生的(0) 

泡面状态:还没熟(2)
泡面状态:半生不熟(4)
泡面状态:煮熟了(6)
泡面状态:煮糊了(9)

OK


Monitor the status every minute

class Cook_instant_noodles:

    def __init__(self):
        self.cookedState = '生的'
        self.cookedLevel = 0

    def __str__(self):
        return '泡面状态:%s(%d)'%(self.cookedState,self.cookedLevel)

    def cook(self,cooked_time):
        self.cookedLevel = cooked_time
        if cooked_time >= 0 and cooked_time < 3:
            self.cookedState = '还没熟'
        elif cooked_time >= 3 and cooked_time < 5:
            self.cookedState = '半生不熟'
        elif cooked_time >= 5 and cooked_time < 8:
            self.cookedState = '煮熟了'
        elif cooked_time >= 8:
            self.cookedState = '煮糊了'
    def cook_state(self,i): #监控每分钟的状态
        for i in range(0,10):
            instant_noodles.cook(i)
            print(instant_noodles)

#创建了一个泡面对象
instant_noodles = Cook_instant_noodles()
print(instant_noodles,"\n")
#开始煮泡面
instant_noodles.cook_state(10)

The result is

泡面状态:生的(0) 

泡面状态:还没熟(0)
泡面状态:还没熟(1)
泡面状态:还没熟(2)
泡面状态:半生不熟(3)
泡面状态:半生不熟(4)
泡面状态:煮熟了(5)
泡面状态:煮熟了(6)
泡面状态:煮熟了(7)
泡面状态:煮糊了(8)
泡面状态:煮糊了(9)

add seasoning

class Cook_instant_noodles:

    def __init__(self):
        self.cookedState = '生的'
        self.cookedLevel = 0
        self.condiments = []
        self.J = ['a','b','c','d','e','f','g','h','i','j']
        self.A = [1,2,3,4,5,6,7,8,9,10]

    def __str__(self):
        return '泡面状态:%s(%d),佐料有%s'%(self.cookedState,self.cookedLevel,str(self.condiments))

    def cook(self,cooked_time):
        self.cookedLevel = cooked_time
        if cooked_time >= 0 and cooked_time < 3:
            self.cookedState = '还没熟'
        elif cooked_time >= 3 and cooked_time < 5:
            self.cookedState = '半生不熟'
        elif cooked_time >= 5 and cooked_time < 8:
            self.cookedState = '煮熟了'
        elif cooked_time >= 8:
            self.cookedState = '煮糊了'

    def addcondiments(self,thing):
        self.condiments.append(thing)

    def cook_state(self,A,J): #监控每分钟的状态
        A = self.A
        J = self.J
        for i,j in zip(A,J):
            instant_noodles.cook(i)
            self.addcondiments(j)
            print(instant_noodles)

#创建了一个泡面对象
instant_noodles = Cook_instant_noodles()
print(instant_noodles,"\n")
#开始煮泡面
instant_noodles.cook_state(10,'')

The result is

泡面状态:生的(0),佐料有[] 

泡面状态:还没熟(1),佐料有['a']
泡面状态:还没熟(2),佐料有['a', 'b']
泡面状态:半生不熟(3),佐料有['a', 'b', 'c']
泡面状态:半生不熟(4),佐料有['a', 'b', 'c', 'd']
泡面状态:煮熟了(5),佐料有['a', 'b', 'c', 'd', 'e']
泡面状态:煮熟了(6),佐料有['a', 'b', 'c', 'd', 'e', 'f']
泡面状态:煮熟了(7),佐料有['a', 'b', 'c', 'd', 'e', 'f', 'g']
泡面状态:煮糊了(8),佐料有['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
泡面状态:煮糊了(9),佐料有['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
泡面状态:煮糊了(10),佐料有['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Reference
[1] Python object-oriented programming starts from scratch (1) - from no object to object

[2] Python object-oriented programming starts from scratch (2) - mutual understanding with objects

[3] Python object-oriented programming starts from scratch (3) - Miss Sister's Treat Part 1

[4] Python object-oriented programming starts from scratch (4) - Miss Sister's Treat Part II

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326696508&siteId=291194637
Recommended