[python](8)----面向对象练习题

这篇博客介绍了Python面向对象编程的概念,通过创建多个类如汽车、球员、僵尸、立方体、交通工具、英雄、人以及不同类型的车辆,展示了类的属性、方法、构造函数以及继承的应用。并提供了测试代码来演示这些类的实例化和操作。
摘要由CSDN通过智能技术生成
  1. 定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值
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()
  1. 定义一个球员类(Player),属性有身高,体重,姓名,实例化两个球员,分别是姚明和科比;
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()
  1. 定义一个僵尸类(Zombie),属性有名子,体力值,攻击力,实例化三个僵尸类,并给属性赋值;
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()
  1. 设计一个立方体类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.wi
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值