day-12

作者: FansYuercero | 来源:发表于2018-07-31 20:34 被阅读0次

1.声明一个电脑类:
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性

class Computer:
    def __init__(self,species,color,cpu_size):
        self.species = species
        self.color = color
        self.cpu_size = cpu_size

    def fun1(self):
        print('play game')

    def fun2(self):
        print('watch video')

    def fun3(self):
        print('write code')

if __name__ == '__main__':
    computer = Computer('Hp','black','8G')
    print('this computer\'s species is %s,it\'s color is %s,it\'s cpu-size is %s'\
          %(computer.species,computer.color,computer.cpu_size))
    computer.fun1()
    computer.fun2()
    computer.fun3()
    computer.color='red'
    computer.species='apple'
    computer.cpu_size='4G'
    print('the computer\'value afer updated is %s %s %s'\
          %(computer.species,computer.color,computer.cpu_size))
    # 查
    print(computer.__getattribute__('species'))
    print(getattr(computer,'species'))
    # 修改
    computer.__setattr__('color','green')
    print(computer.color)
    setattr(computer,'color','yellow')
    print(computer.color)
    # 增加 setattr方法添加不存在的属性就是增加
    computer.__setattr__('price','10000')
    print(computer.price)
    setattr(computer,'borndate','2018-4-30')
    print(computer.borndate)
    # 删除 del 与delattr
    del computer.color
    computer.__delattr__('species')
    delattr(computer,'cpu_size')

this computer's species is Hp,it's color is black,it's cpu-size is 8G
play game
watch video
write code
the computer'value afer updated is apple red 4G
apple
apple
green
yellow
10000
2018-4-30

2.声明一个人的类和狗的类:
狗的属性:名字、颜色、年龄 狗的方法:叫唤
人的属性:名字、年龄、狗 人的方法:遛狗
a.创建人的对象小明,让他拥有一条狗大黄,然后让小明去遛大黄

class Dog:
    def __init__(self,name,color,age):
        self.name = name
        self.color = color
        self.age = age
    def barking(self):
        print('wang wang wang')
class Person:
    def __init__(self,name,age,dog):
        self.name = name
        self.age = age
        self.dog = dog
    def walk_dog(self):
        dog = Dog('大黄','黄色','5')
        dog.barking()
        print('小明正在遛他的宠物狗%s,大黄是%s的,今年%s岁了'%(dog.name,dog.color,dog.age))
xiaoming = Person('小明',12,'大黄')
xiaoming.walk_dog()

wang wang wang
小明正在遛他的宠物狗大黄,大黄是黄色的,今年5岁了

3.声明一个矩形类:
属性:长、宽 方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积

class Rect:
    def __init__(self,len,width):
        self.len = len
        self.width = width
    def eara(self):
        print('面积是%.2f'%(self.width*self.len))
    def perimeter(self):
        print('周长是%.2f'%((self.len+self.width)*2))
r1 = Rect(10,8)
r1.eara()
r1.perimeter()
r2 = Rect(6,7)
r2.eara()
r2.perimeter()

面积是80.00
周长是36.00
面积是42.00
周长是26.00

4.创建一个学生类:
属性:姓名,年龄,学号 方法:答到,展示学生信息
创建一个班级类:
属性:学生,班级名 方法:添加学生,删除学生,点名

from random import  randint
class Student:
    def __init__(self,name,age,id):
        self.name = name
        self.age = age
        self.id = id
    def __str__(self):
        return str(self.name+','+str(self.age)+','+self.id)
    def replied(self):
        print('到')
class Class:
    def __init__(self,name,student=[]):
        self.name = name
        self.student = student
    def add_student(self):
        name = input('input student\'s name')
        age = input('input student\'s age')
        id = 'py1805'+str(randint(1,100)).rjust(3,'0')
        stu = Student(name,age,id)
        self.student.append(stu)
    def all_student(self):
        for item in self.student:
            print(item)
    def del_student(self):
        info = input('input the student\'s name or id')
        for item in self.student[:]:
            if item.name ==info or item.id == info:
                self.student.remove(item)
    def dianming(self):
        name = input('input student\'s name')
        for item in self.student:
            if name == item.name:
                stu = Student(item.name,item.age,item.id)
                stu.replied()
            

classone = Class('1班')
classone.add_student()
classone.add_student()
classone.all_student()
classone.del_student()
classone.all_student()
classone.dianming()

input student's name1
input student's age1
input student's name2
input student's age2
1,1,py1805053
2,2,py1805051
input the student's name or id1
2,2,py1805051
input student's name2
到

5.写一个类,封装所有和数学运算相关的功能(包含常用功能和常用值,例如:pi,e等)

class Math:
    pi = 3.14159265357826
    e = 2.718

    @staticmethod
    def sum(*number):
        sum1 = 0
        for item in number:
            sum1+=item
        return sum1
    @classmethod
    def circle(cls,rudius):
        return cls.pi*rudius*rudius

print(Math.sum(10,2,3))
print(Math.circle(5))

6.1.写一个班级类,属性:班级名、学生;功能:添加学生、删除学生、根据姓名查看学生信息,展示班级的所有学生信息


