第二阶段 第一章 面向对象

1 类

1.1 类的成员方法

1.1.1 类的成员

  • 设计表格,称之为:设计类(class)
  • 打印表格,称之为:创建对象
  • 填写表格,称之为:对象属性赋值
python 复制代码
# 1.设计类(类比生活中: 设计一张登记表)
class Student:	
	name = None				#记录学生姓名
	gender = None			#记录学生性别
	nationality = None		#记录学生国籍
	native_place = None		#记学生籍贯
	age = None

# 2.创建对象(类比生活中: 打印一张登记表)
stu_1 = Student()

# 3.对象属性赋值(类比生活中:填写表单)
stu_1.name = "林俊杰"
stu_1.gender = "男"
stu_1.nationality = "中国"
stu_1.native_place = "山东省"
stu_1.age = 31

# 4. 获取对象中记录的信息
print(stu_1.name)
print(stu_1.gender)
print(stu_1.nationality)
print(stu_1.native_place)
print(stu_1.age)
python 复制代码
林俊杰
男
中国
山东省
31

1.1.2 类的方法

python 复制代码
# 定义一个带有成员方法的类
class Student:
	name = None
	
	def say_hi(self):
		print(f"大家好呀,我是{self.name},欢迎大家多多关照")
	def say_hi2(self, msg):
		print(f"大家好呀,我是{self.name},{msg}")

stu = Student()
stu.name = "周杰伦"
stu.say_hi()
stu.say_hi2("哎呦不错哟")

stu2 = Student()
stu2.name = "林俊杰"
stu2.say_hi("小伙子我看好你")

1.2 类和对象

python 复制代码
# 设计一个闹钟类
class Clock:
	id = None
	price = None
	
	def ring(self):
		import winsound
		winsound.Beep(2000, 3000) #2000表示频率,3000表示持续时间(毫秒)
		
# 构建2个闹钟对象并让其工作
clock1 = Clock()
clock1.id = "003032"
clock1.price = 19.99
print(f"闹钟ID:{clock1.id},价格:{clock1.price}")
clock1.ring()

clock2 = Clock()
clock2.id = "003033"
clock2.price = 21.99
print(f"闹钟ID:{clock1.id},价格:{clock1.price}")
clock2.ring()

1.3 构造方法__init__

python 复制代码
# 构造方法的名称:__init__
class Student:	
	def __init__(self, name, age, tel):
		self.name = name
		self.age = age
		self.tel = tel
		print("Student类创建了一个类对象")
stu = Student("李明", 12, 12345)
print(self.name)
print(self.age)
print(self.tel)
python 复制代码
# 构造方法的名称:__init__
class Student:	
	name = None
    age - None
    tel = None
	def __init__(self, name, age, tel):
		self.name = name
		self.age = age
		self.tel = tel
		print("Student类创建了一个类对象")
stu = Student("李明", 12, 12345)
print(self.name)
print(self.age)
print(self.tel)

1.4 案例 学生信息录入

python 复制代码
class Student:
	def __init__(self, name, age, address):
		self.name = name
		self.age = age
		self.address = address
stu_list = []
count = input()
for i in range(1, count + 1):
	print(f"当前录入第{i}位学生信息,总共需录入{count}位学生信息")
	stu_name = input("请输入学生姓名:")
	stu_age = input("请输入学生年龄:")
	stu_add = input("请输入学生地址:")
	stu = Student(stu_name, stu_age, stu_add)
	stu_list.append(stu)
	stu_data = stu_list[i - 1]
	print(f"学生1信息录入完成,信息为:【学生姓名:{stu_data.name},年龄:{stu_data.age},地址:{stu_data.address}】")

1.5 魔术方法

1.5.1 str

python 复制代码
class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age
	def __str__(self):
		return f"Student类对象,name:{self.name},age:{self.age}"
	
stu = Student("李明", 12)
print(stu)
print(str(stu))

1.5.2 lt

python 复制代码
class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age
	def __lt__(self, other):
		return self.age < other.age
	
stu1 = Student("李明", 12)
stu2 = Student("林俊杰", 14)
print(stu1 < stu2)

1.5.3 le

python 复制代码
class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age
	def __le__(self, other):
		return self.age <= other.age
	
stu1 = Student("李明", 14)
stu2 = Student("林俊杰", 12)
print(stu1 >= stu2)

1.5.4 eq

python 复制代码
class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age
	def __eq__(self, other):
		return self.age <= other.age
	
stu1 = Student("李明", 14)
stu2 = Student("林俊杰", 14)
print(stu1 == stu2)
相关推荐
hayzone几秒前
pnpm 你用了吗?
前端
墨雪不会编程2 分钟前
C++基础语法篇八 ——【类型转换、再探构造、友元】
java·开发语言·c++
hellsing5 分钟前
UniPush 2.0 实战指南:工单提醒与多厂商通道配置
前端·javascript
老毛肚7 分钟前
登录架构设计
java·开发语言
月明长歌8 分钟前
【码道初阶】【牛客BM30】二叉搜索树与双向链表:java中以引用代指针操作的艺术与陷阱
java·数据结构·算法·leetcode·二叉树·笔试·字节跳动
快手技术11 分钟前
入围AA总榜Top 10,Non-Reasoning Model榜单第一!KAT-Coder-Pro V1 新版本踏浪归来!
前端·后端·前端框架
小坏讲微服务11 分钟前
Spring Boot4.0整合RabbitMQ死信队列详解
java·spring boot·后端·rabbitmq·java-rabbitmq
wangpq12 分钟前
记录曾经打开半屏小程序遇到的事
前端·微信小程序
yuuki23323313 分钟前
【C++】内存管理
java·c++·算法
model200517 分钟前
Web 服务和 SFTP 用户 操作目录
linux·运维·服务器