美文网首页
2018-10-19 Day15作业

2018-10-19 Day15作业

作者: 佐手牵鼬手_89a9 | 来源:发表于2018-10-19 09:55 被阅读0次

0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()

class Score:
    def  __init__(self,chinese,math,english):
        self.chinese = int(chinese)
        self.math = int(math)
        self.english = int(english)

class Student:
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.scores = []
    def get_name(self):
        print('姓名:%s'%self.name)
    def get_age(self):
        print('年龄:%s'%self.age)
    def get_course(self):
        score = Score(66,77,88)
        new_score = score.__dict__
        self.scores.append(new_score)
        return max(score.english,score.math,score.chinese)


stu1 = Student('张三',18)
stu1.get_name()
stu1.get_age()
print(stu1.get_course())

1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,
并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。
再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量 覆盖加速 减速的方法

class Auto:
    def __init__(self,tires,color,weight,speed):
        self.tyre = tires
        self.color = color
        self.weight = weight
        self.speed = speed
    @staticmethod
    def speed_up():
        print('加速')

    @staticmethod
    def slow_down():
        print('减速')

    @staticmethod
    def stop():
        print('停车')
class CarAuto(Auto):
    def __init__(self,air_conditioner,CD,tires,color,weight,speed):
        super().__init__(tires,color,weight,speed)
        self.air_conditioner=air_conditioner
        self.CD=CD

    @staticmethod
    def speed_up():
        print('我是CarAuto,正在加速!')

    @staticmethod
    def slow_down():
        print('我是CarAuto,正在减速!')
p1=CarAuto('美的','天空之城',4,'黑色','1kg','80km/h')
print(p1.__dict__)


2.创建一个名为User 的类,其中包含属性firstname 和lastname ,还有用户简介通常会存储的其他几个属
性。在类User 中定义一个名 为describeuser() 的方法,它打印用户信息摘要;
再定义一个名为greetuser() 的方法,它向用户发出个性化的问候。
管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承User类。
添加一个名为privileges 的属性,用于存储一个由字符串
(如"can add post"、"can delete post"、"can ban user"等)组成的列表。
编写一个名为show_privileges()的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。

class User:
    def __init__(self,age,sex):
        self.firstname = '张三'
        self.lastname = '李四'
        self.age= age
        self.sex =sex
    def describeuser(self):
        print(self.__dict__)

    @staticmethod
    def greetuser():
        print('你们好呀么么哒')



3.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数

class Person():
    number = 0
    def __init__(self):
        Person.number += 1
p1 = Person()
p2 = Person()
print(Person.number)

(尝试)5.写一个类,其功能是:
1.解析指定的歌词文件的内容
2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。
歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
'''

class Lyric:
    def __init__(self,time,word):
        self.time=time
        self.word=word
    def __gt__(self, other):
        return self.time > other.time
class Analysis:
    def __init__(self):
        self.all_lyric=[]
        self.collect_lyric()
    def get_time_word(self,content):
        contents=content.split(']')
        word=contents[-1]
        for time in contents[:-1]:
            times = time[1:].split(':')
            fen = float(times[0])
            miao = float(times[1])
            new_time = fen*60 + miao
            lyric=Lyric(new_time,word)
            self.all_lyric.append(lyric)

    def collect_lyric(self):
        try:
            with open('./蓝莲花.txt','r',encoding='utf-8') as f:
                line=f.readline()
                while line:
                    self.get_time_word(line)
                    line=f.readline()
                self.all_lyric.sort(reverse=True)
        except FileNotFoundError:
            print('文件不存在')
    def get_word(self,time):
        for lyric in self.all_lyric:
            if lyric.time <=time:
                return lyric.word

an1 = Analysis()
print(an1.get_word(17))





相关文章

网友评论

      本文标题:2018-10-19 Day15作业

      本文链接:https://www.haomeiwen.com/subject/tttyzftx.html