OOP类:根据被调用的方法覆盖子类中的静态方法

时间:2021-12-15 22:49:50

I have the ParentClass, with method TestMethod (NOTE: I shouldn't make any changes to the ParentClass at all, it should stay as it is):

我有ParentClass,方法是TestMethod(注意:我根本不应对ParentClass进行任何更改,它应保持不变):

class ParentClass{ 
    public static function TestMethod() { 
        return "original method"; 
    } 
}

And the child class that extends original class has two additional methods (ChildMethod1 and ChildMethod2) that are not included in the ParentClass:

扩展原始类的子类有两个不包含在ParentClass中的方法(ChildMethod1和ChildMethod2):

class ChildClass extends ParentClass{ 

    public function ChildMethod1() { 
        .
        .
        . 
    } 
    public function ChildMethod2() { 
        .
        .
        .
    } 

}

I'm instancing and working with an objects from ChildClass. But I need to override the TestMethod from the ParentClass.

我正在实例化并使用ChildClass中的对象。但我需要从ParentClass重写TestMethod。

In the moment when I will call the ChildMethod1, the TestMethod from the ParentClass should be overriden and to look like this:

在我将调用ChildMethod1的那一刻,来自ParentClass的TestMethod应该被覆盖并且看起来像这样:

public static function TestMethod() { 
    return "child method 1"; 
} 

and in the moment when I will call the ChildMethod2, the TestMethod should be overridden like this:

在我将调用ChildMethod2的那一刻,应该覆盖TestMethod,如下所示:

public static function TestMethod() { 
    return "child method 2"; 
} 

so probably the ChildClass should look like this:

所以ChildClass可能看起来像这样:

class ChildClass extends ParentClass{ 

    public function ChildMethod1() { 
        .
        .
        . 
    } 
    public function ChildMethod2() { 
        .
        .
        .
    } 

    public function TestMethod() {
        if(some condition){
             return "child method 1"; 
        } else {
             return "child method 2"; 
        }
    }

}

NOTE: TestMethod in ParentClass is actually a static function.

注意:ParentClass中的TestMethod实际上是一个静态函数。

Is there any way of implementing this logic without any change to the ParentClass? I was thinking maybe introducing the variable in ChildClass can help us in the creating the conditions in overriden class, but not sure how to implement all of that.

有没有任何方法可以实现这个逻辑而不需要对ParentClass进行任何更改?我想也许在ChildClass中引入变量可以帮助我们在覆盖类中创建条件,但不知道如何实现所有这些。

1 个解决方案

#1


0  

As far as I know, since a static method isn't part of a class instance, it can't be overridden. This is by design in every programming language in the world.

据我所知,由于静态方法不是类实例的一部分,因此无法覆盖它。这是世界上每种编程语言的设计。

If these static methods were instance methods...

...probably you would end up implementing two derived class of ParentClass and that conditional logic would be implemented where you instantiate the so-called derived classes, and this means that you would be taking advantage of polymorphism:

...可能你最终会实现两个派生类的ParentClass,并且在实例化所谓的派生类时会实现条件逻辑,这意味着你将利用多态:

ParentClass instance;

if([condition])
{
    instance = new DerivedClass1();
}
else
{
    instance = new DerivedClass2();
}

    instance.OverriddenMethod();
}

When you cast a reference of a most derived instance into a less derived one, you're performing an upcast. While you're upcasting either DerivedClass1 or DerivedClass2, you're not losing their implementations of OverriddenMethod, because upcasting just reduces the typing without losing implementation details.

当您将最派生的实例的引用转换为较少派生的实例时,您正在执行向上转换。当您向上转换DerivedClass1或DerivedClass2时,您不会丢失其OverriddenMethod的实现,因为向上转换只会减少键入而不会丢失实现细节。

#1


0  

As far as I know, since a static method isn't part of a class instance, it can't be overridden. This is by design in every programming language in the world.

据我所知,由于静态方法不是类实例的一部分,因此无法覆盖它。这是世界上每种编程语言的设计。

If these static methods were instance methods...

...probably you would end up implementing two derived class of ParentClass and that conditional logic would be implemented where you instantiate the so-called derived classes, and this means that you would be taking advantage of polymorphism:

...可能你最终会实现两个派生类的ParentClass,并且在实例化所谓的派生类时会实现条件逻辑,这意味着你将利用多态:

ParentClass instance;

if([condition])
{
    instance = new DerivedClass1();
}
else
{
    instance = new DerivedClass2();
}

    instance.OverriddenMethod();
}

When you cast a reference of a most derived instance into a less derived one, you're performing an upcast. While you're upcasting either DerivedClass1 or DerivedClass2, you're not losing their implementations of OverriddenMethod, because upcasting just reduces the typing without losing implementation details.

当您将最派生的实例的引用转换为较少派生的实例时,您正在执行向上转换。当您向上转换DerivedClass1或DerivedClass2时,您不会丢失其OverriddenMethod的实现,因为向上转换只会减少键入而不会丢失实现细节。