0

basically i have two class (Customer, EmpClass), and i tried many times to find a way in witch one employee (object of EmpClass) can perform the functionalities that are available to the Customer Class's object,ex Deposit,withdraw and so on and off course on behalf of one customer, and later on time of enquiry i can tell which employee have perform what operations for a particular customer and like that . and thanks in advance :)

my models :

class Customer(object):
''' this class contains all Customer related Information and Functions'''

    BankName = "SBH"

    def __init__(self, Cname,CacountNumber, Cbalance):

        self.Cname = Cname
        self.CacountNumber = CacountNumber
        self.Cbalance = Cbalance

    def Deposit(self, DepositAmount):
        self.Cbalance = self.Cbalance + DeposetAmount


    def WithDraw(self, WithDrawAmount):
        if WithDrawAmount > self.Cbalance:
            raise InSuffetiantFunds
        self.Cbalance = self.Cbalance - WithDrawAmount


    def C_Info(self):

        print "Bank Name :", Customer.BankName
        print "Customer Name :", self.Cname
        print "Customer Acount Number : ", self.CacountNumber
        print "Customer Balance :", self.Cbalance 

class EmpClass(Customer):
        ''' this class contains all Employee related Information and Functions'''

    BankName = "SBH"

    def __init__(self, EmpName,EmpId,):
        self.EmpName = EmpName
        self.EmpId = EmpId 
        Customer.__init__(self, Cname,CacountNumber, Cbalance)

    def E_Info(self):
        print "Bank Name :", EmpClass.BankName
        print "Employee Name:", self.EmpName
        print "Employee Id : ", self.EmpId

1 Answers1

0

In python there is super which enables you to call the inherited classes functionalities.

class EmpClass(Customer):
    def __init__(self, EmpName, EmpId, *args, **kwargs):
        self.EmpName = EmpName
        self.EmpId = EmpId 
        super(EmpClass, self).__init__(*args, **kwargs)

If you are on python3 you no longer need to pass arguments to super()

Use *args and **kwargs to pass arguments to the parent, without defining them again in the subclass.

Community
  • 1
  • 1
dahrens
  • 3,879
  • 1
  • 20
  • 38
  • Thanks Mr Dahrens, put really iam stell not geting , if you can give more explanation and examp, I have tried to create object for poth class example :c1=Customer("omer",25200000,1000000) e1=EmpClass("employee",1000,Cname=c1.Cname,Cbalance=c1.Cbalance). but indeed i am just creating another object of the EmpClass, with same customer info , so hellllppp :::)) –  Oct 02 '16 at 13:09
  • Of course you do - this is how inheritance works. Do you want the EmpClass to be related to a Customer? Than you should __not inherit__ but make customer a attribute of EmpClass and pass an instance over to `__init__` – dahrens Oct 02 '16 at 14:55
  • Yes I do want EmpClass to be related to Customer Class, but my problem is that i want EmpClass's object to be able to modify Customer's Class objects , not to create new EmpClass object , and thanks a lot for your support Mr Dahrens. –  Oct 02 '16 at 15:14
  • objects can [modify other objects without inheriting](https://gist.github.com/dahrens/967366b2e2dfc94c44c8c98733373b15) them – dahrens Oct 02 '16 at 16:07