工程项目结构如下:
包AnimalShow和Class_test是同级包,AnimalShow是父类,Gound,Sea,Sky继承它,Chicken继承Gound和Sky
首先是Gound.py引入Animal模块,如下
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from AnimalShow.Animal import Animals
class Gound_Animal(Animals) :
def __init__(self , name , age , message):
self.age = age
self.name = name
self.message = message
print("Gound_Animal初始化完毕")
def printA(self):
print("name : %s, age : %d, message : %s" % (self.name, self.age, self.message))
def GG(self):
print("我是Gound_Animal独有方法")
Sky和Sea同理
下面是同级包的导入,Chicken引入Gound和Sky,如下,
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from AnimalShow import Gound_Animals,Sky_Animals
class chicken(Gound_Animals.Gound_Animal,Sky_Animals.Sky_Animal):
def __init__(self, name, age, message):
self.age = age
self.name = name
self.message = message
print("chicken初始化完毕")
def printA(self):
print("name : %s, age : %d, message : %s" % (self.name, self.age, self.message))
def CC(self):
print("我是chicken独有方法")
if __name__ == "__main__":
GA = Gound_Animals.Gound_Animal("陆地动物",10,"我是陆地动物")
CK = chicken("小鸡",2,"小鸡")
CK.printA()
CK.CC()
CK.GG()
CK.hobby()
测试结果: