#定义类
# 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 2Province.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 #1print(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 * 10li = []for i in range(100): li.append(i)flag = Truewhile 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))