zoukankan      html  css  js  c++  java
  • 面向对象联系

    #面向对象
    # 类:把一类事物的相同特性和动作整合到一起就是类。类是抽象的
    # 对象:就是基于类而创建的一个具体的事物(具体存在的),也是特征和动作整合一起

    #类:学校
    #类的特征:校名,地址,性质
    #对象(动作,功能):招生,放假

    #用函数实施面向对象编程
    def school(name,addr,type):
    def init(name,addr,type):
    sch = {
    'name':name,
    'addr':addr,
    'type':type,
    'zhao_sheng':zhao_sheng,
    'fang_jia':fang_jia
    }
    return sch
    def zhao_sheng(sch):
    print('%s 正在招生'%sch['name'])

    def fang_jia(sch):
    print('位于%s的%s学校%s已经放假了'%(sch['addr'],sch['type'],sch['name']))
    return init(name,addr,type)

    a = school('杭电','杭州','公立')
    print(a) #{'name': '杭电', 'addr': '杭州', 'type': '公立', 'zhao_sheng': <function school.<locals>.zhao_sheng at 0x0000000002300B70>, 'fang_jia': <function school.<locals>.fang_jia at 0x0000000002300EA0>}

    a['zhao_sheng'](a) #杭电 正在招生

    c = school('苏大','苏州','公立')
    c['fang_jia'](c)#位于苏州的公立学校苏大已经放假了


    #用类class来实施面向对象编程
    class dog:
    def __init__(self,name,type,color):
    self.name = name
    self.type = type
    self.color = color
    def jiao(self):
    print('%s狗在叫' %self.name)

    def chi_shi(self):
    print('%s狗%s在吃屎' %(self.type,self.name))

    def shui_j(self):
    print('%s是%s色' %(self.name,self.color))


    a = dog('豆豆','公','黄色')
    a.jiao() #豆豆狗在叫

    b = dog('臭臭','母','白色')
    b.shui_j() #臭臭是白色色
  • 相关阅读:
    Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
    UVA 1597 Searching the Web
    UVA 1596 Bug Hunt
    UVA 230 Borrowers
    UVA 221 Urban Elevations
    UVA 814 The Letter Carrier's Rounds
    UVA 207 PGA Tour Prize Money
    UVA 1592 Database
    UVA 540 Team Queue
    UVA 12096 The SetStack Computer
  • 原文地址:https://www.cnblogs.com/lhqlhq/p/8821348.html
Copyright © 2011-2022 走看看