简体   繁体   中英

Is it possible to change the superclass variable from a subclass's method and use it in another subclass?

Edited: Dou to @Prune comment I've edited the question

I have a main class as superclass and have many classes extended from it.

I want to change a variable of superclass from a method inside of subclass and use it in another subclass.

Assume this:

class MainClass:
    def __init__(self):
        self.test = 'GOOGLE'   # This is a variable

    def plus(x, y):
        return x + y


class SubClassOne(MainClass):
    def __init__(self):
        super().__init__()


    def substract(self, x, y):
        return x + y
        self.test = 'YAHOO'    # Here I'm trying to change the 'test'


class SubClassTwo(MainClass):
    def __init__(self):
        super().__init__()

    def multiply(self, x, y):
        print(self.test)      # Here I'm printing the 'test' and I want to have it with 'YAHOO' value
        return x * y


run = SubClassTwo()
run.multiply(2,5)

1- Look at the variable self.test = 'GOOGLE' in SuperClass initializing.

2- Then I change it in SubClassOne.substract() to self.test = 'YAHOO' .

3- I use self.test in SubClassTwo to reach the changes from SubClassOne.

Means that if i print it, I wanna YAHOO in the output. But the actual output is GOOGLE

What should I do?

You have a critical flaw in your design assumptions:

3- I use self.test in SubClassTwo to reach the changes from SubClassOne.

No, you don't. SubClassTwo and SubClassOne are sibling subclasses. self.test in SubClassTwo refers to the test attribute of a SubClassTwo instance; SubClassOne is not any part of this pedigree. SubClassTwo inherits only from MainClass. A change to the test attribute of a SubClassOne instance cannot automatically affect a SubClassTwo instance.

In your comment to the older answer, I see what may be your problem:

the methodes in runtime are consecutive in order, ClassOne then Class Two

You declared two child classes , independent of one another, not methods. substract() [sic] and multiply are methods of two different classes. For example, were you to try at the end of your posted code,

print(run.substract(5, 2))

you would get a runtime error: run has no method named substract -- run is of type SubClassTwo, which has no such method. substract is a method of SubClassOne only.


Very simply, you need to determine what you want your objects to do, and then write the corresponding Python structures to match. Since you haven't told us enough about your desired operation, we can't alter your posted code to match. Simply setting an instance attribute requires only one class, not three.

You cant get YAHOO as output by self.test of the second class, attributes and methods of classes works in that way: B inherits from A, when you create an object, and you try to execute a method or class of that object, it will be searched in the class A first then B, so if its not re-declared in B it will show the A's one, that's what happens in your case, the attribute self.test is not re-declared in SubClassTwo so its normal that it will show GOOGLE as output, you have to redefine it on that sub class also

You can make SubClassTwo inherit from SubClassOne as it will also inherit that overriding behavior:

class SubClassTwo(SubClassOne):
    def multiply(self, x, y):
        print(self.test)  # prints "YAHOO"
        return x * y

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