溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

面向?qū)ο蠡A(chǔ)

發(fā)布時(shí)間:2020-07-29 21:53:08 來(lái)源:網(wǎng)絡(luò) 閱讀:246 作者:一二毛錢(qián) 欄目:編程語(yǔ)言

面向?qū)ο?
'''

#類
class bar:
    def foo(self,arg):
        print(self.name,self.age,self.gender,arg)

z = bar()
#封裝公有部分到內(nèi)存里
z.name = 'hao'
z.age = 11
z.gender = '男'
z.foo(666)

z1 = bar()
z.name = 'xue'
z.age= 18
z.gender = '女'
z.foo('hahaha')
'''
'''
class bar:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def show(self):
        print('%s:%s' %(self.name,self.age))
z = bar('hao',11)
z.show()
z1 =bar('haha',33)
z1.show()

#輸出結(jié)果:
hao:11
haha:33
'''
#繼承
 class F:
     def f1(self):
         print('F.f1')
     def f2(self):
         print('F.f2')
 class s(F):
     def s1(self):
         print('s.s1')
     def f2(self):
         super(s,self).f2()  #執(zhí)行父類(基類)中的方法
         print('s.f2')

 h = s()
 h.s1()
 h.f1()
 #輸出結(jié)果: s.s1     F.f1

 h.f2()
 #結(jié)果
 F.f2
 s.f2
# class foo:
#     def __init__(self):
#         self.name = "a"
#
#     def bar(self):
#         print('bar')
#
#     @property
#     def per(self):
#         return 1
#     @per.setter
#     def per(self,var):
#         print(var)
#
# obj = foo()
#
# r = obj.per
# # print(r)
#
# obj.per = 123

class pagenation:
    def __init__(self,current_page):

        try:
            p = int(current_page)
        except enumerate as e:
            p = 1

        self.page = p

    @property
    def start(self):
        val = (self.page -1) * 10
        return val

    @property
    def end(self):
        val = self.page * 10
        return val

li = []

for i in range(100000):
    li.append(i)

while True:
    p = input('請(qǐng)輸入你的頁(yè)碼:')

    obj = pagenation(p)

    print(li[obj.start:obj.end])

輸出結(jié)果
面向?qū)ο蠡A(chǔ)

成員修飾符
'''

class F:
    def __init__(self):
        self.ag =12
        self.__ag = 34

class S(F):

    def __init__(self,name):

        self.name = name
        self.__age =18
        super(S,self).__init__()
    def show(self):
        print(self.name)
        print(self.__age)         #間接調(diào)用私有字段
        print(self.ag)
        # print(self.__ag)        #無(wú)法繼承父類的私有字段

obj = S('hao')

obj.show()
'''
'''
class Foo:
    def __init__(self):
        print('hah')

    def __call__(self, *args, **kwargs):
        print('123')

    def __int__(self):
        return 111
    def __str__(self):
        return 'hao'
Foo()()     #調(diào)用__call__方法

a = int(Foo())      #調(diào)用__int__方法
print(a)

b = str(Foo())      #調(diào)用__str__方法
print(b)
'''

# class Foo:
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#         self.n = 123
# obj = Foo('hao',18)
#
# r = obj.__dict__        #結(jié)果:{'name': 'hao', 'age': 18, 'n': 123}
# print(r)
'''
class Foo:
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.n = 123
    def __getitem__(self, item):

        return item+10
    def __setitem__(self, key, value):
        print(key,value)
    def __delitem__(self, key):
        print(key)
obj = Foo('hao',18)

r = obj[8]  #r=item
print(r)        #調(diào)用__getitem方法,結(jié)果:18

obj[10]=888     #調(diào)用__setitem方法,結(jié)果:10,888

del obj[999]    #調(diào)用__delitem方法,結(jié)果:999
'''

# class Foo:
#
#     def __iter__(self):
#
#         return iter([11,22,33,44])
# obj = Foo()
#
# for i in obj:
#     print(i)
#輸出結(jié)果
# 11
# 22
# 33
# 44
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI