面向对象
#类
class bar:
def foo(self,arg):
print(self.name,self.age,self.gender,arg)
z = bar()
#封装公有部分到内存里
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()
#输出结果:
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() #执行父类(基类)中的方法
print('s.f2')
h = s()
h.s1()
h.f1()
#输出结果: s.s1 F.f1
h.f2()
#结果
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('请输入你的页码:')
obj = pagenation(p)
print(li[obj.start:obj.end])
输出结果
成员修饰符
'''
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) #间接调用私有字段
print(self.ag)
# print(self.__ag) #无法继承父类的私有字段
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()() #调用__call__方法
a = int(Foo()) #调用__int__方法
print(a)
b = str(Foo()) #调用__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__ #结果:{'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) #调用__getitem方法,结果:18
obj[10]=888 #调用__setitem方法,结果:10,888
del obj[999] #调用__delitem方法,结果:999
'''
# class Foo:
#
# def __iter__(self):
#
# return iter([11,22,33,44])
# obj = Foo()
#
# for i in obj:
# print(i)
#输出结果
# 11
# 22
# 33
# 44
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。