练习题1.创建Person类,属性有姓名、年龄、性别,创建方法personInfo,打印这个人的信息
class Person():
def __init__(self,Name,Age,Sex):
self.Name=Name
self.Age=Age
self.Sex=Sex
def PersonInfo(self):
print("姓名:"+self.Name+" "
+"年龄:"+str(self.Age)+" "
+"性别:"+self.Sex+" ")
people=Person("Wong",23,"male")
people.PersonInfo()
练习题2.创建Student类,继承Person类,属性有学院college,班级class,重写父类personInfo方法,调用父类方法打印个人信息外,将学生的学院、班级信息也打印出来,
class Person():
def __init__(self,Name,Age,Sex):
self.Name=Name
self.Age=Age
self.Sex=Sex
def PersonInfo(self):
print("姓名:"+self.Name+" "
+"年龄:"+str(self.Age)+" "
+"性别:"+self.Sex+" ")
class Students(Person):
def __init__(self,Name,Age,Sex,College,Class,Tname,Tage,Tsex,Tcollege,Tprofessional):
super().__init__(Name,Age,Sex)
self.College=College
self.Class=Class
self.teacher=Teacher(Tname,Tage,Tsex,Tcollege,Tprofessional)
def PersonInfo(self):
print("姓名:"+self.Name+" "
+"年龄:"+str(self.Age)+" "
+"性别:"+self.Sex+" "
+"学院:"+self.College+" "
+"班级:"+str(self.Class)+" ")
def Study(self):
self.teacher.teachObj()
print("老师"+self.teacher.Name+"我终于学会了")
Stu=Students("Wong",23,"male","NetWorkring",5)
Stu.PersonInfo()
练习题3.创建Teacher类,继承Person类,属性有学院college,专业professional,重写父类personInfo方法,调用父类方法打印个人信息外,将老师的学院、专业信息也打印出来。
class Teacher(Person):
def __init__(self,Name,Age,Sex,College,Professional):
super().__init__(Name,Age,Sex)
self.College=College
self.Professional=Professional
def PersonInfo(self):
print("姓名:"+self.Name+" "
+"年龄:"+str(self.Age)+" "
+"性别:"+self.Sex+" "
+"学院:"+self.College+" "
+"专业:"+self.Professional+" ")
def teachObj(self):
print("今天讲了如何用面向对象设计程序")
练习题4.创建三个学生对象,分别打印其详细信息
stu1=Students("Wong",23,"male","NetWorkring",5,"Zhang",45,"male","NetWorking","Math")
stu2=Students("Yu",22,"male","NetWorking",5,"Zhang",45,"male","NetWorking","Math")
stu3=Students("Rui",20,"male","NetWorking",5,"Zhang",45,"male","NetWorking","Math")
stu1.PersonInfo()
stu2.PersonInfo()
stu3.PersonInfo()
练习题5.创建一个老师对象,打印其详细信息
teacher=Teacher("Zhang",45,"male","NetWorking","Math")
teacher.PersonInfo()
练习题6.学生对象调用Study方法
stu1.Study()
练习题7.将三个学员添加至列表中,通过循环将列表中的对象打印出来,print(Student对象)。
StuL=[stu1,stu2,stu3]
for i in range(0,3):
print(StuL[i].Name+" "+str(StuL[i].Age)+" "+StuL[i].Sex+" "+StuL[i].College+" "+str(StuL[i].Class))