1. 程式人生 > >面向對象中的特殊的成員修飾符和幾個特殊的方法

面向對象中的特殊的成員修飾符和幾個特殊的方法

lex 字段 我們 返回值 ini 獲取 elf super pri

面向對象的成員修飾符

#Auther Bob
#--*--conding:utf-8 --*--
 
# 成員修飾符
#     共有成員
#     私有成員
        # 1、私有字段
        #         私有的普通字段
        #         私有的靜態字段
 
        # 2、私有方法
        #         私有的方法
 
# 默認情況下,類的字段和方法都是共有的,通過對象和類就直接可以訪問,但是如果我們在字段或者方法名字的前面加2個下劃線,那麽我們就不能在外部訪問這些字段和方法,這些
# 方法和字段只能內部方法

class person(object):
    name = "diandian"
    __age = 3
    def __init__(self,address,phone):
        self.address = address
        self.__phone = phone
    def func_print1(self):
        print(self.name,self.__age)
        print(self.address,self.__phone)
    def __func_print2(self):
        
print(self.name,self.__age) print(self.address,self.__phone) return self.__phone def func_print2(self): return self.__func_print2() p1 = person("shenzhen","123456789") print(p1.name) # 這裏的name是共有靜態字段,在外部可以直接訪問 # print(p1.__age) # 這裏的__age是私有的靜態字段,在外部不可以直接訪問
print(p1.address) # 這裏address是共有的普通字段,在外部可以直接訪問 # print(p1.__phone) # 這裏的__phone是私有的普通字段,在外部不可以直接訪問 p1.func_print1() # func_print1是共有的方法,在外部可以直接訪問 # p1.__func_print2() # __func_print2是私有的方法,在外部不可以直接訪問 ret = p1.func_print2() print(ret) # 私有方法可以在內部通過共有的方法調用直接,可以間接的達到在外部執行內部私有的方法 # 子類也可以執行父類的私有方法 # 子類不可以使用父類的私有的字段 class cuihongyan(person): def __init__(self,address,phone,emill): super(cuihongyan,self).__init__(address,phone) self.emill = emill def test_cuihongyan(self): # self.__func_print2 print("---------------") print(self.emill) super(cuihongyan, self).func_print1() # super(cuihongyan,self).__func_print2() # print(self.__phone) # print(self.__age) # __phone和__age都屬於是父類的私有的普通字段和靜態字段,這裏在子類中都不可以訪問

面向對象中的幾個特殊的方法

#Auther Bob
#--*--conding:utf-8 --*--
 
class test(object):
    def __init__(self,name):
        self.name = name
    def __call__(self, *args, **kwargs):
        print("xxxx")
    def __int__(self):
        return 345
    def __str__(self):
        return "aaaaaaa"
t = test("cui")
 
# ======================================================================================================
t()
# 對象+括號就會執行__call__方法
# ======================================================================================================
print(int(t))
 
# int(對象)就會自動執行對象中的__int__方法,然後把好這個方法的返回值接收到
# ======================================================================================================
print(str(t))
 
# str(對象)就會自動執行對象中的__str__方法,然後把這個方法的返回值接收到
 
print(t)
# print(對象)這個方法也是默認會執行對象中的str方法,然後接受str方法的返回值接收到,效果等同於print(str(對象))
# ======================================================================================================
 
class test1(object):
    def __init__(self,age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
 
 
t1 = test1(20)
t2 = test1(30)
 
print(t1 + t2)
# 我這裏定義了一個__add__這個函數,那麽如果object1+object2相加,那麽就會調用object1對象的__add__方法,然後把第二個對象當做參數傳遞給__add__這個
# 函數,用other來接受
 
# 同樣加減乘除的效果都和加是一樣的
 
# ======================================================================================================
class test2(object):
    def __init__(self,age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
    def __del__(self):
        print("對象被銷毀了")
 
t1 = test2(20)
 
# 對象的__del__方法,就是對象被銷毀的時候自動執行的,由python自動執行的.
# ======================================================================================================
class test3(object):
 
    name = "alex"
    def __init__(self,age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
 
 
t1 = test3(20)
 
print(t1.__dict__)
print(test3.__dict__)
 
# __dict__打印對象中的的字段,而只打印普通字段,而不打印靜態字段
#__dict__打印類中的成員
# ======================================================================================================
 
class test3(object):
    name = "alex"
 
    def __init__(self, age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
    def __getitem__(self, item):
        return item * 10
t = test3(20)
 
print(t[20])
 
# 使用對象[item]的方式,就會自動調用類的__getitem__方法
 
# ======================================================================================================
class test3(object):
    name = "alex"
 
    def __init__(self, age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
    def __getitem__(self, item):
        return item * 10
    def __setitem__(self, key, value):
        print(key,value)
 
t = test3(30)
t["ke"] = "v1"
 
# 使用  對象["k"] = "v"就會自動執行對象中的__setitem__方法
# ======================================================================================================
class test3(object):
    name = "alex"
 
    def __init__(self, age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
    def __getitem__(self, item):
        return item * 10
    def __setitem__(self, key, value):
        print(key,value)
    def __delitem__(self, key):
        print(key)
 
t = test3(20)
 
del t[39]
 
 
# del 對象[值] 就會自動調用類的__delitem方法
# ======================================================================================================
class test3(object):
    name = "alex"
 
    def __init__(self, age):
        self.age = age
 
    def __add__(self, other):
        return self.age + other.age
    def __getitem__(self, item):
        return item * 10
    def __setitem__(self, key, value):
        print(key,value)
    def __delitem__(self, key):
        print(key)
    def __iter__(self):
        return iter(["a","b","c","d"])
 
t = test3("30")
for i in t:
    print(i)
 
# 如果對象中有__iter__方法,那麽說明這個對象是一個可叠代的對象
 
 
# 如果對對象執行for循環進行可叠代對象
# 1、執行類的__iter__方法,然後獲取他的返回值,這裏要用iter來返回
# 2、然後在for循環中循環這個返回值
# ======================================================================================================

面向對象中的特殊的成員修飾符和幾個特殊的方法