class car():
def __init__(self,color,brand,plate,price):
self.color = color
self.brand = brand
self.plate = plate
self.price = price
def driver(self):
print(self.color,self.brand,self.plate,self.price)
c = car("红色","奔驰","黑A00000",15000000)
c.driver()
class player():
def __init__(self,high,weight,name):
self.high = high
self.weight = weight
self.name = name
def man(self):
print(self.high,self.weight,self.name)
p1 = player(2.2,200,"姚明")
p2 = player(2.25,225,"科比")
p1.man()
p2.man()
class zombie():
def __init__(self,name,stamina,attack):
self.name = name
self.power = power
self.attack = attack
def dead(self):
print(self.name,self.power,self.attack)
d1 = zombie("大傻",100,10)
d2 = zombie("二傻",80,40)
d3 = zombie("三傻",60,20)
d1.dead()
d2.dead()
d3.dead()
class box():
def __init__(self,length,width,high):
self.length = length
self.width = width
self.high = high
def volume(self):
print(self.length*self.width*self.high)
def superficial(self):
print(2*(self.length*self.width+self.length*self.high+self.width*self.high))
b1 = box(1,1,1)
b1.volume()
b1.superficial()
class Vehicle():
def __init__(self,speed,size):
self.__speed = speed #初始化
self.size = size
def move(self,s):
print("移动了%s"%s)
def setSpeed(self,speed):
if str(speed).isdigit():
self.__speed = speed #赋值
else:
print("请输入正确速度")
def speedUp(self):
self.__speed += 10
print("当前速度",self.__speed)
def speed_Down(self):
self.__speed -= 10
print("当前速度",self.__speed)
def test():
v = Vehicle(30,15)
v.move(20)
v.setSpeed(50)
v.speedUp()
v.speedUp()
v.speed_Down()
if __name__ == "__main__":
test()
class Hero():
def __init__(self,name):
self.__name = name
self.__power = 100
def go(self):
if self.__power<=0:
print("不能行走,此英雄已死亡")
else:
print("英雄前进")
def eat(self,n):
self.__power += n
if self.__power>100:
self.__power = 100
print("当前体力值:",self.__power)
def hurt(self):
self.__power -= 10
if self.__power<=0:
self.__power = 0
print("此英雄死亡")
print("当前体力值:",self.__power)
h = Hero("阿大撒")
h.go()
for i in range(10):
h.hurt()
h.eat(120)
class Person():
def __init__(self,name,age):
self.__name = name
self.__age = age
def display(self):
print("姓名:%s,年龄%d"%(self.__name,self.__age))
def test():
p = Person("李宏宇",18)
p.display()
test()
class WuMingFen():
def __init__(self, theMa, quantity, likeSoup):
self.__theMa = theMa
self.__quantity = quantity
self.__likeSoup = likeSoup
print("三个参数")
def __init__(self, theMa, quantity, likeSoup):
self.__theMa = theMa
self.__quantity = quantity
print("两个参数")
def show_info(self):
print(self.__theMa,self.__quantity)
wmf = WuMingFen("大妈",10,True)
wmf.show_info()
定义一个名为Vehicles(交通工具)的基类,该类中应包含str类型的成员属性brand(商标)和
color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和
showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。
编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),
还应增加成员方法showCar(在控制台显示小汽车的信息),并编写构造方法。
编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),
还应增加成员方法showTruck(在控制台显示卡车的信息),并编写构造方法。
在测试方法中测试以上各类。
class Vehicles():
def __init__(self,brand,color):
self.__brand = brand #_Vehicles__brand
self.__color = color
def run(self):
print("我已经开动了")
def showInfo(self):
print(self.__brand,self.__color)
class Car(Vehicles):
def __init__(self,brand,color,seats):
super().__init__(brand,color)
self.__seats = seats
def showCar(self):
self.showInfo()
print(self.__seats)
class Truck(Vehicles):
def __init__(self,brand,color,load):
super().__init__(brand,color)
self.__load = load
def showTruck(self):
self.showInfo()
print(self.__load)
c = Car("7Q","绿色",5)
c.run()
c.showCar()
t = Truck("五菱宏光","蓝色",6.66)
t.run()
t.showTruck()
3.继承:
建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期,而成人继承自居民,
多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
要求每个类的字段都以属性的方式对外提供数据输入输出的功能。
class jumin():
def __init__(self,idcard,name,birthday):
self.__idcard = idcard
self.__name = name
self.__birthday = birthday
def get_name(self):
return self.__name
def set_name(self,name):
self.__name = name
class chengren(jumin):
def __init__(self,idcard,name,birthday,xueli,job):
super().__init__(idcard,name,birthday)
self.__xueli = xueli
self.__job = job
class guanyuan(chengren):
def __init__(self, idcard, name, birthday, xueli, job,dangpai,zhiwu):
super().__init__(idcard,name,birthday,xueli,job)
self.__dangpai = dangpai
self.__zhiwu = zhiwu
gy = guanyuan("123","lhy","1998-1-23","博士","python教授","民主","科员")
name = gy.get_name()
print(name)
1.编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。
然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,
并能求出平均成绩。最后在测试函数中对student类的功能进行验证。
class person():
def __init__(self,name,age,sex):
self.__name = name
self.__age = age
self.__sex = sex
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
def get_sex(self):
return self.__sex
def set_sex(self, sex):
self.__sex = sex
class student(person):
def __init__(self,name,age,sex,*mylist):
super().__init__(name,age,sex)
self.__mylist = mylist
def get_mylist(self):
return self.__mylist
def set_mylist(self,*mylist):
self.__mylist = mylist
def myavg(self):
sum = 0
for i in self.__mylist:
sum+=i
return sum/len(self.__mylist)
s = student("hjx",18,"男",58,59,57,56,53)
s.set_mylist(1,2,3,4,5)
myavg = s.myavg()
print(myavg)
print(s.get_name())