面向对象基础
面向对象
'''
十载的汕尾网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整汕尾建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“汕尾网站设计”,“汕尾网站推广”以来,每个客户项目都认真落实执行。
#类
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
本文标题:面向对象基础
网页地址:http://scyanting.com/article/pggggg.html