Python全栈开发——类

#类属性包括数据属性和函数属性,类的实例实际却只有数据属性 
#函数加括号传的是运行函数,没有括号传的是内存地址
def name():
    print('dddd')
print(name)    #<function name at 0x01388618>
print(name())   
#dddd None

#------------类的增删改查
class Chinese:
    country='China'
    def __init__(self,name):
        self.name=name

    def play_ball(self,ball):
        print('%s 正在打 %s'%(self.name,ball))

#查
print(Chinese.country)
#改 Chinese.country
='Japan' p1=Chinese('alex') print(p1.__dict__) #常用内置的方法 print(p1.country) #Japan
#增加 Chinese.dang
='GongChangDang' print(p1.dang) #GongChangDang
def eat_food(self,food): print(
'%s 正在吃 %s' %(self.name,food)) Chinese.eat=eat_food #传地址
print(Chinese.__dict__) p1.eat(
'ff') #alex 正在吃 ff #删 del Chinese.dang del Chinese.country print(Chinese.__dict__) #常用内置的方法
 
 
#------------实例的增删改查
class Chinese:
    country='China'
    def __init__(self,name):
        self.name=name

    def play_ball(self,ball):
        print('%s 正在打 %s'%(self.name,ball))

#查看实例属性
p1=Chinese('lujiacheng')
print(p1.__dict__)  #{'name': 'lujiacheng'}
print(p1.play_ball('Baskball'))

#增加
p1.age=21
print(p1.__dict__) #{'name': 'lujiacheng', 'age': 21}
print(p1.age)   #21
静态属性 
@property         #将一个函数属性隐藏起来
class Room:
    def __init__(self,name,owner,width,length,height):
        self.name=name
        self.owner=owner
        self.width=width           #静态属性
        self.length=length
        self.heigth=height

    @property  # 将一个函数属性隐藏起来
    def cal_Area(self):
        print ('%s 住的是 %s,总面积是 %s' %(self.owner, self.name, self.width * self.length))
r1=Room('厕所','alex',10,10,10)
r1.cal_Area  
#alex 住的是 厕所,总面积是 100
静态方法
 @staticmethod   #类的工具包,实例不能调用
class Room:
    tag=1
    def __init__(self,name,owner,width,length,height):
        self.name=name
        self.owner=owner
        self.width=width
        self.length=length
        self.heigth=height
 
    @staticmethod   #类的工具包,实例不能调用
    def wash_body(a,b,c):
        print('%s %s %s 正在洗澡' %(a,b,c))

Room.wash_body('dvd','dsvfs','dddddsg')
#dvd dsvfs dddddsg 正在洗澡 

类方法 (不跟实例绑定在一起)
@classmethod      #供类使用的方法,不需实例化
class Room:
    tag=1
    def __init__(self,name,owner,width,length,height):
        self.name=name
        self.owner=owner
        self.width=width
        self.length=length
        self.heigth=height


    @classmethod      #供类使用的方法,不需实例化
    def tell_info(cls,name):
        print(cls.tag,name)

Room.tell_info('liju')   #调用
#1 liju    
 
 

 

 
 

 

 
 

 

 
 

 

 
 
 
 

 

 
 
 
 
 
 
 
 

 

 

转载于:https://www.cnblogs.com/lujiacheng-Python/p/9727145.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值