码迷,mamicode.com
首页 > 编程语言 > 详细

Python 继承关系

时间:2018-12-27 23:01:00      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:foo   变量   catch   UNC   cat   sel   派生类   作业   class   

class Base:
    def chi(self):
        print("我会吃")


# 派生类  => 子类
class Foo(Base): # 这个类继承了Base类. Foo类是对Base的一个扩展
    def he(self):
        print("我会喝")

f = Foo()
f.chi()
f.he()


class Cat:  # 父类 => 基类  => 超类
    def catch_mouse(self):
        print("猫可以抓老鼠")

class BosiCat(Cat):  # 子类 => 派生类
    pass


class Foo:
    pass


print(hash(Foo)) # 可哈希
print(hash(Foo()))

# 我们写好的类和创建的对象默认都是可哈希的


# 去掉可哈希
class Foo:
    __hash__ = None # 当前类的对象不可哈希


print(hash(Foo)) # 可哈希
print(hash(Foo())) # TypeError: unhashable type: ‘Foo‘


class Foo:
    def chi(self, food):
        print("我爱吃鱼和", food)

class Bar:
    def chi(self, food):
        print("我爱吃肉和", food)

dic = {Foo: "鸡蛋", Bar: "香肠"}

for k, v in dic.items():
    k().chi(v)

# 类名  => 变量名 -> 为了今天作业

def func():
    pass

an = func

an()



class Base:
    def __init__(self, num):
        self.num = num
    def func1(self):
        print(self.num)
class Foo(Base):
    pass
obj = Foo(123)
obj.func1()



class Base:
    def __init__(self, num):
        self.num = num
    def func1(self):
        print(self.num)

class Foo(Base):
    def func1(self):
        print("Foo. func1", self.num)

obj = Foo(123)
obj.func1()


class Base:
    def __init__(self, num):
        self.num = num
    def func1(self):
        print(self.num)
        self.func2()
    def func2(self):
        print("Base.func2")

class Foo(Base):
    def func2(self):
        print("Foo.func2")
obj = Foo(123)
obj.func1()



class Base:
    def __init__(self, num):
        self.num = num

    def func1(self):
        print(self.num)
        self.func2()

    def func2(self):
        print(111, self.num)

class Foo(Base):
     def func2(self):
        print(222, self.num)

lst = [Base(1), Base(2), Foo(3)]
for obj in lst:
 obj.func2()



class Base:
    def __init__(self, num):
        self.num = num
    def func1(self):
        print(self.num)
        self.func2()
    def func2(self):
        print(111, self.num)

class Foo(Base):
    def func2(self):
        print(222, self.num)

lst = [Base(1), Base(2), Foo(3)]
for obj in lst:
    obj.func1()

  

Python 继承关系

标签:foo   变量   catch   UNC   cat   sel   派生类   作业   class   

原文地址:https://www.cnblogs.com/demons97/p/10187502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!