相关疑难解决方法(0)

在Python 3.x中继承python的对象是必要的还是有用的?

在旧的python版本中,当你在python中创建一个类时,它可以从对象继承,这是我理解的一个特殊的内置python元素,它允许你的对象成为一个新式的对象.

那么新版本(> 3.0和2.6)呢?我用google搜索了类对象,但是得到了很多结果(显而易见的原因).任何提示?

谢谢!

python python-3.x

74
推荐指数
2
解决办法
1万
查看次数

子类不继承父类

我的代码遇到了问题.我正在尝试创建一个继承父类的属性和方法的子类,但它不起作用.这是我到目前为止所拥有的:

class Employee(object): 
  def __init__(self, emp, name, seat):
    self.emp = emp
    self.name = name
    self.seat = seat
Run Code Online (Sandbox Code Playgroud)

下面的代码块有些问题 - 子类.

我必须__init__再次创建吗?我如何为子类创建一个新属性.从阅读问题,听起来像__init__在子类中将覆盖父类 - 如果我调用它来定义另一个属性是真的吗?

class Manager(Employee): 
  def __init__(self, reports):
    self.reports = reports
    reports = [] 
    reports.append(self.name) #getting an error that name isn't an attribute. Why? 

  def totalreports(self):
    return reports
Run Code Online (Sandbox Code Playgroud)

我希望Employee类中的名称位于报告列表中.

例如,如果我有:

emp_1 = Employee('345', 'Big Bird', '22 A')
emp_2 = Employee('234', 'Bert Ernie', '21 B')

mgr_3 = Manager('212', 'Count Dracula', '10 C')

print mgr_3.totalreports()
Run Code Online (Sandbox Code Playgroud)

我想,reports = …

python inheritance attributes parent-child

5
推荐指数
1
解决办法
5278
查看次数