Python--面相对象基础

Python–面相对象基础

1.面相对象基础概念

  面相过程编程vs面相对象编程

2.类与对象

  类:是描述一组具有相同属性和方法的模版

  对象:对象是类的实例,是基于类创建出来的

  对象是由类创建出来的,创建对象的过程,也称为对象的实例化,类名的命名规范,遵循大驼峰命名法,每个单词的首字母都是大写,单词之间没有分隔符,例如:UserInfo UserAccount

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#类的定义方法一 动态添加类的属性
class Car:
# pass 用于占位,不执行任何操作
pass

c1 = Car()
c1.color = "red"
c1.brand = "BMW"
c1.name = "X5"
c1.price = 500000

print(c1.__dict__) # 打印对象c1的属性字典

#类的定义方法二 初始化方法
class Car:
#__init__方法是初始化的方法,会在对象创建时自动调用,可以在该方法中定义类的属性
def __init__(self, color, brand, name, price):
self.color = color
self.brand = brand
self.name = name
self.price = price
print("Car is created")

c2 = Car("blue", "Audi", "A4", 400000)
print(c2.__dict__) # 打印对象c2的属性字典

print("--------------------------------") #分割线

  __init__:初始化方法,对象创建后自动调用,主要用于设置对象的初始状态(设置对象属性)

3.实例方法

 在类中定义实例方法时,定义语法与之前学习的函数定义的方式是一致的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Car:
def __init__(self, color, brand, name, price):
self.color = color
self.brand = brand
self.name = name
self.price = price
print("Car is created")

#定义实例方法
def running(self):
print(f"{self.brand} {self.name} is running")

def total_cost(self, discount, rate):
"""
计算总费用
参数:
discount:折扣率
rate:月利率
返回值:
总费用
"""
total_cost = self.price * discount + rate * self.price
return total_cost

#调用
c3 = Car("red", "Audi", "A4", 400000)
c3.running()
print(c3.total_cost(0.9, 0.03))
print("--------------------------------") #分割线

4.魔法方法

  魔法方法是指Pyton中提供的以下划线开头和结尾的特殊方法,用于定义类的特殊行为。比如:__init__,魔法方法是不需要我们手动嗲用的,Python会在合适的时机自动调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#面相对象的魔法方法的使用
class Car:
def __init__(self, color, brand, name, price):
self.color = color
self.brand = brand
self.name = name
self.price = price
print("Car is created")

def running(self):
print(f"{self.brand} {self.name} is running")

#__str__方法是打印对象时调用的方法,返回一个字符串
def __str__(self):
return f"{self.brand} {self.name} is a {self.color} car"

#__eq__方法是比较对象是否相等的方法,返回一个布尔值
def __eq__(self, other):
return self.brand == other.brand and self.name == other.name

#__lt__方法是比较对象是否小于的方法,返回一个布尔值
def __lt__(self, other):
return self.price < other.price

#__gt__方法是比较对象是否大于的方法,返回一个布尔值
def __gt__(self, other):
return self.price > other.price

#__le__方法是比较对象是否小于等于的方法,返回一个布尔值
def __le__(self, other):
return self.price <= other.price

#__ge__方法是比较对象是否大于等于的方法,返回一个布尔值
def __ge__(self, other):
return self.price >= other.price

c1=Car("red", "Audi", "A4", 400000)
c2=Car("blue", "Audi", "A4", 400000)
c3=Car("red", "Audi", "A4", 400000)

print(c1 == c2) # False
print(c1 == c3) # True
print(c1 < c2) # False
print(c1 > c2) # False
print(c1 <= c2) # True
print(c1 >= c2) # True
print("--------------------------------") #分割线

  

5.实例属性与类属性

  • 实例属性:

    实例属性属于每个具体对象的属性,每个对象都是独立的(各个对特有的数据)

  • 类属性:

    类属性是属于类本身的属性,所有实例共享的。(所有对象共享的数据或配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#实例属性和类属性
class Car:
#类属性是所有对象共享的属性,定义在类中,方法外
wheel = 4 # 轮胎数量
tax_rate = 0.03 # 税率
def __init__(self, color, brand, name, price):
#实例属性是每个对象独有的属性,定义在方法中
self.color = color
self.brand = brand
self.name = name
self.price = price
self.wheel = 2
print("Car is created")

def total_cost(self, discount, rate):
"""
计算总费用
参数:
discount:折扣率
rate:月利率
返回值:
总费用
"""
total_cost = self.price * discount + rate * self.price
return total_cost


c1 = Car("red", "Audi", "A4", 400000)
print(c1.wheel) # 打印对象c1的属性字典 通过实例对象,会先查找实例属性,没有找到,就查找类属性
print(c1.tax_rate) # 打印对象c1的属性字典
print(c1.__dict__) # 打印对象c1的属性字典
print("--------------------------------") #分割线

Python--面相对象基础
https://one-null-pointer.github.io/2025/12/29/Python--面相对象基础/
Author
liaoyue
Posted on
December 29, 2025
传送口