一、静态属性
@property将方法标记成数据属性;可以访问实例和类的属性
 @classmethod标记成类的方法,不需要实例化,可以类直接调用的方法。可以访问类的属性方法,不能访问实例的
class Room:
tag=1
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh @property
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length def test(self):
print('from test',self.name) @classmethod
def tell_info(cls,x):
print(cls)
print('--》',cls.tag,x)#print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag) # print(Room.tag) # Room.test(1) #1.name
# r1=Room('厕所','alex',100,100,100000)
Room.tell_info(10)

二、静态方法:

@staticmethod 类的工具包,跟类完全独立,不能调用类的属性 可以用实例和类直接调用
class Room:
tag=1
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh @property
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length @classmethod
def tell_info(cls,x):
print(cls)
print('--》',cls.tag,x)#print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag) @staticmethod
def wash_body(a,b,c):
print('%s %s %s正在洗澡' %(a,b,c)) def test(x,y):
print(x,y) # Room.wash_body('alex','yuanhao','wupeiqi') print(Room.__dict__) r1=Room('厕所','alex',100,100,100000) print(r1.__dict__)

@property@classmethod@staticmethod的更多相关文章

  1. 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)

    组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...

  2. python 全栈开发,Day22(封装,property,classmethod,staticmethod)

    一.封装 封装 : 广义上的 :把一堆东西装在一个容器里 狭义上的 :会对一种现象起一个专门属于它的名字 函数和属性装到了一个非全局的命名空间 —— 封装 隐藏对象的属性和实现细节,仅对外提供公共访问 ...

  3. 面向对象 公有私有 property classmethod staticmethod

    接口类(抽象类)--------就是一种规范 面向对象的私有与公有 对于每一个类的成员而言都有两种形式: 公有成员,在任何地方都能访问 私有成员,只有在类的内部才能方法 私有成员和公有成员的访问限制不 ...

  4. Python学习第十六课——静态属性(property, classmethod, staticmethod)

    计算所居住房子的面积 普通写法 class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.ow ...

  5. python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?

    回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...

  6. Python类中装饰器classmethod,staticmethod,property,

    @classmethod 有的时候在类中会有一种情况,就是这个方法并不需要使用每一个对象属性 因此 这个方法中的self参数一个完全无用的参数,使用classmethod class A: __cou ...

  7. @classmethod @staticmethod 个人理解

    官方解释 @classmethod 一个类方法把类自己作为第一个实参, 就像一个实例方法把实例自己作为第一个实参. 语法格式: class C: @classmethod def f(cls, arg ...

  8. @classmethod, @staticmethod和@property这三个装饰器的使用对象是在类中定义的函数。下面的例子展示了它们的用法和行为:

    class MyClass(object): def __init__(self): self._some_property = "properties are nice" sel ...

  9. 面向对象之—property,staticmethod

    一 特性( property) property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值. property是内置的一种封装方法:把一个属性“伪装”成一个数据属性,做法就是在需要伪装 ...

随机推荐

  1. 函数默认参数的TDZ

    我们知道块级作用域会有TDZ. 其实方法参数也存在TDZ function add(first = second, second) { return first + second; } console ...

  2. shell专题(十):Shell工具(重点)

    10.1 cut cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的.cut 命令从文件的每一行剪切字节.字符和字段并将这些字节.字符和字段输出. 1.基本用法 cut [选项参数]  fi ...

  3. java 基本语法(二) 变量的使用(重点)

    1.变量的分类1.1 按数据类型分类 详细说明://1. 整型:byte(1字节=8bit) \ short(2字节) \ int(4字节) \ long(8字节) //① byte范围:-128 ~ ...

  4. 【高性能Mysql 】读书笔记(一)

    第1章 Mysql架构与历史 MYSQL最重要.最与众不同的特性是它的存储引擎架构,这种架构的设计将查询处理( Query Processing)及其他系统任务( Server Task)和数据的存储 ...

  5. springboot使用maven命令打包jar及配置文件配置

    sspringboot项目如果不想每次修改配置文件就要重新打包jar的话,可以进行以下配置进行打包 1.在resources下新建assembly文件夹package.xml <?xml ver ...

  6. OSCP Learning Notes - Enumeration(1)

    Installing Kioptrix: Level 1 Download the vm machine form https://www.vulnhub.com/entry/kioptrix-lev ...

  7. Ethical Hacking - NETWORK PENETRATION TESTING(14)

    MITM - ARP Poisoning Theory Man In The Middle Attacks - ARP Poisoning This is one of the most danger ...

  8. Python Ethical Hacking - WEB PENETRATION TESTING(4)

    CRAWING SPIDER Goal -> Recursively list all links starting from a base URL. 1. Read page HTML. 2. ...

  9. 一口气说出 4 种分布式一致性 Session 实现方式,面试杠杠的~

    前言 公司有一个 Web 管理系统,使用 Tomcat 进行部署.由于是后台管理系统,所有的网页都需要登录授权之后才能进行相应的操作. 起初这个系统的用的人也不多,为了节省资源,这个系统仅仅只是单机部 ...

  10. 你闺女也能看懂的插画版 Kubernetes 指南

    Matt Butcher是Deis的平台架构师,热爱哲学,咖啡和精雕细琢的代码.有一天女儿走进书房问他什么是Kubernetes,于是就有了这本插画版的Kubernetes指南,讲述了勇敢的Phipp ...