Python之面向对象方法

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__()

  链接:详细

猜你喜欢

转载自www.cnblogs.com/george92/p/9233860.html