from random import randint
class Student:
    def __init__(self,name,age,id,sex):
        self.name = name
        self.age = age
        self.sex = sex
        self.id = id
    def __str__(self):
        return str('name is %s,age is %d,id is %s,sex is %s'%(self.name,self.age,self.id,self.sex))



class Class:
    def __init__(self,name='',students=[]):
        self.class_name = name
        self.students = students
    def add_student(self):
        name = input('student\'name')
        age = input('student\'age')
        sex = input('student\'sex')
        id = 'py1805'+str(randint(1,100)).rjust(3,'0')
        # 根据输入的信息创建学生对象
        stu = Student(name,int(age),id,sex)
        #添加学生 self得到Class的对象属性
        self.students.append(stu)

    def check_student(self):
        input_info = input('input the student\'s name or id')
        for item in self.students:
            if input_info == item.name or input_info == item.id:
                print(item)
    def check_all_student(self):
        for item in self.students:
            print(item)
    def del_student(self):
        input_info = input('input the student\'s name or id')
        for item in self.students[:]:
            if input_info == item.name or input_info == item.id:
                self.students.remove(item)


# cls1 = Class('py1805')
# cls1.add_student()
# print(cls1.students[0])
# cls1.check_student()
# cls1.del_student()
# print(cls1.students)

while True:
    print('*'*50)
    clsone = Class('py1805')
    print('1.添加学生\n2.查找学生\n3.删除学生\n4.查看所有学生\nq.退出')
    choose = input('>>>')
    if choose=='1':
        while True:
            clsone.add_student()
            print('1.继续添加\n2.返回上级')
            choose1 = input('>>>')
            if choose1 !='1'or choose1=='2':
                break
            continue

    elif  choose=='2':
        while  True:
            clsone.check_student()
            print('1.继续查看\n2.返回上级')
            choose2 = input('>>>')
            if choose2 != '1' or choose2 == '2':
                break
            continue
    elif choose =='3':
        while True:
            clsone.del_student()
            print('1.继续删除\n2.返回上级')
            choose3 = input('>>>')
            if choose3 != '1' or choose3 == '2':
                break
            continue
    elif choose=='4':
        while True:
            clsone.check_all_student()
            print('1.返回上级')
            choose4 = input('>>>')
            if choose4==1:
                break
            break
    else:
        break

**************************************************
1.添加学生
2.查找学生
3.删除学生
4.查看所有学生
q.退出
>>>
1
student'name1
student'age1
student'sex1
1.继续添加
2.返回上级
1
student'name2
student'age2
student'sex2
1.继续添加
2.返回上级
2
1.添加学生
2.查找学生
3.删除学生
4.查看所有学生
q.退出
>>>2

input the student's name or id2
name is 2,age is 2,id is py1805021,sex is 2
1.继续查看
2.返回上级
2
1.添加学生
2.查找学生
3.删除学生
4.查看所有学生
q.退出
>>>4
name is 1,age is 1,id is py1805063,sex is 1
name is 2,age is 2,id is py1805021,sex is 2
1.返回上级
1
1.添加学生
2.查找学生
3.删除学生
4.查看所有学生
q.退出
>>>3
input the student's name or idpy1805021
1.继续删除
2.返回上级
>>>2
1.添加学生
2.查找学生
3.删除学生
4.查看所有学生
q.退出
>>>4

name is 1,age is 1,id is py1805063,sex is 1
1.返回上级
>>>

相关文章

  • day-12

    1.声明一个电脑类:属性:品牌、颜色、内存大小方法:打游戏、写代码、看视频a.创建电脑类的对象,然后通过对象点的方...

  • DAY-12

  • 2018-12-31 再见,2018

    #2018超级冲刺打卡# Day-12 【学员昵称】:栀子花的秋天 【学习内容】:日期函数与PPT 【学习心得】:...

  • 快书速读:《当下的力量(下)》

    【大螺丝】打卡 | 快书速读 Day-12 一句总结: 抱怨、懒惰、压力、忧虑、等待都属于“无意识”的状态,这些状...

  • day-12 总结

    生成式 1.什么是生成式生成式就是生成器一种固定写法 2.写法a.生成器 = (表达式 for 变量 in 序列...

  • DAY-12 感恩

    用水杯喝水时,不可避免要仰起头,有时水杯口会残留一些水,随着水杯被抬升,角度的变化经常使得水滴落下来,滴在衣服上,...

  • 2017.04.22智慧存款第11天

    DAY-12 2017.04.22 老婆,你是一个贤惠的妻子,在我们不大的房间里面,你总能找到办法让我们的空间可以...

  • 大白第12天

    DAY-12 2022.05.23 今天协助淮海街道社区卫生服务中心采样,这个街道我是第一次来,环境很好,还有专门...

  • 拓展弱联系

    《请停止无效努力》Day-12 共读内容:第四章 提升沟通能力——如何成为高段位沟通者 第4节 拓展弱联系——最有...

  • [Python] (Day-12) - 函数

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段 函数能提高应用的模块性,和代码的重复利用率 函数...

网友评论

      本文标题:day-12

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