小明和阿花,python 面向对象基本释义

2022-08-1718:57:08编程语言入门到精通Comments1,346 views字数 3455阅读模式

面向对象基本释义

  • 对象: 具有一系列属性方法的集合
  • 举例:
    ---------------------------------------
                      同学小明
    --------------------属性----------------
    名字: 小明
    父亲:(未知)
    母亲:(未知)
    性别:男
    年龄:15   
    --------------------方法----------------
    滚出去: 调用此方法可以让小明滚出教室
    闭嘴: 调用此方法可以让小明闭嘴
    告密:(仅老师可用)
    写给阿花从未寄出去的表白信:(仅自己可知)
  • 属性说明:
    • 小明的名字、年龄、性别,对外可见,就是public属性;
    • 小明的父亲、母亲对外不可见,只有小明知道,这个就是private属性;
    • 如果小明还有兄弟姐妹,那么父亲、母亲这两个属性对兄弟姐妹父母都是可见的,这个就是protected属性,即有选择范围的对外可见
      • 一般情况下指的是同一个package范围
  • 方法说明:
    • 滚出去、闭嘴这两个是任何人可以对小明进行调用的方法,默认public
    • 告密是只有指定的人才可以调用,即选择性开放,一般为proteced
    • 从未寄出去的表百姓,就是私有方法,除了自己谁都不知道,private
说明:
1. Python中默认是没有proteced属性的,而且共有私有之间隔离并不是完全绝对
2. Python中所有类型默认都是对象形式组织

面向对象在类中的体现

上面说的小明,用代码表述出来就是:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

 1 #! coding:utf-8
 2 # object是对象类,也是python种一切对象的父类;所有的类型都显示/隐式的继承自object
 3 # 平时使用时,可以直接写class XiaoMing
 4 class XiaoMing(object):
 5 
 6     name = "小明"
 7     age = 15
 8     gender = "男"
 9 
10     __father = "father_of_xiaoming"
11     __mother = "mother_of_xiaoming"
12 
13     def __init__(self, relationship):
14         self.relationship = relationship
15 
16     def goOut(self):
17         '''
18             小明滚出去
19         '''
20         print "Xiaoming, your %s let you go out"%self.relationship
21 
22     def shutUp(self):
23         '''
24             小明闭嘴
25         '''
26         print "Xiaoming, your %s let you shut up"%self.relationship
27 
28     # python没有proteced,默认为同一个package可以直接调用
29     def gaoMi(self):
30         '''
31             告密
32         '''
33         pass
34 
35     #私有方法,开头方式是这样
36     def __sayLove(self):
37         '''
38             写一封不敢寄出去的情书,唉,少年应许多情
39         '''
40         print "%s, you should not watch it!"%self.relationship

小明这个类的使用过程是这样的文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

#实例化一个对象
xiaoming = XiaoMing("dude")
print xiaoming.name
print xiaoming.age
print xiaoming.gender
xiaoming.goOut()
xiaoming.shutUp()
xiaoming.gaoMi()

# ======output======== #
# 小明
# 15
# 男
# Xiaoming, your dude let you go out
# Xiaoming, your dude let you shut up

Python中对象的应用

在python中可以通过dir(obj)来查看每个对象当前可用的属性和方法,如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

print dir(xiaoming)

# ======output======== #
#['_XiaoMing__father', '_XiaoMing__mother', '_XiaoMing__sayLove', '__doc__', '__init__', '__module__', 'age', 'gaoMi', 'gender', 'goOut', 'name', 'relationship', 'shutUp']

上面例子中,注意_XiaoMing__father_XiaoMing__mother,这两个属性为私有变量__father、__mother的外部调用方式,如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

print xiaoming._XiaoMing__father
# ======output======== #
# father_of_xiaoming

python中一切皆对象,凡是对象都可以用dir(obj)来查看属性方法,可以试试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

类继承与重构

小明长大变超人了!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

    ---------------------------------------
                      超人小明
    --------------------属性----------------
    年龄:30   
    必杀技:镭射光线!biubiu!
    --------------------方法----------------
    滚出去: 谁敢叫本超人滚出去,站出来试试!
    告密:已经不需要告密了
    写给老婆阿芳的家信:暗恋的人最终没有成为自己的老婆啊
  • 属性说明:
    • 超人小明仍然具备同学小明的一切属性,除了年龄在长大
    • 超人小明有了自己的必杀技,毕竟是超人嘛
  • 方法说明:
    • 滚出去:现如今哪个不怕死的敢让超人滚出去
    • 告密:那些黑历史怎么可能重提
    • 写给老婆阿芳的家信:心里也继续暗恋阿花,但毕竟有了自己的老婆了

如上,超人小明是同学小明后续的一个可能,也就是扩展子类,用代码描述为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

#! coding:utf-8
class SuperManXiaoMing(XiaoMing):

    action = "镭射光线!biubiu!"
    def __init__(self, relationship):
        super(SuperManXiaoMing, self).__init__(relationship)
        self.age = 30

    def goOut(self):
        print "现如今哪个不怕死的敢让超人滚出去! %s 你小子活腻歪了是吧!"%self.relationship

    def gaoMi(self):
        pass

    def loveToWife(self):
        print "阿芳,额想你!"

调用的例子为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

    superman = SuperManXiaoMing("dude")
    print superman.name
    print superman.age
    print superman.gender
    print superman.action
    superman.goOut()
    superman.shutUp()
    superman.gaoMi()
    superman.loveToWife()

    print dir(superman)
    print superman._XiaoMing__father
# ======output======== #
# 小明
# 30
# 男
# 现如今哪个不怕死的敢让超人滚出去! dude 你小子活腻歪了是吧!
# Xiaoming, your dude let you shut up
# 阿芳,额想你!
# ['_XiaoMing__father', '_XiaoMing__mother', '_XiaoMing__sayLove', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'action', 'age', 'gaoMi', 'gender', 'goOut', 'loveToWife', 'name', 'relationship', 'shutUp']
# father_of_xiaoming

看,上述输出中,gaoMi没有输出,因为被pass了。其他父类子类的内容全在里面了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html

思考1

  • 掌握上述知识可以做什么?
  • 继承父类,super(className, self).init() 和 ParentClass.init()区别是什么?
  • 类的使用场景有哪些?
  • 对测试而言,类的好处与不好处有哪些?
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/27357.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/27357.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定