import sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from p1 import Person
class Student(Person):
def __init__(self,name,height,stno,school):
super().__init__(name,height) #super为继承父类的属性
self.stno =stno
self.school=school
def do_homework(self):#子类私有的属性
print('我是'+self.name+'我在'+self.school+'上学,我在写作业')
class Person:
'''这是人类,使学生和工人的父亲,人类有名字和升高,可以跑步和说话'''
def __init__(self, name, height):
super.__init__(name,height)
self.name = name
self.height = height
def run(self):
print(self.name + '在跑步。')
def say(self):
print(self.name + '在说话。')
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from p1 import Person
class Worker(Person):
def __init__(self,name,height,factory):
super().__init__(name,height)
self.factory=factory
def work(self):
print('我是'+self.name+'我在'+self.factory+'工作')
# def say(self):
# print(self.name + '在说话。')
最后运行调用的代码?
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from w1 import Worker
#
#
# student = Student('堂七七',175,'180','解放小学')
#
# student.do_homework()
# print(student.name)
# print(student.height)
#
# student.say()
# student.run()
worker=Worker('堂七七',185,'欧亚卖场')
worker.work()
#
# work.say()
# worker.run()
from ch3.w1 import Worker报错
错误? modulenotfounderror: no module named 'ch3'
如果不放心自己目录有没有导入,或者报错,可以添加以下两行代码,一步解决问题。
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|