如果我们将语言称为高级语言,那么该语言就必须具有面向对象的范例。开发人员应该能够编写干净的面向对象的代码。而且,如果不使用面向对象,就不能构建大型项目。
Python提供了非常干净的面向对象设计语法。借助某些关键字,我们可以通过将代码视为真实世界对象来对代码进行编程。本文不会涵盖所有面向对象的编程概念。但是,它将教你如何在Python中设计面向对象的程序。所以,让我们开始吧。
Python中的类
类是OOP的基本概念。您可以将其视为现实世界对象的模板。例如在一个句子中 - 爱丽丝是人类,人类是真实世界对象爱丽丝的一类。类提供了一种在框中将数据和函数绑定在一起的方法。它可以被视为现实世界对象的模板。在技术方面,您可以将其称为用户定义的数据类型。
在Python中,我们可以创建一个这样的类。
class ClassName:
<statement-1>
.
.
.
<statement-N>
这里可以包含变量语句和函数语句。让我们看一个更好理解的例子
class House:
def getDimensions(self):
return f"Height - {self.height}, Length - {self.length}, Width - {self.width}"
在上面的例子中,我们创建了一个名为House的类。我们已经声明了三个可变长度,宽度和高度。我们已经声明了一个名为getDimensions(self)的函数。
关于self关键字:self关键字定义或指向调用方法的对象。
一个对象是一个类的实例。
现在,让我们创建一个House类的对象。在下面的例子中,myHouse是House类的一个实例。这意味着self现在等于myHouse。我们初始化了myHouse的变量,并在myHouse上调用了getDimensions方法。当方法getDimensions被执行时,语句self.height将等于myHouse.height,因为self指向myHouse。
myHouse = House()
myHouse.height = 100
myHouse.width = 500
myHouse.length = 800
dim = myHouse.getDimensions()
print(dim)
# Output -> Height - 100, Length - 800, Width - 500
类构造函数
类构造用于通过初始化它的部件和操作的方式访问类之前需要一些初始设置来构造一个类。Python提供了一个特殊的方法__init __(..)来初始化或构造一个类。
让我们修改我们的类来添加一个构造函数。
class House:
def __init__(self, length, width, height):
self.length = length
self.width = width
self.height = height
def getDimensions(self):
return f"Height - {self.height}, Length - {self.length}, Width - {self.width}"
myHouse = House(100, 500, 800)
dim = myHouse.getDimensions()
print(dim)
#Output -> Height - 800, Length - 100, Width - 500
在上面的代码中,我们在构造函数中声明实例变量,而在构造对象时,我们传递单个值。
类析构函数
一个类的析构函数是一个特殊的功能,当一个类的实例不再被调用。这用于在实例销毁之前清理任何变量或资源。让我们通过在House类中添加析构函数来看到这一点。
class House:
def __init__(self, length, width, height):
self.length = length
self.width = width
self.height = height
def getDimensions(self):
return f"Height - {self.height}, Length - {self.length}, Width - {self.width}"
def __del__(self):
print("Destructor called")
myHouse = House(100, 500, 800)
dim = myHouse.getDimensions()
print(dim)
# Output -> Height - 800, Length - 100, Width - 500
del myHouse
# Output -> Destructor called
在上面的例子中,我们删除了实例myHouse,因此析构函数被调用。
类变量v / s实例变量
- 类变量与一个类别相关联,并在所有情况下是共享的。
- 一个实例变量只与实例相关联。
让我们考虑下面的例子
class Shooting:
remainingBullets = 100
def __init__(self, playerName):
self.playerName = playerName
self.remainingBullets = 10
Shooting.remainingBullets -= 10
def shoot(self):
if(Shooting.remainingBullets == 0):
print("Can not shoot. No more bullets")
else:
self.remainingBullets -=1
shooting_alice = Shooting("Alice")
print("\nGame created for Alice")
print(f"Remaining bullets [Total] - {Shooting.remainingBullets}")
print(f"Remaining bullets [Alice] - {shooting_alice.remainingBullets}")
shooting_bob = Shooting("Bob")
print("\nGame created for Bob")
print(f"Remaining bullets [Total] - {Shooting.remainingBullets}")
print(f"Remaining bullets [Bob] - {shooting_bob.remainingBullets}")
shooting_alice.shoot()
print("\nAlice fired a shot")
print(f"Remaining bullets [Total] - {Shooting.remainingBullets}")
print(f"Remaining bullets [Alice] - {shooting_alice.remainingBullets}")
shooting_bob.shoot()
print("\nBob fired a shot")
print(f"Remaining bullets [Total] - {Shooting.remainingBullets}")
print(f"Remaining bullets [Bob] - {shooting_bob.remainingBullets}")
shooting_chris = Shooting("Chris")
print("\nGame created for Chris")
print(f"Remaining bullets [Total] - {Shooting.remainingBullets}")
print(f"Remaining bullets [Chris] - {shooting_chris.remainingBullets}")
for i in range(0,5):
shooting_chris.shoot()
print("\nChris fired 5 shots")
print(f"Remaining bullets [Total] - {Shooting.remainingBullets}")
print(f"Remaining bullets [Chris] - {shooting_chris.remainingBullets}")
# Output ->
# Game created for Alice
# Remaining bullets [Total] - 90
# Remaining bullets [Alice] - 10
#
# Game created for Bob
# Remaining bullets [Total] - 80
# Remaining bullets [Bob] - 10
#
# Alice fired a shot
# Remaining bullets [Total] - 80
# Remaining bullets [Alice] - 9
#
# Bob fired a shot
# Remaining bullets [Total] - 80
# Remaining bullets [Bob] - 9
#
# Game created for Chris
# Remaining bullets [Total] - 70
# Remaining bullets [Chris] - 10
#
# Chris fired 5 shots
# Remaining bullets [Total] - 70
# Remaining bullets [Chris] - 5
让我们通过上面的例子理解这一点。在上面的示例中,有一个类变量remainingBullets和一个实例变量remainingBullets。当类的一个实例拍摄被创建,从射击类10发子弹被分配给玩家。因此,现在剩余的子弹比现在少了10个,而玩家的剩余子弹现在是10个。
当玩家开枪时,子弹数量会从玩家的(实例) remainingBullets变量中减少。
当玩家的游戏被创建时,子弹数量会从射击游戏(类) 剩余的小圈变量中减少。
结论是,类的变量可以由不同的实例共享和更改,但实例的变量只能通过实例更改。
欢迎大家关注公众号:「Python精选」,关注公众号,回复「1024」你懂得,免费领取 6 本经典Python编程书籍。关注我,与 10 万程序员一起进步。每天更新Python干货哦,期待你的到来!
网友评论