Python实战:类

一、圆的面积、周长

python 复制代码
class Circle:
    # 初始化一个类参数:r
    def __init__(self,r):
        self.r = r

    # 计算面积的方法
    def get_area(self):
        return 3.14*pow(self.r,2)

    # 计算周长的方法
    def get_perimeter(self):
        return 2*3.14*self.r

#创建对象
r = eval(input('请输入圆的半径:'))
c = Circle(r)

#调用方法
area = c.get_area() #调用计算面积的方法
perimeter = c.get_perimeter() #调用计算周长的方法
print('圆的面积为:',area)
print('圆的周长为:',perimeter)

运行结果:

二、定义学生类录入5个学生信息存储到列表

python 复制代码
# 姓名、年龄、性别、分数、学生信息输出的方法
class Student:
    def __init__(self,name,age,sex,score):
        self.name = name
        self.age = age
        self.sex = sex
        self.score = score

    def info(self):
        print("姓名:",self.name,",年龄:",self.age,",性别:",self.sex,",分数:",self.score)




print('请输入5位学生的信息:(姓名#年龄#性别#成绩)')
lst = [] # 用于存储5个学生对象
for i in range (0,5):
    s = input(f'请输入学生第{i+1}位学生的信息及成绩:')
    s_lst = s.split('#') # 这个列表中索引为0的是姓名,1:年龄,2:性别 3:成绩
    # 创建学生对象
    stu = Student(s_lst[0],s_lst[1],s_lst[2],s_lst[3])
    lst.append(stu)

#遍历列表,调用info方法
for item in lst:
    item.info()

运行结果:

三、使用面向对象思想实现乐器弹奏

python 复制代码
# 父类
class Instrument:
    def make_sound(self):
        pass

#Erhu集成Instrument类
class Erhu(Instrument):
    #重写父类方法
    def make_sound(self):
        print('二胡在弹奏')

class Piano(Instrument):
    def make_sound(self):
        print('钢琴在弹奏')

class Violin(Instrument):
    def make_sound(self):
        print('小提琴在弹奏')


#编写函数
def play(obj):
    obj.make_sound()

er = Erhu()
piano = Piano()
vio = Violin()

#调用方法
play(er)
play(piano)
play(vio)

运行结果:

四、使用面向对象思想,设计自定义类,描述出租车和家用轿车的信息

python 复制代码
class Car(object):
    def __init__(self,type,no): # 车型、车牌号
        self.type = type
        self.no = no

    def start(self):
        print('我是车,我能启动')

    def stop(self):
        print('我是车,我能停止')

class Taxi(Car):
    def __init__(self, type, no,company):
        super().__init__(type, no)
        self.company = company
    #重写父类的启动和停止方法
    def start(self):
        print('乘客您好!')
        print(f'我是{self.company}c出租车公司,我的车牌是:{self.no},您要去哪里?')

    def stop(self):
        print('目的地到了,请您扫码付款,欢迎下次乘坐')

class FamilyCar(Car):
    def __init__(self, type, no,name):
        super().__init__(type, no)
        self.name = name

    def start(self):
        print(f'我是{self.name},我的轿车我做主')

    def stop(self):
        print('目的地到了,我们去玩吧')


#编写测试
taxi = Taxi('上海大众','9999','长城')
taxi.start()
taxi.stop()
family_car = FamilyCar('上海大众','9999','张三')
family_car.start()
family_car.stop()
相关推荐
小龙在山东22 分钟前
Python 包管理工具 uv
windows·python·uv
weixin_3077791343 分钟前
批量OCR的GitHub项目
python·github·ocr
孤狼warrior2 小时前
灰色预测模型
人工智能·python·算法·数学建模
神仙别闹2 小时前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
机器学习之心2 小时前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金2 小时前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python
站大爷IP3 小时前
Python办公自动化实战:手把手教你打造智能邮件发送工具
python
chao_7893 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
zdw3 小时前
fit parse解析佳明.fit 运动数据,模仿zwift数据展示
python
剑桥折刀s4 小时前
Python打卡:Day46
python