Python之面向对象方法

  property的用法:

    property属于类的封装的范畴

    property是一种特殊的属性,访问它时会执行一段功能(函数),然后返回值。

    用property的方法,就可以实现用property作为装饰器,来直接用被装饰的函数里的数据。

    而不用再繁琐的去用 "__" 的方法去调用。

import math
class Circle:
def __init__(self,radius): #圆的半径radius
self.radius=radius @property #area=property(area)
def area(self):
return math.pi * self.radius**2 #计算面积 @property
def perimeter(self):
return 2*math.pi*self.radius #计算周长 c=Circle(7)
print(c.radius)
c.radius=10 # print(c.area())
# print(c.perimeter())
print(c.area)
print(c.perimeter)
class People:
def __init__(self,name,age,height,weight):
self.name=name
self.age=age
self.height=height
self.weight=weight
@property
def bodyindex(self):
return self.weight/(self.height**2) # p1=People('cobila',38,1.65,74)
# print(p1.bodyindex)
# p1.weight=200
# print(p1.bodyindex)
#被property装饰的属性会优先于对象的属性被使用

#而被propery装饰的属性,如sex,分成三种:
# 1.property
# 2.sex.setter
# 3.sex.deleter #
# class People:
# def __init__(self,name,SEX):
# self.name=name
# # self.__sex=SEX
# self.sex=SEX #self.sex='male' p1.sex='male'
# @property
# def sex(self):
# return self.__sex #p1.__sex
#
# @sex.setter
# def sex(self,value):
# # print(self,value)
# if not isinstance(value,str):
# raise TypeError('性别必须是字符串类型')
# self.__sex=value #p1.__sex='male'
# @sex.deleter
# def sex(self):
# del self.__sex #del p1.__sex # p1=People('cobila','male')
# print(p1.tell_name())
#
# print(p1.sex)
# p1.sex='123' # p1.sex='female'
# print(p1.sex) # p1.sex=123123123123123123123123123213 # p1=People('cobila',123123123123123)
# p1=People('cobila','male')
# print(p1.sex)
# del p1.sex #del self.sex
# print(p1.sex) class People:
def __init__(self,name,SEX):
self.name=name
# self.__sex=SEX
self.sex=SEX#p1.sex='male' @property
def sex(self):
print('------proerty---------------------->')
return self.__sex @sex.setter
def sex(self,value):
print('===================》')
# print(self,value)
# if not isinstance(value,str):
# raise TypeError('性别必须是字符串类型')
self.__sex=value #p1.ABCDEFG='male'
@sex.deleter
def sex(self):
print('delete 操作')
del self.__sex p1=People('George','male')
# print(p1.__dict__)
# print(p1.sex)
# del p1.sex
# print(p1.sex) # print(p1.ABCDEFG)
# p1.ABCDEFG=123123
# print(p1.ABCDEFG) # p1.sex
# print(p1.__dict__) # p1.sex # p1.sex='mall' # del p1.sex

  staticmethod的用法:

     应用场景:

      

