1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤小 方法:打游戏、写代码、看视频
class Pc:
def __init__(self,brand1,color1,memory1 ):
self.brand = brand1
self.color = color1
self.memory = memory1
def func(self,n:''):
print('品牌%s、颜色%s、内存%s、可以用来%s'%(self.brand,self.color,self.memory,n))
pc1 = Pc('华硕','蓝色','8g')
pc1.func('玩游戏')
# print(pc1)
pc2 = Pc('戴尔','黑色','2g')
pc2.func('写代码')
pc3= Pc('联想','黑色','1g')
pc3.func('看视频')
# a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
print(pc1.brand)
pc1.brand = '外星人'
pc1.sizes = '14英寸'
print(pc1.brand,pc1.sizes)
# del pc1.sizes
print(pc1.sizes)
# b.通过attr相关⽅方法去获取、修改、添加和删除它的属性
setattr(pc1,'brand','麒麟')
print(pc1.brand)
# delattr(pc2.brand)
print(pc2.brand)
setattr(pc1,'function1','看电影')
print(pc1.function1)
2.声明⼀个人的类和狗的类:
狗的属性:名字、颜⾊色、年年龄
狗的⽅方法:叫唤
人的属性:名字、年年龄、狗
人的⽅方法:遛狗
a.创建⼈人的对象⼩小明,让他拥有⼀一条狗⼤大⻩黄,然后让⼩小明去遛⼤大⻩黄
class Dog:
def __init__(self,name1,color1,age1):
self.name = name1
self.color = color1
self.age = age1
def way1(self,n:''):
pass
dog1 = Dog('大黄','黄色','2')
dog1.way1('叫')
# print(dog1.name)
class Person:
def __init__(self,name2,age2,dog2):
self.name = name2
self.age = age2
self.dog = dog2
def way2(self,m:''):
print('%s'%(person1.name),m,'%s'%(person1.dog))
person1 = Person('小明','12',dog1.name)
person1.way2('溜')
3.声明⼀一个圆类,自己确定有哪些属性和方法
class Round:
def __init__(self,s1,l1):
self.s = s1
self.l = l1
def way2(self,n1,n2,):
print('%s = %s*%s的平方'%(round1.s,n1,n2))
print('%s = 2%s%s' % (round1.l, n1, n2))
round1 = Round('S','L')
round1.way2('π','r')
4.创建⼀一个学⽣生类:
属性:姓名,年龄,学号
方法:答到,展示学⽣生信息
class Stu:
def __init__(self,name1,age1,number1):
self.name = name1
self.age = age1
self.number = number1
def answer(self,name2):
import random
a = random.randint(0, 1)
if a:
print('在下%s信息如下'%(name2))
print(stu1.__dict__)
else:
print('人不在')
stu1 = Stu('小白',12,'001')
stu1.answer(stu1.name)