简体   繁体   中英

Change a inherited class to another inherited class keeping the attributes

I need to change a inherited class to another inherited class where only one of the attributes has changed

i need to "Promote" a Cashier to a Manager, the only thing that is suppose to change is the salary

both Cashier and Manager are inherited classes of Employee (where I'm not sure if I'm using the "hasattr" function the right way)

class Employee:
  def __init__(self,name):
    self.name=name
    if(hasattr(self,'shifts')==False):
        self.shifts=[]
class Manager(Employee):
  def __init__(self,name,salary):
    Employee.__init__(self,name)
    self.salary=salary
class Cashier(Employee):
  def __init__(self,name,salarey_per_hours):
    Employee.__init__(self,name)
    self.salery_per_hours=salarey_per_hours
  def promote(self,salary):
    return Manager(self.name,salary)

Ps It's my first time uploading a question

What you could do is create the addition method of your class and add self to the manager class you are returning like so:

class Employee(object):
    def __init__(self, name):
        self.name=name
        if not hasattr(self, 'shifts'):
            self.shifts = []

    def __add__(self, other):
        if isinstance(other, Employee):
            for key, value in other.__dict__.items():
                if key == 'salary':
                    continue
                self.__setattr__(key, value)
        return self


class Manager(Employee):
    def __init__(self, name, salary):
        super().__init__(name)
        self.salary = salary


class Cashier(Employee):
    def __init__(self,name,salary):
        super().__init__(name)
        self.salary = salary

    def promote(self, salary):
        manager = Manager(self.name, salary)
        manager += self
        return manager


cashier = Cashier('hank', 22)
cashier.shifts = [1, 2, 3, 4]
print(cashier.shifts)
promoted_cashier = cashier.promote(30)
print(promoted_cashier.shifts)

Here you make sure that everything except the "salary" is transferred to the promoted class. And since both the Manager and the Cashier are an Employee this should work nicely. I changed your code a bit to what I'm used to since there was some unusual coding with you Calling Employee in the init which I assumed you did not explicitly needed. Sorry if that was not the case.

You can change the object's class by obj.__class__ to the another class by doing obj.__class__ = SomeClass

Beware that is can lead to strange behaviours if it is handled incorrectly.

by modifying your code

class Employee:
  def __init__(self,name):
    self.name=name
    if(hasattr(self,'shifts')==False):
        self.shifts=[]

class Manager(Employee):
  def __init__(self,name,salary):
    Employee.__init__(self,name)
    self.salary=salary

class Cashier(Employee):
  def __init__(self,name,salarey_per_hours):
    Employee.__init__(self,name)
    self.salery_per_hours=salarey_per_hours
  def promote(self,salary):
    self.__class__ = Manager
    # return Manager(self.name,salary)

You can also take a look at this post changing the class of a python object (casting)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM