• 面向对象之基础


    
    
    oop把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数

    面向对象的设计思想是从自然界中来的,因为在自然界中,类(class)和实例(instance)的概念是很自然的
    类是一种抽象的模板

    创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字成为类的属性
    创建一个对象就会创建一个对象的名称空间,存放对象的名字,称为对象的属性


    可以自由地给一个实例变量绑定属性,但是由于类起到模板的作用,因此在定义类的时候,
    可以通过定义一个特殊的函数__init__,将一些我们认为必须绑定的属性强制写进去,这样实例就可以使用这些属性
    
    
     函数是公用的,不论哪个对象都可以调用
    方法是绑定对象的,只对调用它的对象有用


    #类:一、实例化 二 引用名字 (类名.变量名, 类名.函数名)

    class Garen: #类的名字 首字母大写
    camp = 'Demacia'
    def attack(self):
    print('attack')
    #如何使用类
    x = int(10)
    print(x)

    obj = Garen() #实例化
    print(obj)

    print(Garen.camp) #引用类的特征 :变量
    print(Garen.attack) #引用类的特征 :函数
    #实例: 引用名字(实例名.类的变量,实例名.绑定方法,实例名.实例自己的变量名
    #如何使用实例
    class Garen:
    camp = 'Demacia'
    def __init__(self,nickname):
    self.nick=nickname #g1.nick = nickname
    def attack(self):
    print('attack')

    g1 = Garen('caoconglun') #自动调用函数 Garen.__init__(g1,'caocongln') 并将g1作为一个参数传给函数
    g2 = Garen('weisuolun')

    print(g1.nick) #查看g1对象的实例化名字 即查看__init__函数的执行结果
    g1.attack() #对象调用方法没有参数不会报错,因为它将自身g1作为参数传给函数attack


    Garen.attack() #类调用函数没有参数会报错,因为函数形参为位置参数不传参会报错

    print(g1.nick)
    print(g1.camp)
    print(g1.attack) # 对象调用的是绑定方法
    print(Garen.attack) # 类调用的是函数

    #类的变量的增删改查
    print(Garen.camp)#查
    Garen.camp='aaaaaaaaa' #改
    print(Garen.camp)

    Garen.x = 1 #增
    print(Garen.x)

    del Garen.x #删
    print(Garen.x)

    #实例对自己变量名和自己方法的操作
    g1 = Garen('alex') #设置实例出来的对象的名字
    print(g1.nick)

    del g1.nick #删除对象的绑定方法(即解除绑定)
    print(g2.nick)

    g1.sex = 'famale' #实例自己的变量名
    print(g1.sex)



    class Student(object):
        '学生类'
    country = 'china'
    def __init__(self,id ,name,sex,province): #并不是内置的函数,用来定义对象的独特的属性
    self.id = id
    self.name = name
    self.sex = sex
    self.province = province
    def search_score(self):
    print('tell score')
    def study(self):
    print('study',self)
    s1 = Student('23577889','cobila','famale','shandong')

    print(Student.__dict__) #查看类的名称空间
    print(s1.__dict__) #查看对象的名称空间
    print(Student.__bases__) #查看类的基类
    print(Student.__doc__) #查看类的备注信息
    print(s1.id) #对象本身就只有特征/变量

    s1 = Student.__init__(s1,'23577889','cobila','famale','shandong')



    面向对象的软件开发:
    面向对象分析 OOA
    面向对象设计 OOD
    面向对象编程 OOP
    面向对象测试 OOT
    面向对象维护 OOSM

    学校 共同的特征 : 培训类
    特有的特征 :name address teacher_name country
    共同的技能:recruit_student
    老师 共同的特征 :无
    特有的特征 :name id teach_course
    共同的技能:teach
    学生 共同的特征 : course
    特有的特征 :name age
    共同的技能:study search handin


    class School:
    type = 'education'
    def __init__(self,name,address,teacher_name,country):
    self.name = name
    self.address = address
    self.teacher_name = teacher_name
    self.country = country
    def find_student(self):
    print('招生啦')
    class Teacher:
    def __init__(self,name,id,teach_course):
    self.name = name
    self.id = id
    self.teach_course = teach_course
    def teach(self,lesson):
    print('teaching %s'%lesson)
    class Student:
    course = 'python'
    def __init__(self,name,age):
    self.name = name
    self.age = age
    self.course_list=[]
    def study(self):
    print('study python')
    def search(self,lesson_score):
    print('your score is %s'%lesson_score)

    def handin(self):
    pass
    class Course:
    def __init__(self,name,price,period):
    self.name = name
    self.price = price
    self.period = period



    s1 = Student('liuliu',22)
    print(s1.name,s1.age)
    print(s1.course_list)
    s1.course_list.append('python')
    s1.course_list.append('linux')
    print(s1.course_list)

    python_obj = Course('python',15800,'5m')
    linux_obj = Course('linux',10000,'2m')

    s1.course_list.append(python_obj)
    s1.course_list.append(linux_obj)

    print(s1.course_list)

    print('''student name is %s
    course name is %s
    course price is %s
    '''%(s1.name,s1.course_list[0].name,s1.course_list[1].price))
     
  • 相关阅读:
    ASP.net中页面事件的先后顺序
    我回来了
    ASP.NET中添加引用不能显示
    VS2008中MVC无法打开项目文件,此安装不支持该项目类型
    windows2003 IIS错误
    C#中使用TimeSpan计算两个时间的差值
    javascript做在翻译
    GridView导出EXCEL
    用资源管理器限制大数据量查询
    linux单机配置DG过程记录
  • 原文地址:https://www.cnblogs.com/liuguniang/p/6748807.html
Copyright © 2020-2023  润新知