面向对象的编程 (OOP) 是一种编程范例,可使用它以代码形式对现实世界进行建模。 借助 OOP,可通过更少的代码创建易于修改和扩展的实现。
面向对象的编程 (OOP) 是一种编程范例。
无论使用哪种范例,程序都使用相同的一系列步骤来解决问题:
1、数据输入:从某个位置读取数据,此位置可以是数据存储,如文件系统或数据库。
2、处理:数据被解释并且可能被修改,以准备进行显示。
3、数据输出:显示数据,使物理用户或系统能够读取数据并与之交互。
数据封装
简易性
易于修改
可维护性
可重用性
通过使用关键字class并为其提供一个名称。
class Car;
若要对一个对象进行实例化,可向类的名称添加括号。
car=Car()
描述的对象
保持状态
在 Python 中,构造函数的名称为 __init()__。 你还需要将特殊关键字 self 作为参数传递给构造函数。 关键字 self 引用该对象的实例。 对此关键字的任何赋值都意味着该特性最终出现在该对象实例上。 如果你不将特性添加到 self,则该特性将被视为在 __init()__ 执行完毕后不再存在的临时变量。
class Elevator:
def __init__(self, starting_floor):
self.make = "The elevator company"
self.floor = starting_floor
# To create the object
elevator = Elevator(1)
print(elevator.make) # "The Elevator company"
print(elevator.floor) # 1
[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\program.py"
The elevator company
1
[Done] exited with code=0 in 1.825 seconds
为了强调关键字 self 的工作方式,请考虑以下代码,其中在构造函数 __init__() 中分配了两个特性 color 和 make。
class Car:
def __init__():
self.color = "Red" # ends up on the object
make = "Mercedes" # becomes a local variable in the constructor
car = Car()
print(car.color) # "Red"
print(car.make) # would result in an error, `make` does not exist on the object
[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\program.py"
Traceback (most recent call last):
File "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\program.py", line 6, in
car = Car()
TypeError: __init__() takes 0 positional arguments but 1 was given
[Done] exited with code=1 in 1.747 seconds
init的后面()里增加self,运行结果如下:
[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\program.py"
Red
Traceback (most recent call last):
File "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\program.py", line 8, in
print(car.make) # would result in an error, `make` does not exist on the object
AttributeError: 'Car' object has no attribute 'make'
[Done] exited with code=1 in 0.442 seconds
从make=,修改为self.make=,运行结果如下:
[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\tempCodeRunnerFile.py"
Traceback (most recent call last):
File "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python\tempCodeRunnerFile.py", line 1, in
print(car.make) # would result in an error, `make` does not exist on the object
NameError: name 'car' is not defined
[Done] exited with code=1 in 1.971 seconds
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python> touch rock-paper-scissor.py
touch : The term 'touch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the s
pelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ touch rock-paper-scissor.py
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (touch:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python>
OOP中的方法
封闭:保护数据
为何需要
你不需要知道内部数据。
你不应该知道内部数据。
访问级别
Python 完成数据隐藏的方式是向特性名称添加前缀。 一条前导下划线 _
向外界传达了一个信息,即可能不应接触这些数据。
什么是Getter和Setter?
对Getter和Setter使用修饰器
class Participant:
def __init__(self, name):
self.name = name
self.points = 0
self.choice = ""
def choose(self):
self.choice = input("{name}, select rock, paper or scissor: ".format(name= self.name))
print("{name} selects {choice}".format(name=self.name, choice = self.choice))
class GameRound:
def __init__(self, p1, p2):
p1.choose()
p2.choose()
def compareChoices(self):
print("implement")
def awardPoints(self):
print("implement")
class Game:
def __init__(self):
self.endGame = False
self.participant = Participant("Spock")
self.secondParticipant = Participant("Kirk")
def start(self):
game_round = GameRound(self.participant, self.secondParticipant)
def checkEndCondition(self):
print("implement")
def determineWinner(self):
print("implement")
game = Game()
game.start()
运行结果如下:
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python> python program.py
Spock, select rock, paper or scissor: rock
Spock selects rock
Kirk, select rock, paper or scissor: paper
Kirk selects paper
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Python>
没搞懂。