python面向对象基础知识_python面向对象的基础知识

python面向对象的基础知识

发布时间:2020-06-16 12:01:27

来源:亿速云

阅读:68

作者:鸽子

面向对象#类

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])

输出结果

0db7243c936b8f8dbad48e7f8d3c14f3.png

成员修饰符

'''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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值