'''
面向对象:
面向过程:根据业务逻辑从上到下写代码。
面向对象:将变量与函数绑定到一起,分类进行封装,每个程序只要负责分配给自己的分类,这样能够更快速的开发程序,减少了重复代码
'''
# 定义类:
class Computer:
# 属性: 特征的描述都是通过属性
id = '001'
brand = ''
price = ''
weight = ''
color = ''
# 方法
def online(self):
print('我能上网啦')
print(Computer)
print(id(Computer))
# 创建对象
c1 = Computer()
print(c1)
print(id(c1))
print(c1.id) # 对象调用属性
c1.id = '002'
print(c1.id) # 对象调用属性
c2 = Computer()
print(c2)
print(id(c2))
print(c2.id)
c3 = Computer()
print(c3)
print(id(c3))
print(c3.id)
# 程序理解对象
# 定义一个类Phone,属性:brand id color size 动作: call
# 使用Phone类创建2个对象
# 打印phone对象的品牌
'''
# 定义一个类Phone,属性:brand id color size 动作: call
# 使用Phone类创建2个对象
# 打印phone对象的品牌
类属性: 属于类本身,所有对象都可以访问此属性
类属性的修改访问:
类名.属性=新值
print(类名.属性)
print(对象名.属性) ---> 对象本身没有此属性,则得到的就是类的属性
对象属性:
对象属性的添加:
1.动态添加:
对象名.属性名 = 值
2.获取对象的属性值
对象名.属性
'''
# 1. 定义类
class Phone:
# 类属性和对象属性
brand = '华为'
id = '001'
price = '3999'
color = '黑色'
# 方法:
def call(self):
print("self:",self)
print('可以使用手机打电话')
def sendMessage(self):
print("self:", self)
print('发送信息!')
# 创建对象
names = ['', ''] # <class 'list'>
# Phone也是一个类型,使用Phone类型创建的变量,将此变量huawei称作对象
huawei = Phone()
print(huawei.brand)
xiaomi = Phone()
print(xiaomi.brand)
print('---操作类属性---')
# 如果修改类的属性,必须通过:类名.属性名=新值
Phone.price = 4999
# 访问得到的就是更改之后值
print(huawei.price) # 先找对象本身是否存在price属性,如果不存在则去类中找price属性
print(xiaomi.price)
print('------对象属性的添加--------')
huawei.size = '6.39'
xiaomi.weight = '0.09'
huawei.id = '005'
print(xiaomi.weight)
print(huawei.size,huawei.id)
print('---------------')