不能从超类继承

时间:2018-08-26 02:44:09

标签: python class inheritance instantiation superclass

我有一个简单的代码,如下所示,其中Employee类应该从Person类继承。

/demo/project1

它给了我这个错误:

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state: {
    authData: {
      token: null,
      username: null,
      cargo: null,
      isAuthenticated: false
    }
  },
  mutations: {
    setAuthData (state, payload) {
      state.authData.token = payload.token
      state.authData.username = payload.username
      state.authData.cargo = payload.cargo
      if (payload.token) {
        state.authData.isAuthenticated = true
      } else {
        state.authData.isAuthenticated = false
      }
    }
  },
  actions: {
    setAuthData ({commit}, payload) {
      commit('setAuthData', payload)
    }
  },
  plugins: [
    createPersistedState()
  ]
})

我检查了Python documentation中有关类的信息,它在子类的class Person: def __init__(self, firstname, lastname, age): self.firstname = firstname self.lastname = lastname self.age = age def getname(self): return self.firstname, self.lastname def getage(self): return self.age class Employee(Person): def __init__(self, first, last, empid, age): Person.__init__(self, first, last, age): self.empid = empid def getemp(self): return self.getname() + ", " + self.empid employee = Employee("Bart", "Simpson", 1006, 16) print(employee.getemp()) 中没有超类的初始化。但是我发现在here这样的其他网站中,Dog继承自Pet。 所以,我想念什么?

2 个答案:

答案 0 :(得分:2)

您什么都不丢失。您需要摆脱该行上的:

:仅在原始定义之后,即def getname(self):,并始终紧跟声明该功能的缩进行。调用函数时,请勿执行此操作。

答案 1 :(得分:0)

更改

Person.__init__(self, first, last, age):

super().__init__(first, last, age)

相关问题