python中封裝、繼承與多態

#定義類
# class bar:
#     def foo(self, arg):
#         print(self, self.name, self.age, self.sex, arg)
#類實例化(中間變量調用類方法)
# z = bar()
# z.name = 'alex'
# z.age = 18
# z.sex = 'female'
# z.foo(666)
#===================封裝==============================
# class bar:
#     def __init__(self, name, age): #構造方法
#         self.n = name
#         self.a = age
#     def foo(self, des):
#         print('%s-%s,%s' % (self.n, self.a, des))
# Lihuan = bar('李歡', 18, )
# Lihuan.foo('哈哈哈')
# Hu = bar('胡', 35, )
# Hu.foo('嘿嘿嘿')
#=====================繼承==========================
# 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): #重寫
#         print('S.s2')
#         super(S, self).f2() #執行父類中的f2方法
# obj = S()
#obj.s1()
#obj.f1() #調用父類的方法
# obj.f2()

#==========================多繼承========================

# class F:
#     def a(self):
#         print('F.a')
# class F1:
#     def a(self):
#         print('F1.a')
# class S(F, F1): #基類在前,執行在前的基類的方法
#     pass
# class S1(F1, F):
#     pass
# obj = S()
# obj.a() #F.a
# obj1 = S1()
# obj1.a()#F1.a


# class BaseRequest():
#     def __init__(self):
#         print('BaseRequest.init')
# class RequestHandler(BaseRequest):
#     def __init__(self):
#         print('RequestHandler.init')
#         super(RequestHandler, self).__init__()
#     def serve_forever(self):
#         print('RequestHandler.server_forever')
#         self.process_request() #函數調用函數
#     def process_request(self):
#         print('RequestHandler.prosess_request')
# class Minx():
#     def process_request(self):
#         print('Minx.prosess_request')
# class Son(Minx, RequestHandler):
#     pass
#obj = Son() #RequestHandler.init,BaseRequest.init
#obj.process_request()#Minx.prosess_request
#obj.serve_forever()#RequestHandler.server_forever,Minx.prosess_request
#=====================================類的成員之字段、方法、屬性=======================================
# class Foo:
#     def __init__(self, name):
#         self.name = name #普通字段
#     def show(self): #普通方法
#         print(self.name)
class Province:
    country = '中國' #靜態字段(屬於類,可通過對象訪問,也可以通過類訪問)
    def __init__(self, name):
        self.name = name #普通字段(屬於對象,只通過對象訪問)
    def show(self): #普通方法(保存在類中,由對象調用)
        print(Province.country, self.name)
    @staticmethod #靜態方法(保存在類中,由類調用)
    def sta():
        print('sta.123')
    @staticmethod
    def stat(a1, a2):
        print(a1, a2)
    @classmethod #類方法(保存在類中,由類調用,默認cls參數)
    def classmd(cls):
        print(cls)
        print(cls.country)
        cls.sta()
        cls.stat(1, 2)
        print('classmd')
obj = Province('安徽')
obj.show() #中國 安徽
hunan = Province('湖南')
print(Province.country, hunan.name) #中國 湖南
hubei = Province('湖北')
print(hubei.country, hubei.name) #中國 湖北
Province.sta()#sta.123 (通過類名調用靜態方法)
Province.stat(1, 2)#1 2
Province.classmd()#<class '__main__.Province'>,中國,classmd... (通過類名調用類方法)
# 類成員:1.字段(普通字段,靜態字段),2.方法(普通方法,靜態方法,類方法)

#===================類的成員之屬性==========================================

class Foo:
    def __init__(self):
        self.name = 'a'
    #執行obj.prp
    @property #屬性
    def prp(self):
        print('456')
        return 1
    #obj.prp = '789'
    @prp.setter
    def prp(self, val):
        print(val)
    #del obj.prp
    @prp.deleter
    def prp(self):
        print(666)
obj = Foo()
r = obj.prp #1
print(r)
obj.prp = '789'
del obj.prp #666

#====================利用屬性做一個簡單的分頁操作============================
class Page:
    def __init__(self, page_num):
        try:
            self.page_num = int(page_num)
        except Exception:
            self.page_num = 1
    @property
    def start(self):
        return (self.page_num-1) * 10
    @property
    def end(self):
        return self.page_num * 10
li = []
for i in range(100):
    li.append(i)
flag = True
while flag:
    page_num = input('請輸入你要查看的頁碼')
    obj = Page(page_num)
    print(li[obj.start: obj.end])
    ex = input('是否繼續y/n')
    if ex == 'n':
        print('程序已退出')
        flag = False
#===============================成員修飾符======================
#公有成員,私有成員
# class Foo:
#     __v = '123456' #私有的靜態字段
#     def __init__(self, name, age):
#         self.name = name  #公有成員
#         self.__age = age  #私有成員(外部無法直接訪問)
#     def show(self):
#         return self.__age
#     def show_v(self):
#         return self.__v
# obj = Foo('leo', 25)
# print(obj.name) #leo
# #print(obj.__age) #AttributeError: 'Foo' object has no attribute '__age'
# ret = obj.show()
# print(ret) #25
# ret_v = obj.show_v()
# print(ret_v) #123456

# class Foo:
#     def __f1(self):  #私有方法(外部無法直接調用)
#         return 123
#     def f2(self):    #通過內部方法接收,再通過外部調用
#         r = self.__f1()
#         return r
# obj = Foo()
# #print(obj.__f1()) #AttributeError: 'Foo' object has no attribute '__f1'
# print(obj.f2()) #123

# class F:
#     def __init__(self):
#         self.__gender = 'male'
# class S(F): #無法繼承父類的私有成員
#     def __init__(self, name, age):
#         self.name = name
#         self.__age = age
#     def show(self):
#         print(self.name) #leo
#         print(self.__age) #25
# obj = S('leo', 25)
# obj.show()

#============================類的特殊成員==========================
# class Foo:
#     def __init__(self):
#         print('init')
#     def __call__(self, *args, **kwargs):
#         print('call')
#     def __int__(self):
#         return 1
#     def __str__(self):
#         return 'leo'
# obj = Foo() #init
# obj() #call
# r = int(obj) #int,對象,執行對象的__int__方法,並將返回值返回給int
# print(r) #1
# s = str(obj)
# print(s) #leo

# class Foo:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     def __str__(self):
#         return '%s-%s' % (self.name, self.age)
#     def __add__(self, other):
#         return 123
# obj = Foo('leo', 18)
# obj1 = Foo('eason', 25)
# r = obj + obj1 #__add__()
# print(r) #123
# print(obj) #leo-18 1.print(str(obj)),2.obj.__str__()

# class Foo:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#         self.num =123
# obj = Foo('leo', 18)
# d = obj.__dict__
# print(d, type(d)) #{'name': 'leo', 'age': 18, 'num': 123} <class 'dict'>

# class Foo:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     def __getitem__(self, item):
#         return item+self.age
#     def __setitem__(self, key, value):
#         print(key, value)
#     def __delitem__(self, key):
#         print(key)
# obj = Foo('leo', 18)
# print(obj[8]) #26 調用__getitem__方法
# obj[100] = 'hansom' #100 hansom,調用__setitem__方法
# del obj[10] #10,調用__delitem__方法

# class Foo:
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#     def __iter__(self):
#         return iter([self.name, self.age])
# li = Foo('leo', 18)
# for i in li:
#     print(i, type(i)) 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章