类是什么
类是对信息和行为进行分组的一种方式。例如,让我们考虑在物理模拟中建造一艘宇宙飞船。要做的第一件事是跟踪宇宙飞船的坐标 (x, y)。代码中飞船的形式如下:
class Rocket():
def __init__(self):
# Each rocket has an (x, y) position
self.x = 0
self.y = 0
在类中要做的第一件事是定义 __init__ 函数。当创建对象时,会执行__init__函数,为需要的参数设置初始值。后面会介绍self,简而言之,它是一种允许你访问类中变量的语法。
到目前为止,Rocket 类存储了两条信息,但它什么也没做。 Rocket类的第一个核心行为是:移动。代码如下所示:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
现在Rocket类存储了一些信息,可以做一些事情。但你还没有真正建造自己的船。接下来就是搭建自己的飞船了,代码如下:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
print("Created")
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a Rocket object.
my_rocket = Rocket()
print(my_rocket)
要使用该类,请创建一个变量,如 my_rocket 。然后用类名后跟圆括号为变量赋值。这将创建该类的一个对象。对象是类的实例,它拥有类中所有变量的副本,可以做类中定义的所有行为。在上面的代码中,可以看到变量my_rocket是__main__程序文件中的一个Rocket对象,存放在内存的特定位置。
使用类,你可以定义对象并使用其方法。举例如下:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a Rocket object, and have it start to move up.
my_rocket = Rocket()
print("Rocket altitude:", my_rocket.y)
my_rocket.move_up()
print("Rocket altitude:", my_rocket.y)
使用对象名称和点符号来访问对象的变量和方法。因此,要获得 my_rocket 的 y 值,请使用 my_rocket.y 。使用 my_rocket.move_up() 访问 move_up() 函数。
一旦定义了类,就可以创建任意数量的对象。每个对象都有独立的变量空间,互不影响。如下:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a fleet of 5 rockets, and store them in a list.
my_rockets = []
for x in range(0,5):
new_rocket = Rocket()
my_rockets.append(new_rocket)
# Show that each rocket is a separate object.
for rocket in my_rockets:
print(rocket)
如果你知道列表理解,你也可以重写代码如下:
class Rocket():
# Rocket simulates a rocket ship for a game,# Rocket
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a fleet of 5 rockets, and store them in a list.
my_rockets = [Rocket() for x in range(0,5)]
# Show that each rocket is a separate object.
for rocket in my_rockets:
print(rocket)
每个火箭在内存中都是独立的。有自己的 x 和 y 。你可以通过移动不同的船只来证明这一点。如下:
class Rocket():
# Rocket simulates a rocket ship for a game,
# or a physics simulation.
def __init__(self):
# Each rocket has an (x,y) position.
self.x = 0
self.y = 0
def move_up(self):
# Increment the y-position of the rocket.
self.y += 1
# Create a fleet of 5 rockets, and store them in a list.
my_rockets = [Rocket() for x in range(0,5)]
# Move the first rocket up.
my_rockets[0].move_up()
# Show that only the first rocket has moved.
for rocket in my_rockets:
print("Rocket altitude:", rocket.y)
现在类语法对你来说可能不是很清楚。但是考虑创建一个没有类的宇宙飞船。你可能将 x 和 y 存储在字典中,即使定义几艘船也需要编写大量丑陋且难以维护的代码。随着添加的功能越来越多,代码变得越来越难以维护。届时你将看到基于真实世界模型的高效课程。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python字典键的两个属性10/17
- ♥ 如何使用 python3 函数调用局部变量?10/08
- ♥ python如何在桌面创建图标09/10
- ♥ python是什么技术09/19
- ♥ 如何在python中删除换行符“\ n”?10/17
- ♥ 如何在python中打印100以内的斐波那契数?11/17
内容反馈