python成员修饰符

成员修饰符

  1. 公有成员
  2. 私有成员

特殊成员
init 类() 自动执行
del
call 对象() 或 类()() 自动执行
int int(对象)
str str()

add
dict 将对象中封装的所有内容通过字典的形式访问
getitem
setitem
delitem

iter
如果类中有 iter 方法,对象 --> 可迭代对象
对象.iter()的返回值,就是迭代器
for 循环,迭代器, next
for 循环,可迭代对象,对象.iter(),迭代器,next
1. 获取 foo 对象的类Foo中的 __iter__方法,并获取其返回值
2. 循环上一步中返回的对象

class Foo:

    def __init__(self,name,age):
        # 字段前面加两个下划线,私有变量,外部无法访问
        self.__age = age
        self.name = name

    def show(self):
        return self.__age

foo = Foo('lroyle',18)
name = foo.name
print(name)

age = foo.show()
print(age)

class Foo:
    __v = '123'

    def __init__(self):
        pass

    def show(self):
        return Foo.__v

    @staticmethod
    def stat():
        return Foo.__v

# print(Foo.__v)
foo = Foo()
ss = Foo().show()
print(ss)

ss2 = Foo.stat()
print(ss2)

class Foo:
    def __f1(self):
        return 123

    def f2(self):
        r = self.__f1()
        return r


foo = Foo()
ss = foo.f2()
print(ss)

class F:
    def __init__(self):
        self.ge = 789
        self.__game = 123

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.ge)


s = S('lroyle')
s.show()

class Foo:
    def __init__(self):
        print('init')

    def __call__(self, *args, **kwargs):
        print('call')

# foo = Foo()
# foo()

Foo()()

class Foo:

    def __init__(self):
        pass
    def __int__(self):
        return 111
    def __str__(self):
        return 'sss'

foo = Foo()

print(foo,type(foo))

# int对象,自动执行对象的__int__方法,并将返回值赋值给int对象
print(int(foo))
print(str(foo))

class Foo:

    def __init__(self,n,s):
        self.name = n
        self.age = s
    def __str__(self):
        return '%s-%s'%(self.name,self.age)

foo = Foo('lroyle',18)
print(foo)  #print(str(foo)) --> str(foo)  foo中__str__,并获取其返回值

class Foo:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __add__(self, other):
        # self = foo1('lroyle',18)
        # other = foo2('alfie',19)

        # return self.age + other.age
        # return Foo('lisa',99)
        return Foo(foo1.name,other.age)
    
    def __del__(self):
        print('析构方法')   # 对象被销毁时,自动执行

foo1 = Foo('lroyle',18)
foo2 = Foo('alfie',19)

s = foo1 + foo2
# 两个对象相加时,自动执行第一个对象的 __add__方法,并且 将第二个对象当作值传递进入
print(s,type(s))

class Foo:

    def __init__(self,name,age):
        pass

# foo = Foo('lroyle',18)
# d = foo.__dict__
# print(d)

ret = Foo.__dict__
print(ret)

class Foo:

    def __init__(self,name,age):
        self.naem = name
        self.age = age

    def __getitem__(self, item):
        # return item+10
        # 如果 item 是基本类型, int, str,索引获取
        # 如果是slice 对象的话,切片
        if type(item) == slice:
            print(item.start)
            print(item.stop)
            print(item.step)
            print('调用这希望内部做切片处理')
        else:

            print('调用这希望是索引处理')
    def __setitem__(self, key, value):
        print(key,value)

    def __delitem__(self, key):
        print(key)

li = Foo('lroyle',18)
rr = li[8]   # 自动执行 li 对象的类中的 __getitem__方法,8当作参数传递给item
print(rr)

li[100] = '888'
print(li[100])

del li[999]

print(li[123])
li[1:5:3]

class Foo:

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __iter__(self):
        return iter([self.name,self.age])

foo = Foo('lroyle',18)

# 如果类中有 __iter__ 方法,对象 --> 可迭代对象
# 对象.__iter__()的返回值,就是迭代器
# for 循环,迭代器, next
# for 循环,可迭代对象,对象.__iter__(),迭代器,next
# 1. 获取 foo 对象的类Foo中的 __iter__方法,并获取其返回值
# 2. 循环上一步中返回的对象
for i in foo:
    print(i)
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值