《Python编程的术与道:Python语言入门》视频课程
《Python编程的术与道:Python语言入门》视频课程链接:https://edu.csdn.net/course/detail/27845
类和对象 (Classes and Objects)
类和对象是面向对象编程的两个主要方面。
类是创建单个对象的蓝图。
![
在这里插入图片描述 | |
---|---|
](https://img-blog.csdnimg.cn/54baa22fb51842b0a2befa934dd0d995.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAYmFpNjY2YWk=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
例如,在现实世界中,可能存在成千上万辆具有相同品牌和型号的汽车。
每辆汽车都是根据相同的蓝图建造的,因此包含相同的组件。
用面向对象的术语来说,具体的某辆汽车是Car类的一个实例(对象)。
在Python中,一切都是对象 (everything is an object),包括:整数、字符串、列表、函数,甚至类本身。
但是,Python在特殊语法的帮助下隐藏了对象机制。
例如,当键入num = 42
时,Python实际上会创建一个整数类型的新对象,其值是42,并将其引用赋值给名称num。
面向对象程序设计(Object Oriented Programming,OOP)以对象为核心,该方法认为程序由一系列对象组成。类是对现实世界的抽象,包括表示数据和对数据的操作,对象是类的实例化。对象间通过消息传递相互通信,来模拟现实世界中不同实体间的联系。在面向对象的程序设计中,对象是组成程序的基本模块。
创建类
要在Python中创建自己的自定义对象,首先需要使用关键字class定义一个类。
假设要创建对象来表示有关汽车的信息。 每个对象将代表一辆汽车。 首先,需要定义一个名为Car的类。
Example: Create a class called ‘Car’
class Car:
pass
这里的pass语句用于指示该类为空。
__init__()
方法
__init __()
是初始化一个对象的特殊方法。
每次创建类的对象时,此方法都会自动运行。
__init __()
方法通常用于执行创建对象之前必需的操作。
class Car:
# initializer
def __init__(self):
pass
在类定义中定义__init __()
时,其第一个参数应为self
。
self
参数
self
参数是指一个对象本身。它用于获取或设置特定实例的属性。
self
应该始终是类中任何方法的第一个参数,即使该方法不使用它也是如此。
属性 (Attributes)
用Python编写的每个类都有两个基本功能:属性和方法。
属性是将一个对象与另一个对象区分开的独立事物。 它们确定该对象的外观、状态或其他。
在我们的例子中,“汽车”类可能具有以下属性:
- 样式:轿车、SUV、双门轿跑车
- 颜色:银色、黑色、白色
- 车轮:四个
属性是通过变量在类中定义的,每个对象对于这些变量可以有自己的值。
属性有两种类型:实例属性(Instance attributes)和类属性(Class attributes)。
实例属性
实例属性对于每个对象(实例)都是唯一的。 该类的每个对象都有该变量自己的副本。
对这种变量所做的任何更改都不会反映在该类的其他对象中。
就我们的Car()类而言,每辆车都有特定的颜色和样式。
Example: A class with two instance attributes
class Car:
# initializer with instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
类属性
所有对象的类属性都相同。 该变量只有一个副本与所有对象共享。
对该变量所做的任何更改都将反映在所有其他对象中。
就我们的Car()类而言,每辆车有4个轮子。
Example: A class with one class attribute
class Car:
# class attribute
wheels = 4
# initializer with instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
因此,虽然每辆车都有自己独特的样式和颜色,但每辆车都会有4个车轮。
创建一个对象 (Create an Object)
可以通过调用类名并像传递函数一样传递参数来创建类的对象。
示例:通过传递样式和颜色从“Car”类创建对象
class Car:
# class attribute
wheels = 4
# initializer with instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
c = Car('Sedan', 'Black')
print(c)
<__main__.Car object at 0x000002B92E8DC708>
在这里,我们通过传递样式和颜色参数的字符串从Car类中创建了一个新对象。
但是,我们没有传递self
参数。这是因为,当创建一个新对象时,Python会自动确定自己是什么(在这个例子中,就是我们新创建的对象),并将其传递给__init__
方法。
访问和修改属性
使用.
符号可访问实例的属性并将其赋值给该属性。
class Car:
# class attribute
wheels = 4
# initializer with instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
c = Car('Black', 'Sedan')
# Access attributes
print(c.style) # Sedan
print(c.color) # Black
# Modify attribute
c.style = 'SUV'
print(c.style) # SUV
Sedan
Black
SUV
方法 (Methods)
方法确定类具有的功能类型,类如何处理其数据以及其总体行为。
没有方法,一个类将仅仅是一个结构。
在我们的例子中,“汽车”类可能具有以下方法:
- 换颜色 (Change color)
- 启动引擎 (Start engine)
- 停止引擎 (Stop engine)
- 换档 (Change gear)
就像有实例属性和类属性一样,也有实例方法和类方法。
实例方法对类的实例进行操作; 而类方法对类本身进行操作。
实例方法 (Instance Methods)
实例方法是在类中定义的对类的实例进行操作的函数。
现在,我们向类添加一些方法。
- showDescription()方法:打印所有实例属性的当前值
- changeColor()方法:更改“颜色”属性的值
class Car:
# class attribute
wheels = 4
# initializer / instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
# method 1
def showDescription(self):
print("This car is a", self.color, self.style)
# method 2
def changeColor(self, color):
self.color = color
c = Car('Black', 'Sedan')
# call method 1
c.showDescription() # This car is a Black Sedan
# call method 2 and set color
c.changeColor('White')
c.showDescription() # This car is a White Sedan
This car is a Black Sedan
This car is a White Sedan
删除属性和对象 (Delete Attributes and Objects)
要删除对象属性,可使用del关键字。
Example: Delete an attribute
del c.color
可以使用del
关键字完全删除对象。
Example: Delete the object completely
del c