Python面向对象简单的练习题
1.定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值。
比如:属性:“红色”,“奔驰”,“黑A00000”,15000000
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()
2.定义一个球员类(Player),属性有身高,体重,姓名,实例化两个球员,分别是姚明和科比;
比如:2.2,200,“姚明”
比如:2.25,225,"科比
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()
3.定义一个僵尸类(Zombie),属性有名子,体力值,攻击力,实例化三个僵尸类,并给属性赋值;
比如:
“大傻”,100,10
“二傻",80,40
“三傻",60,20
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()
4.设计一个立方体类Box,定义三个属性,分别是长,宽,高。定义二个方法,分别计算并输出立方体的体积和表面积。
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()