面向对象之类的方法 私有属性(加俩下划线) 新式类 经典类的区别

 @classmetod

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Animal:
    def __init__(self,name):
        self.name = name
    hobbie = 'meat'


    @classmethod   #类方法不能访问实例变量  加上后  self.name就无法访问了
    def talk(self):
        #print("%s is talking" %self.name)
        print("%s is talking" %self.hobbie)


Animal.hobbie
Animal.talk()

d = Animal("wangwang")
d.talk()

  

@staticmethon

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Animal:
    def __init__(self,name):
        self.name = name
    hobbie = 'meat'

    '''
    @classmethod   #类方法不能访问实例变量  self.name就无法访问了
    def talk(self):
        #print("%s is talking" %self.name)
        print("%s is talking" %self.hobbie)
    '''
    @staticmethod   #静态方法,跟事例没有任何关系了,实例变量和类变量都无法访问了,
    def walk():
        print("is walking..." )


#Animal.hobbie
#Animal.talk()

d = Animal("wangwang")
d.walk()

  

@property

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Animal:
    def __init__(self,name):
        self.name = name
        self.num = None
    hobbie = 'meat'


    '''
    @classmethod   #类方法不能访问实例变量  self.name就无法访问了
    def talk(self):
        #print("%s is talking" %self.name)
        print("%s is talking" %self.hobbie)


    @staticmethod   #静态方法,跟事例没有任何关系了,实例变量和类变量都无法访问了,
    def walk():
        print("is walking..." )
    '''
    @property   #加了property后babit就不是方法了,调用时不用加括号,是属性。把方法变成属性,类变量实例变量还可以访问。不能传值了。如果非要传值看下面的例子,删除也在下面
    def habit(self):
        print("[%s] habit is xxoo" %self.name)

   #往里面传值
    @property
    def total_players(self):
        return self.num
    @total_players.setter
    def total_players(self,num):
        self.num = num
        print("total players:",self.num)

    #删值
    @total_players.deleter
    def total_players(self):
        print("total player got delted")
        del self.num
#Animal.hobbie
#Animal.talk()

# d = Animal("wangwang")
# d.walk()

d = Animal("WangWang")
d.habit

print(d.total_players)
d.total_players = 3
del d.total_players
print(d.total_players)

私有变量(私有属性))   外部无法访问,如果特殊情况下非要访问也是可以的

print("OUT__Num:",d.__num)改成 print("OUT__Num:",d._Animal__num)
#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Animal:
    def __init__(self,name):
        self.name = name
        self.__num = None
    hobbie = 'meat'


    '''
    @classmethod   #类方法不能访问实例变量  self.name就无法访问了
    def talk(self):
        #print("%s is talking" %self.name)
        print("%s is talking" %self.hobbie)


    @staticmethod   #静态方法,跟事例没有任何关系了,实例变量和类变量都无法访问了,
    def walk():
        print("is walking..." )
    '''
    @property   #加了property 后babit就不是方法了,是属性。把方法变成属性,类变量实例变量还可以访问。不能传值了。如果非要传值看下面的例子,删除也在下面
    def habit(self):
        print("[%s] habit is xxoo" %self.name)

   #往里面传值
    @property
    def total_players(self):
        return self.__num
    @total_players.setter
    def total_players(self,num):
        self.__num = num
        print("total players:",self.__num)

    #删值
    @total_players.deleter
    def total_players(self):
        print("total player got delted")
        del self.num
#Animal.hobbie
#Animal.talk()

# d = Animal("wangwang")
# d.walk()

d = Animal("WangWang")
d.habit

print(d.total_players)
d.total_players = 3
d.__num = 9
print("OUT__Num:",d.__num)
#del d.total_players
print(d.total_players)

  

多继承 经典类和新式类的区别

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# B > C  > A 广度优先  从同一级从左到右以此查找,然后再去上一级   新式类
# B > A > C                   深度有先   经典类
#新式类和经典类 区别在于 py2 和py3 的区别,同样的代码用2.7的就成了深度优先
class A:                               #经典类
#class A(object):                       #新式类
    n = "A"
    def f2(self):
        print("AF2")
class B(A):
    n = "B"
    def f1(self):
        print("B")
    def f2(self):
        print("B2")
class C(A):
    n = "C"
    def f2(self):
        print("C")

class D(B,C):
    pass

d = D()
#d.f1()
d.f2()





#面向对象特性之多态
'''
__doc__
显示类的解释注释

__del__
析构方法
等程序执行结束后统一销毁

__call__
实例化加()自动执行call方法  无卵用


__new__
把类重写, new调用了init   暂时无用


__dict__
类的成员变量以字典形式显示出来

'''

  

原文地址:https://www.cnblogs.com/dribs/p/5701008.html