2026/1/9 类1

6 阅读2分钟

《预测之书》里有一段很有意思的话是这么写的: 在智能社会,我们的行为方式和社交模式会更像古代的君子。对君子来说,重要的是少做事,多立范式;少拼力气,多守分寸;少讲辛苦,多讲证据链与担当。 你们怎么看呢?

今天上午处理了一个小时工作,学习了一个单元的idom(出自Speak English Like an American);下午学习了 类 的部分内容,笔记如下:

  面向对象编程(object-oriented programming,OOP),编写表示显示世界中的事物和情景的类(class),并基于这些类来创建对象(object)-实例化。

1. 创建类

dog.py
class Dog:
	"""一次模拟小狗的简单尝试"""
	def __init__(self,name,age):
		"""初始化属性 name 和 age"""
		self.name = name
		self.age = age
		
	def sit(self):
		"""模拟小狗收到命令时坐下"""
		print(f"{self.name} is now sitting.")
	
	def roll_over(self):
		"""模拟小狗收到命令时打滚"""
		print(f"{self.name} rolled over!")
__init__(self,形参1,形参2,....)
  • self.name = name 获取与形参name相关联的值,并将其赋给变量name,像这样可通过实例访问的变量称为属性

2. 根据类创建实例

创建特定小狗的实例
class Dog:
	--snip--
my_dog=Dog('Willie',6)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
  • 访问属性 通过点号来访问实例的属性
my_dog.name
  • 调用方法 创建实例后,用点号来调用类中定义的任何方法
class Dog:
	--snip--
my_dog = Dog('Willie',6)
my_dog.sit()
my_dog.roll_over()

# 》Willie is now sitting.
# 》Willie rolled over!
  • 创建多个实例
class Dog:
	--snip--
my_dog = Dog('Willie',6)
your_dog = Dog('Lucy',3)

练习

  • 餐馆
class Restaurant:
	"""尝试一个定义餐馆的类"""
	def __init__(self,restaurant_name,restaurant_type):
		self.restaurant_name = restaurant_name
		self.restaurant_type = restaurant_type
	
	def describe_restaurant(self):
		print(f"{self.restaurant_name}.")
		print(f"{self.restaurant_type}.")
	
	def open_restaurant(self):
		print(f"The {self.restaurant_name} is open.")

restaurant = Restaurant("Shaqimu","kaoyadian")
print(f"The restaurant's name is {restaurant.restaurant_name}.")
print(f"The restaurant's type is {restaurant.restaurant_type}.")
restaurant.describe_restaurant()
restaurant.open_restaurant()

# 》The restaurant's name is Shaqimu.
# 》The restaurant's type is kaoyadian.
# 》Shaqimu.
# 》kaoyadian.
# 》The Shaqimu is open.

class Restaurant:
	"""调用类"""
	--snip--
restaurant1 = Restaurant('Yangguofu','malangtang')
restaurant2 = Restaurant('Pengpuyizha','zhachuandian')
restaurant3 = Restaurant('Haidilao','Hotpot')

restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()	
  • 用户
class User:
	"""创建一般用户类"""
	def __init__(self,first_name,last_name,location,age):
		self.first_name = first_name
		self.last_name = last_name
		self.location = location
		self.age = age
		
	def describe_user(self):
		print(f"The user's name is {self.first_name.title()} {self.last_name.title()}."
              f"\n{self.first_name.title()} lives in {self.location.title()}."
              f"\n{self.first_name.title()} is {self.age} years old.")
	
	def greet_user(self):
		print(f"Hello,{self.first_name.title()}.")

user1 = User('tracy','an','shanghai',34)
user2 = User('coco','cai','shanghai',8)
user3 = User('menglei','cai','shanghai',33)

user1.describe_user()
user1.greet_user()
print()

user2.describe_user()
user2.greet_user()
print()

user3.describe_user()
user3.greet_user()
# 》"""
The user's name is Tracy An.
Tracy lives in Shanghai.
Tracy is 34 years old.
Hello,Tracy.

The user's name is Coco Cai.
Coco lives in Shanghai.
Coco is 8 years old.
Hello,Coco.

The user's name is Menglei Cai.
Menglei lives in Shanghai.
Menglei is 33 years old.
Hello,Menglei."""