class 类

类的简介:

"""
什么事类:
    类:把一类事物相同的特征和动作整合到一起就是类,类是一个抽象的概念
什么对象:
    对象:是基于 类 而创建的一个具体的事物(具体存在的)
    也就是特征和动作整合到一起
"""
class foo:
    '文档注释'
    def speak(self):
        print('yjq再说话')

    def  eat(self,name):
        print('%s在吃饭'%name)


obj = foo()#实例化一个对象
obj.speak()#调用对象中的方法

o = foo()#实例化一个对象
o.eat('yjq')#调用对象中的方法
print(o.__doc__)
print(o.__module__)

初始化构造函数:

p1 = chinese('yjq',21,'')#实例化一个对象
print('类country',chinese.country)
print('实例化',p1.country)
p1.country = 'japan'                                 #给实例化对象复制(修改值),都是对实例化对象进行操作
print('实例化更改实例 的 country值',p1.country)
#调用实例对象的方法
p1.like('Python编程')
print('打印实例化的字典变量',p1.__dict__)

p2 = chinese('xxl',22,'')
p2.like('汽车机修')
"""
yjq
类country china
实例化 china
实例化更改实例 的 country值 japan
我非常喜欢这Python编程书
打印实例化的字典变量 {'name': 'yjq', 'age': 21, 'sex': '男', 'country': 'japan', 'book': 'Python编程'}
xxl
我非常喜欢这汽车机修书
"""

append

print(chinese.lis)
print(chinese.__dict__)
p2.lis.append('C++')   # 添加: 添加class 并不是实例化对象的
print(p2.lis)
print(chinese.lis)
print(chinese.__dict__)
""['c', 'java', 'python']
{'__module__': '__main__', 'country': 'china', 'lis': ['c', 'java', 'python'], '__init__': <function chinese.__init__ at 0x0000027F3922F9D8>, 'like': <function chinese.like at 0x0000027F3922FA60>, '__dict__': <attribute '__dict__' of 'chinese' objects>, '__weakref__': <attribute '__weakref__' of 'chinese' objects>, '__doc__': None}
['c', 'java', 'python', 'C++']
['c', 'java', 'python', 'C++']
{'__module__': '__main__', 'country': 'china', 'lis': ['c', 'java', 'python', 'C++'], '__init__': <function chinese.__init__ at 0x0000027F3922F9D8>, 'like': <function chinese.like at 0x0000027F3922FA60>, '__dict__': <attribute '__dict__' of 'chinese' objects>, '__weakref__': <attribute '__weakref__' of 'chinese' objects>, '__doc__': None}

"""
@property
class Room:
    def __init__(self,name,onewr,width,length,height):#构造函数
        self.name = name
        self.onewr = onewr
        self.width = width
        self.length = length
        self.height = height

    @property #更改函数的调用方式,print(r1.cal_arae) 有点:封装函数了处理逻辑
    def cal_arae(self):
      return self.width*self.length

    @property
    def cal_volume(self):
        return self.width*self.height*self.length

r1 = Room('客厅','yjq',20,30,3.5)
print(r1.name)
print(r1.cal_arae)
print(r1.cal_volume)
'''
运行结果:
客厅
600
2100.0
'''
调用类方法
class Room:#类数据
    def __init__(self,name,onewr,width,length,height):
        self.name = name
        self.onewr = onewr
        self.width = width
        self.length = length
        self.height = height

    @property #更改函数的调用方式,print(r1.cal_arae) 有点:封装函数了处理逻辑
    def cal_arae(self):
      return self.width*self.length

    @property
    def cal_volume(self):
        return self.width*self.height*self.length

    @classmethod
    def test_info(cls):
        print(cls)
        print('from test_info')

#调用类方法
Room.test_info()  
#from test_info

静态方class Room:#类数    def __init__(self,name,onewr,width,length,height)        self.name = name        self.onewr = onewr

        self.width = width
        self.length = length
        self.height = height
    @staticmethod#静态方法
    def room_info():
        print('111111111')

    @staticmethod
    def wash_body(name):
        print('%s正在洗澡'% name)

Room.room_info()

Room.wash_body('yjq')
"""

111111111 yjq正在洗澡 """

 

转载于:https://www.cnblogs.com/yangjinquan/p/9735450.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值