python基础(类、实例、属性、方法)
1. 类、实例
类创建的实体叫对象
# 语法
class ClassName(object):
# 具体内容
def __init__(self): # self代表实例化对象
print(“这是一个初始化函数”)
# 实例化
cl = ClassNmae()
1. 实例的属性
class ClassName(object):
# 具体内容
def __init__(self): # self代表实例化对象
self.name = name
self.age = age
cl = ClassName()
print(cl.__dict__) # 查看所有的属性
2. 实例的方法
# 实例的方法,就是类中的函数
3. 类属性
class Name:
name = X
4. 类方法
# 对于所有的实例做处理
@classmathod
def get_name(cls):
pass
5. 静态方法
@staticmethod
def isvalid(**kwargs):
# 对实例传入的参数做校验
2. 面向对象的特点
1. 继承
# 子类可以继承父类的东西
class Name(父类):
def __init__(self,xx):
super().__init__(xx)
self.time = time # 子类重写属性
def again(self):
#重写父类的方法
2. 多态
同一个方法,可以处理不同的数据
len(str)
len(list)
3. 封装
变量的声明
单_声明变量不要随意调用
双__变量私有化,外面不能调用,只能在类里面自己调用
# 封装调用函数
# 在外部将函数当成变量使用
@property
def func():
pass
#变量的修改器
def age():
return self.__age
@age.setter
def age(self.age):
self.__age = age
class Player:
def __init__(self,name,age):
self.name = name
self.age = age
@property
def xm(self):
return self.name
@xm.setter
def xm(self,change_name):
self.name = change_name
4. 魔法方法
# __init__魔法函数
5. 闭包
ef wrapper(fn):
def inner(*args,**kargs):
print("函数执行之前执行")
fn(*args,**kargs)
print("函数执行后执行")
return inner
@wrapper
def func2(name,pwd):
print(f"账户{name},密码{pwd}")
func2('Jack',7432)
简单项目
'''
学生
班级
老师
课程
'''
# 信息基类
class User:
def __init__(self,name,sex,age,id_number):
self.name = name
self.sex = sex
self.age = age
self.id_number = id_number
def show_infos(self):
print(f"{26 *'*'}\n姓名:{self.name}\n性别:{self.sex}\n年龄:{self.age}\n学(工)号:{self.id_number}")
class Student(User):
# 属性:姓名、性别、年龄、学号
def __init__(self,name,sex,age,id_number,curse):
super().__init__(name,sex,age,id_number)
self.curse = curse
# 查看信息
def show_infos(self):
super().show_infos()
print("已选课程:%s" %self.curse)
# 添加课程
def add_curse(self,curse):
if curse:
self.curse.append(curse)
else:
print("课程不能为空")
class Teacher(User):
# 属性:姓名、年龄、性别、工号、是否是导员、班级列表
def __init__(self,name,sex,age,id_number,dao,cla):
super().__init__(name,sex,age,id_number)
self.dao = dao
self.cla = cla
def show_infos(self):
super().show_infos()
print(f"是否是导师:{self.dao}")
if self.cla:
for i in self.cla:
print(i,end=" ")
else:
raise Exception("不存在班级信息")
print()
class Cla:
# 属性:班级名称、班级号、辅导员、学生
def __init__(self,name,id_number,teacher,student):
self.name = name
self.id_number = id_number
self.teacher = teacher
self.student = student
# 查看班级信息
def show_infos(self):
print(f"{10 * '*'}班级信息{10 * '*'}\n班级:{self.name}\n班号:{self.id_number}")
print(f"老师姓名:{self.teacher.name}")
print("学生信息:",end="")
if self.student:
for i in self.student:
print(i.name,end=' ')
else:
print("班级不存在学生信息")
print()
# 添加学生信息
def add_student(self,student):
if student in self.student:
print(f"班级存在{student.name}学生信息,不需要添加")
else:
self.student.append(student)
# 删除学生信息
def del_student(self,student):
if student not in self.student:
print(f"班级不存在{student.name}信息,无需删除")
else:
self.student.remove(student)
class Curse:
curses = []
# 属性:课名、课程id、老师、学生列表、课程性质、课程容量
def __init__(self,name,id_number,teacher,student,must,numbers):
self.name = name
self.id_number = id_number
self.teacher = teacher
self.student = student
self.must = must
self.numbers = numbers
Curse.curses.append(self.name)
for i in self.student:
i.add_curse(self.name)
def show_infos(self):
print(f"{10 * '*'}课程信息{10 * '*'}\n课程名字:{self.name}\n课程id:{self.id_number}\n课程老师:{self.teacher.name}\n",end="")
print("学生信息:",end="")
if self.student:
for i in self.student:
print(i.name,end=" ")
else:
print("学生信息为空")
print()
print("课程属性:%s" %self.must)
print("可选人数:%d" % self.numbers)
# 已选人数
students = len(self.student)
# 剩余可选人数
chose = self.numbers - students
print("课程已选:%d 人" %students)
print("课程剩余:%d" %chose)
# 课程新增学生
def add_student(self,student):
if student not in self.student:
self.student.append(student)
self.student.add_curse(self.name)
else:
print(f"课程已经添加学生{student.name}")
# 课程删除学生
def sub_student(self,student):
if student in self.student:
self.student.remove(student)
else:
print(f"学生{student.name}未选择课程{self.name}")
# 课程列表
@classmethod
def curses_info(self):
return Curse.curses
# 学生tom
tom = Student('Tom','男',18,1159,[])
# 学生jack
jack = Student('Jack','男',28,1160,[])
# 老师jarry
jarry = Teacher('Jarry','男',32,10010,True,["计算机一班","计算机三班","计算机五班"])
# 老师mia
mia = Teacher("Mia",'女',18,10011,False,["计算机二班"])
# 班级1
computer_cla1 = Cla("计算机三班",10086,jarry,[tom])
# 班级2
computer_cla2 = Cla("计算机二班",10087,mia,[jack])
# python课程
python = Curse("python课程",150,jarry,[tom],"必修",10)
java = Curse("java课程",151,jarry,[tom,jack],"必修",5)
tom.show_infos()
# java课程