1.声明 个电脑类: 属性:品牌、颜 、内存 法:打游戏、写代码、看视频
class Computer:
def __init__(self, brand1, color1, memory1):
self.brand = brand1
self.color = color1
self.memory = memory1
def play(self):
print('打游戏', '写代码', '看视频')
c1 = Computer('lenovo', 'red', '1000')
c1.play()
a.创建电脑类的对象,然后通过对象点的 式获取、修改、添加和删除它的属性 b.通过attr相关 法去获取、修改、添加和删除它的属性
class Computer:
def __init__(self, brand1, color1, memory1):
self.brand = brand1
self.color = color1
self.memory = memory1
a = Computer('lenovo', 'red', '1000')
print(a.brand) # 查
a.color = 'green' # 修改
print(a.color)
a.size = 17 # 添加
print(a.size)
del a.brand # 删除
# print(a.brand)
getattr(a, 'size') # 查、获取
print(a.size,'=====')
setattr(a, 'size', 21) # 改
print(a.size)
getattr(a, 'money', 1000) # 添加
print(a.memory)
del a.color # 删除
print(a.color)
2.声明人的类和狗的类:
狗的属性:名字、颜 、 龄 狗的 法:叫唤 的属性:名字、 龄、狗 的 法:遛狗 a.创建 的对象 明,让他拥有 条狗 ,然后让 明去遛
class Dog:
def __init__(self, name='小花', color='红', age=3):
self.name = name
self.color = color
self.age = age
def call(self):
print('汪汪')
a = Dog()
a.call()
3.声明 个矩形类:
属性: 、宽 法:计算周 和 积 a.创建 同的矩形,并且打印其周 和 积
class Rectangle:
def __init__(self, long=20, wide=30):
self.long = long
self.wide = wide
def perimeter(self):
return (self.wide + self.long)*2
def area(self):
return self.long*self.wide
a = Rectangle()
b = a.perimeter() # 周长
c = a.area() # 面积
print('周长为:%d;面积为:%d' % (b, c))
网友评论