我们都知道数据封装、继承和多态是面向对象的三大特点,Python中也需要用对象来包装好一系列数据的组织结构,让对象的功能和属性数据能够结合在一起。
1. Python中的对象
类和对象是面向对象编程的两个主要方面。类创建一个新类型,而对象就是这个类型的实例。在Python中,我们把一个对象的特征称为属性,而把对对象的操作称为方法。
对象 = 属性 + 方法:
属性:
Student.name
Student.sex
Student.age
方法:
Student.display()
2. 类和实例
类(class)是对象的抽象,而对象是类的实例。
举例:
class Ball: #Ball类
def bounce(self): #这是一个方法
if self.direction == "down":
self.direction == "up"
class后面紧接着是类名,即Ball,类名通常是大写开头的单词,紧接着是(self)
,self参数会告诉方法哪个对象调用它,这叫做实例引用。它在Python中没有任何特殊的含义,就是一个实例引用名。
这是从书上找的例子:
# 创建Ball类和MyBall对象实例
# -utf-8-
class Ball:
def bounce(self):
if self.direction == "down":
self.direction = "up"
MyBall = Ball() #创建一个类的实例MyBall
MyBall.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"
print
MyBall.bounce()
print "Now the Ball's direction is", MyBall.direction
输出:
>>> ================================ RESTART ================================
>>>
I just created a Ball.
My Ball is small
My Ball is red
My Ball's direction is down
Now 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().py
class Person:
def __init__(self, name): #__init__()方法
self.name = name
def display(self):
print 'Hello, my name is', self.name
p = 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 msg
MyBall = Ball("red", "small", "down")
print MyBall
输出:
>>> ================================ RESTART ================================
>>>
Hi, I'm a small red ball!
4. 数据隐藏
在Python中,Class类有属性和数据操作,外部代码可以通过直接调用实例变量的方法来操作数据。
如果为了内部数据的安全,可以把属性的名称前加上下划线__
,在Python中,实例的变量名如果以__
开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问。
例子:
#Student_init.py
class Student(self):
def __init__(self, name, score):
self.__name = name
self.__score = score
def print_score(self):
print self.__name, self.__score
S = Student('ZhangSan', 98)
print S._name
输出:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "F:/python/WorkSpace/Ball.py", line 11, in <module>
print p._name
AttributeError: '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 area
class 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.py
class 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()
方法。先介绍这么多吧。。。