# class Foo:
# def spam(self):
# print('----->',self)
#
#
# # Foo.spam(123123)
#
# f1=Foo()
# f1.spam() # class Foo:
# @staticmethod
# def spam(x,y,z):
# print(x,y,z)
#
#
# # Foo.spam(1,2,3)
# f2=Foo()
# f2.spam(1,2,3) import time
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
#
# def test():
# print('from test')
#
@staticmethod
def now(): #用Date.now()的形式去产生实例,该实例用的是当前时间
t=time.localtime() #获取结构化的时间格式
obj=Date(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
return obj @staticmethod
def tomorrow():#用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
t=time.localtime(time.time()+86400)
return Date(t.tm_year,t.tm_mon,t.tm_mday)
# d1=Date(2017,1,13)
# # Date.test()
# print(d1.test)
# d1.test() # d1=Date(2017,1,13)
# d2=Date(2017,1,14)
# date_now=Date.now()
# print(date_now)
# print(date_now.year)
# print(date_now.month)
# print(date_now.day) # d1=Date.now()
# print(d1.year,d1.month,d1.day)
#
# d2=Date.tomorrow()
# print(d2.day) #但凡是定义在类的内部,并且没有被任何装饰器修饰过的方法,都是绑定方法:有自动传值功能
d1=Date(1212,22,22)
print(d1.now)
print(Date.now)
# d1.now() #now(d1) #但凡是定义在类的内部,并且被staticmethod装饰器修饰过的方法,都是解除绑定的方法,实际上就函数:就没有自动传值功能了
d_n1=Date.now()
d_n2=d1.now()
print(d_n1.year,d_n1.month,d_n1.day)
print(d_n2.year,d_n2.month,d_n2.day)

  

  classmethod的用法:

#把一个方法绑定给类:类.绑定到类的方法(),会把类本身当做第一个参数自动传给绑定到类的方法
#拿掉一个类的内存地址后,就可以实例化或者引用类的属性了
class Foo:
def bar(self):
pass
@classmethod #把一个方法绑定给类:类.绑定到类的方法(),会把类本身当做第一个参数自动传给绑定到类的方法
def test(cls,x):
print(cls,x) #拿掉一个类的内存地址后,就可以实例化或者引用类的属性了 # print(Foo.bar)
# print(Foo.test) Foo.test(123123) f=Foo()
print(f.bar)
print(f.test)
print(Foo.test)
f.test(1111)
import time
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
#
# def test():
# print('from test')
#
@classmethod
def now(cls): #用Date.now()的形式去产生实例,该实例用的是当前时间
print(cls)
t=time.localtime() #获取结构化的时间格式
obj=cls(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
return obj @classmethod
def tomorrow(cls):#用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
t=time.localtime(time.time()+86400)
return cls(t.tm_year,t.tm_mon,t.tm_mday) class EuroDate(Date):
def __str__(self):
return '年:%s,月:%s,日:%s' %(self.year,self.month,self.day) # e1=EuroDate.now()
# print(e1) e1=EuroDate(1,1,1)
print(e1)

  __str__ 的用法:

#__str__定义在类内部,必须返回一个字符串类型,
#什么时候会出发它的执行呢?打印由这个类产生的对象时,会触发执行
   
#__str__定义在类内部,必须返回一个字符串类型,
#什么时候会出发它的执行呢?打印由这个类产生的对象时,会触发执行 class People:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return '<name:%s,age:%s>' %(self.name,self.age) p1=People('George',18)
print(p1)
str(p1) #----->p1.__str__()

  链接:详细

Python之面向对象方法的更多相关文章

  1. python基础——面向对象编程

    python基础——面向对象编程 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的 ...

  2. python类及其方法

    python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...

  3. Python的面向对象3

    接下来,我们接着讲Python的面向对象,在上一次的博客中,我们详细介绍了类与对象的属性,今天,我们来详细介绍一下面向对象中的方法! 1.定义实例方法 一个实例的私有属性就是以__开头的属性,无法被外 ...

  4. Python的面向对象2

    我们接着讲解Python的面向对象 1.初始化实例属性 在现实生活中,一种类型的实例会具有相同的某些属性,把这些实例划分为一个类型,则这些实例必然有相似的部分.但是,在创建实例之后,我们一个一个的为实 ...

  5. Python的面向对象1

    今天,我们来介绍Python的面向对象编程,其实面向对象并不陌生,在C++  ,Java  ,PHP中也有大量使用! 好了,我们来步入正题! 那什么是面向对象编程呢? 1. 面向对象编程是一种程序设计 ...

  6. Python进阶---面向对象的程序设计思想

    Python的面向对象 一.面向过程与面向对象的对比 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优 ...

  7. Python之面向对象一

    引子 小游戏:人狗大战 角色:人和狗 角色属性:姓名,血量,战斗力和性别(种类) 技能:打/咬 用函数实现人打狗和狗咬人的情形 def Dog(name,blood,aggr,kind): dog = ...

  8. python基础——面向对象进阶下

    python基础--面向对象进阶下 1 __setitem__,__getitem,__delitem__ 把对象操作属性模拟成字典的格式 想对比__getattr__(), __setattr__( ...

  9. python基础——面向对象进阶

    python基础--面向对象进阶 1.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 ...

随机推荐

  1. bzoj 3779: 重组病毒【LCT+线段树维护dfs序】

    %.8lf会WA!!%.8lf会WA!!%.8lf会WA!!要%.10lf!! 和4817有点像,但是更复杂. 首先对于操作一"在编号为x的计算机中植入病毒的一个新变种,在植入一个新变种时, ...

  2. YCOJ中国邮递员问题

    题目: Description 一个邮递员从邮局出发,需要去 n - 2个城市送信,送完信件以后回家. 邮局在城市 1,家在城市 n,任意两个城市之间都有道路,但是这些道路是单向,也就是说 a 到 b ...

  3. 强连通分量初探 By cellur925

    并不理解.但是毕竟也做了一些题,略微小结. 注:这里讨论的暂时是有向图的强联通分量. 先贴出模板.学长:我也不理解,但我可以叫你们怎么背代码. #include<cstdio> #incl ...

  4. win7 右键菜单残影 消除方法

    1. 治标法: 右键桌面->更改分辨率  随便换一个分辨率再还原就OK了! 2. 根治法: 我的电脑-> 属性 ->高级选项-> 高级-> 设置-> 自定义   不 ...

  5. java基础类型数据与String类包装类之间的转换与理解

    数据类型转换一般分为三种: 在java中整型,实型,字符型视为简单数据类型,这些数据类型由低到高分别为:(byte,short,char--int-long-float-double) 简单数据类型之 ...

  6. Service官方教程(11)Bound Service示例之2-AIDL 定义跨进程接口并通信

    Android Interface Definition Language (AIDL) 1.In this document Defining an AIDL Interface Create th ...

  7. 浏览器 cookie session

    浏览器的cookie被禁用的话,则服务端的session不起作用 session是基于cookie实现的, 还是办理会员卡的问题, cookie客户端支持,即客户手上的会员卡记录了所有信息, sess ...

  8. thinkphp3.2.3连接sqlserver 2008 R2 数据库

    环境: 操作系统——win7 64位旗舰版 PHP——thinkphp 3.23 数据库——Microsoft SQL Server 2008 R2 需要用到的软件: 步骤: 1.搜索SQLSRV30 ...

  9. Scala基础篇-01变量,数据类型

    一.Scala变量 共有3种变量修饰符: val: 可变 var: 不可变 lazy val: 第一次应用时才计算 二.Scala的数据类型 1)类型不匹配:高精度数值赋给低精度数据类型. 2)Uni ...

  10. v使用索引的注意事项及常见场景、案例

    索引的原理与作用,各种书籍和网络上的介绍可以说是铺天盖地,基本上主流数据库系统的也都是一致的.选择索引字段的原则,比如外键字段.数据类型较小的字段.经常用于查询或排序的字段.表关联的字段等等,在此不做 ...