面向对象
专注于为中小企业提供成都网站设计、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业拱墅免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。#类
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
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。