doy14

1.声明个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频

class Cumputer:
'''电脑,类的属性:品牌,颜色,内存大小,
方法:打游戏,写代码,看视频'''

def __init__(self, brand='', color='', memsize=''):
    self.brand = brand
    self.memsize = memsize
    self.color = color


def game(self):
    print('打游戏')


def code(self):
    print('写代码')


def video(self):
    print('看视频')

cpt1=Cumputer('opple','white','300G')
cpt2=Cumputer('dele','black','280G')
cpt3=Cumputer('lenovo','red','400G')

查属性值

print(cpt1.brand)
print(getattr(cpt1,'brand'))

修改属性值

cpt2.memsize='360G'
print(cpt2.memsize)
setattr(cpt3,'memsize','500G')
print(cpt3.memsize)

增加属性值

cpt2.price='800$'
print(cpt2.price)

删除属性的值

del cpt3.color

delattr(cpt2.memsize)

a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性

b.通过attr相关方法去获取、修改、添加和删除它的属性

2.声明⼀个人的类和狗的类:

狗的属性:名字、颜色、年龄

狗的方法:叫唤

人的属性:名字、年龄、狗

人的方法:遛狗

class Human:
'''人类,人的属性:名字、年龄、狗
人的方法:遛狗'''

def __init__(self, name='', age='', dog=''):
    self.name = name
    self.age = age
    self.dog = dog


def game(self):
    print('遛狗')

class Dog:
''' 狗,狗的属性:名字、颜色、年龄,
狗的方法:叫唤
'''

def __init__(self, name='', color='', age=''):
    self.name = name
    self.age = age
    self.color = color


def game(self):
    print('叫唤')

xiaoming=Human('小明','14','dahuang')
dahuang=Dog('大黄','黄色','2')
print('%s拥有%s' % (xiaoming.name,dahuang.name))
print('%s遛%s' % (xiaoming.name,dahuang.name))

a.创建人的对象小明,让他拥有⼀一条狗大黄,然后让小明去遛大黄

3.声明⼀一个圆类,自己确定有哪些属性和方法

class Circle:
'''圆,属性:半径、材料,
圆的方法:做轮子,做井盖,'''

def __init__(self, radial='', material=''):
    self.radial = radial
    self.material= material

def game(self):
    print('做轮子')

def game2(self):
    print('做盖子')

4.创建⼀一个学生类:

属性:姓名,年龄,学号

方法:答到,展示学生信息

创建⼀一个班级类:

属性:学生,班级名

方法:添加学生,删除学生,点名, 求班上学生的平均年龄

class Student:
''' 学生,属性:姓名、年龄,学号,
学生的方法:答到,展示学生信息 '''

def __init__(self, name='',age='',stu_id=''):
    self.name = name
    self.age= age
    self.stu_id=stu_id
def game(self):
    print('答到')

def game2(self):
    print('展示学生信息')

class Class:
'''班级,属性:学生、班级名,
班级的方法:添加学生,删除学生,点名,求班里学生平均年龄'''

def __init__(self, class_name='',student=''):
    self.class_name = class_name
    self.student= student

def game(self):
    print('添加学生')

def game3(self):
    print('点名')

def game4(self):
    print('求学生平均年龄')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 8月22日-----字符串相关 2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消...
    future_d180阅读 999评论 0 1
  • 一、面向对象编程编程思想:1.面向过程编程 ---> 算法,逻辑(数学逻辑)2.函数式编程 ---> 函数,模块3...
    Smr_T阅读 532评论 0 0
  • 1.1面向对象 面向对象(object-oriented ;简称: OO)至今还没有统一的概念 我这里把它定义为:...
    TENG书阅读 577评论 0 0
  • 一、面向对象编辑 编程思想:1.面向过程编程 ---> 算法,逻辑(数学逻辑) 2.函数式编程 ---> 函数,...
    大漠判官1阅读 297评论 0 0
  • 1/579文件IO文件介绍I/O流流的概念读写文件文件备份IO介绍大家应该听说过一句话:“好记性不如烂笔头”。不仅...
    清清子衿木子水心阅读 1,468评论 0 1