美文网首页
3.Python面向对象编程

3.Python面向对象编程

作者: Xia_91 | 来源:发表于2018-08-29 21:38 被阅读26次

#了解一下最基础的类的学习和面向对象的编程与一些引申知识。(self与_init_的用法)
'''
建造生物
类名:生物(creature)
属性(变量):名字(name) 年龄(age) 性别(sex)
方法(函数):呼吸(breath) 行走(move) 进食(eat) 交配(mate)

类名:鸡(cook)
属性(变量):颜色(color)
方法(函数):下蛋(lay)  

类名:人(human)
属性(变量):电话(tel) 住址(address) 学历(degree)
方法(函数):学习(study) 工作(work) 自我介绍(introduce)
'''

class  creature(object):
    def __init__(self,name,age,sex,**kw):  #'__init__'用于初始化属性
        self.name = name
        self.age = age
        self.sex = sex
        for k,w in kw.items():      #**kw表示任意常数,这个for 循环是必须的,由于设置属性值。
            setattr(self,k,w)      #setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在。object -- 对象。        setattr(object, name, value)
#                                                                                              name -- 字符串,对象属性。
#                                                                                              value -- 属性值。
    def breath():
        print('Breathing!!')

    def move():
        print('Step by!!')

    def eat():
        print('Hulk eating!')

    def mate():
        print('Love!!')

class cook(creature):   #继承的用法!
    def __init__(self,name,age,sex,color):
        super(cook, self).__init__(name,age,sex) #一定要用 super(cook, self).__init__(name, age,sex) 去初始化父类,否则,继承自 creature 的 cook 将没有 name 和 age sex
        self.color = color
    def lay():
        print('A egg born!')

class human(creature):
    def __init__(self,name,age,sex,tel,add,degree):
        super(human, self).__init__(name,age, sex)
        self.tel = tel
        self.add = add
        self.degree = degree

    def study(self):  #这里以self为函数参数的原因是因为此函数需要使用类本身定义的属性。
        print('{0} is studying!'.format(self.name)) #如果没有self的话就不知道这个属性是何归属(因为python不和C++一样有public 和 private 之分)。

    def work():
        print('Working!!')

    def introduce(self):
        print(f'My name is {self.name}. \n My tel is {self.tel}.')




#这是动态语言和静态语言(例如Java)最大的差别之一。动态语言调用实例方法,不检查类型,只要方法存在,参数正确,就可以调用。


def main():
    creature.breath()
    cook.lay()
    h = human('dick',11,'man',13807004070,"BackerStreet 1001",'High school')
    h.study()
    print(f'{h.name} is a {h.sex}')#fstring 用法
    h.introduce()
    M = creature('M',3,'female',tall = '18cm')
    print(f'{M.name} is {M.tall} tall.')
    
    #当我们得到一个变量时,如何判断他的信息?
    print(type(M))  #可以用 type() 函数获取变量的类型,它返回一个 Type 对象.
    print(dir(M))  #可以用 dir() 函数获取变量的所有属性.   dir()返回的属性是字符串列表,如果已知一个属性名称,要获取或者设置对象的属性,就需要用 getattr() 和 setattr( )函数了


if __name__ == '__main__':
    main()

相关文章

  • 3.Python面向对象编程

  • 面向对象_初识

    目录 面向对象编程介绍 类与对象介绍 私有属性与私有方法 面向对象编程 1. 面向对象编程介绍 面向对象编程:Ob...

  • 谈谈面向对象编程

    何为面向对象编程 面向对象编程简介 面向对象编程(Object-oriented Programming,缩写:O...

  • 面向对象基础

    面向对象编程包括: 面向对象的分析(OOA) 面向对象的设计(OOD) 面向对象的编程实现(OOP) 面向对象思想...

  • python-day14

    一、面向对象编程 编程思想:1.面向对象编程 --> 算法,逻辑2.函数式编程 --> 函数3.面向对象编程 ...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • 面向对象浅析

    ### 面向对象编程和面向对象编程语言 面向对象编程的英文缩写是 OOP,全称是 Object Oriented ...

  • 2017-08-14

    面向对象编程用对象的思想去写代码,就是面向对象编程-面向过程-面向对象面向对象编程的特点1.抽象 抽取一样的东西...

  • 面向对象编程,类和对象

    面向对象编程 Java是面向对象的一门编程语言,所以余姚使用者具备面向对象编程的思想。 那么,什么是面向对象编程呢...

网友评论

      本文标题:3.Python面向对象编程

      本文链接:https://www.haomeiwen.com/subject/ajclwftx.html