(十) Python面向对象编程
来源:互联网 发布:mdreanshop淘宝 正不正 编辑:程序博客网 时间:2024/09/21 09:22
我们都知道数据封装、继承和多态是面向对象的三大特点,Python中也需要用对象来包装好一系列数据的组织结构,让对象的功能和属性数据能够结合在一起。
1. Python中的对象
类和对象是面向对象编程的两个主要方面。类创建一个新类型,而对象就是这个类型的实例。在Python中,我们把一个对象的特征称为属性,而把对对象的操作称为方法。
对象 = 属性 + 方法:
属性:
Student.nameStudent.sexStudent.age
方法:
Student.display()
2. 类和实例
类(class)是对象的抽象,而对象是类的实例。
举例:
class Ball: #Ball类 def bounce(self): #这是一个方法 if self.direction == "down": self.direction == "up"
上述的例子创建了一个ball类,只有一个方法bounce()。class后面紧接着是类名,即Ball,类名通常是大写开头的单词,紧接着是(self)
,self参数会告诉方法哪个对象调用它,这叫做实例引用。它在Python中没有任何特殊的含义,就是一个实例引用名。
这是从书上找的例子:
# 创建Ball类和MyBall对象实例# -utf-8-class Ball: def bounce(self): if self.direction == "down": self.direction = "up"MyBall = Ball() #创建一个类的实例MyBallMyBall.direction = "down" #************MyBall.color = "red" #设置实例的属性MyBall.size = "small" #************print "I just created a Ball."print "My Ball is ", MyBall.size #***********print "My Ball is ", MyBall.color #打印对象的属性print "My Ball's direction is ", MyBall.direction #*********** print "Now I'm going to bounce the Ball" printMyBall.bounce() print "Now the Ball's direction is", MyBall.direction
输出:
>>> ================================ RESTART ================================>>> I just created a Ball.My Ball is smallMy Ball is redMy Ball's direction is downNow I'm going to bounce the Ball #此处调用bounce()方法是为了让球的方向反弹Now the Ball's direction is up #球的方向发生变化,从"down"变成"up"
3. __init__()方法和__str__()方法
(1) 创建类定义时候,定义一个特定的方法,就是__init__()方法,只要创建了类的实例,我们可以使用_init_()来做对象的初始化。
#FileName: Person__init().pyclass Person: def __init__(self, name): #__init__()方法 self.name = name def display(self): print 'Hello, my name is', self.namep = Person('ZhangSan') #属性作为__init__()的参数传入p.display()
输出:
Hello, my name is ZhangSan
(2) 另一种特殊方法是__str__()方法,打印出你想要让它具体显示的内容。
class Ball: def __init__(self, color, size, direction): self.color = color self.size = size self.direction = direction def __str__(self): msg = "Hi, I'm a " + self.size + " " + self.color + "ball!" return msgMyBall = Ball("red", "small", "down")print MyBall
输出:
>>> ================================ RESTART ================================>>> Hi, I'm a small red ball!
4. 数据隐藏
在Python中,Class类有属性和数据操作,外部代码可以通过直接调用实例变量的方法来操作数据。
如果为了内部数据的安全,可以把属性的名称前加上下划线__
,在Python中,实例的变量名如果以__
开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问。
例子:
#Student_init.pyclass Student(self): def __init__(self, name, score): self.__name = name self.__score = score def print_score(self): print self.__name, self.__scoreS = Student('ZhangSan', 98)print S._name
输出:
>>> ================================ RESTART ================================>>> Traceback (most recent call last): File "F:/python/WorkSpace/Ball.py", line 11, in <module> print p._nameAttributeError: 'Student' object has no attribute '_name'>>>
说明在外部操作已经无法访问内部的数据了,这样使得代码更加健壮。但是外部想要访问数据可以用 "get_name" 和 "get_score"来获取name和score的数据,或者直接使用 "set_score"来设置修改内部score的数据。
5.多态和继承
多态:对于不同的类,可以有同名的两个或是多个方法。
例子:
class Triangle: #三角形 def __init__(self, width, height): self.width = width self.height = height def getArea(self): area = self.width * self.height / 2.0 return areaclass Square: #正方形 def __init__(self, size): self.size = size def getArea(self): area = self.size * self.size return area
>>>MyTriangle = Triangle(4, 5) #两个实例化的对象>>>MySquare = Square(7)
输出:
>>>MyTriangle.getArea() #调用方法名getArea()来计算它们的面积10.0>>>MySquare.getArea()49
继承:在面向对象编程中,类可以从其他类继承属性和方法。新的class称为子类,而被继承的class称为基类、父类或超类。
看网上的例子:
#Animal_run.pyclass Animal: def run(self): print 'Animal is running...'class Dog(Animal): def run(self): print 'Dog is running...' def eat(self): print 'Eating meat...' class cat(Animal): def run(self): print 'Cat is runing...' dog = Dog()dog.run()dog.eat()cat = cat()cat.run()
输出:
>>> ================================ RESTART ================================>>> Dog is running...Eating meat...Cat is runing...>>>此例中子类得到的最大好处就是获得了父类的全部功能。由于Animal实现了
run()
方法,因此,Dog和Cat作为它的子类,不用干活就自动拥有了run()
方法。先介绍这么多吧。。。- (十) Python面向对象编程
- python(十)面向对象编程
- python的学习(二十)--面向对象编程
- python面向对象编程
- python 面向对象编程
- python面向对象编程
- python面向对象编程
- Python 面向对象编程
- Python面向对象编程
- Python 面向对象编程
- python--面向对象编程
- python面向对象编程
- python 面向对象编程
- Python:面向对象编程
- python面向对象编程
- Python面向对象编程
- python面向对象编程
- Python面向对象编程
- Android ListView 数据混乱
- hdu 4941 Magical Forest
- 克隆git仓库
- Android 学习笔记——利用JNI技术在Android中调用、调试C++代码
- Android 知识点收藏
- (十) Python面向对象编程
- 小希的迷宫(并查集,未知点,限定一个老大,判断是否有圈)
- 【深度优先搜索】HDU 2553 N皇后问题
- RecyclerView实现瀑布流效果(二)
- ZOJ Problem Set - 2136 Longest Ordered Subsequence
- Blog API Test
- PHP合并数组array_merge函数运算符加号与的区别
- mysql
- 社会工程学笔记1