技术博客Python

python-迭代器

2018-01-18  本文已影响0人  老生住长亭

迭代器

1、构造方法 init()

无参构造和有参构造

class Foo():

    def __init__(self, name="botter"):

        self.n = name

    def __init__(self):

        print("init method...")

foo = Foo()

print(foo.n)

继承

class Bird:

    def __init__(self):

        self.hungry = True

    def eat(self):

        if self.hungry:

            print("chi chi .")

            self.hungry = False

        else:

            print("No, thanks")

class SongBird(Bird):

    def __init__(self):

        Bird.__init__(self)

        self.song = "AKWW"

    def sing(self):

        print(self.song)

class Song1Bird(Bird):

    def __init__(self):

        super(Song1Bird, self).__init__()

        self.song = "AKWW1"

    def sing1(self):

        print(self.song)

songBird = SongBird()

songBird.sing()

songBird.eat()

song1Bird = Song1Bird()

song1Bird.sing1()

song1Bird.eat()

property 有四个参数property(fget,fset, fdel, doc)

静态方法 @staticmethodclass Myclass:

    @staticmethod

    def smeth():

        print("smeth....")

    @staticmethod

    def cmeth(cls):

        print("cmeth,,,", cls)

上一篇 下一篇

猜你喜欢

热点阅读