[英]call parent class's method on a child class object java
I have several classes that extend others.我有几个类可以扩展其他类。 They both have a common method result
.他们都有一个共同的方法result
。 If I have an instance of foobarbaz
, is it possible to call its parent's/grandparent's result
method?如果我有foobarbaz
的实例,是否可以调用其父/祖父的result
方法?
public class Foo {
protected int resultA;
public void calc(){ resultA=...}
public void result(){ return resultA; }
}
public class Foobar extends Foo{
protected int resultA;
public void calc(){
super.calc();
resultB=...;
}
public void result(){ return resultB; }
}
public class Foobarbaz extends Foobar{
protected int resultA;
public void calc(){
super.calc();
resultC=...;
}
public void result(){ return resultC; }
}
The problem I'm trying to solve is that each class does some extra calculation, besides the one of its parent.我要解决的问题是每个 class 除了其父项之外,还会进行一些额外的计算。 If user wants results from all 3 objects, the CalculationManager knows only Foobarbaz needs to be inited and calculated.如果用户想要所有 3 个对象的结果,CalculationManager 知道只有 Foobarbaz 需要被初始化和计算。 Then it returns an reference to Foobarbaz to whoever is asking for Foo, because Foobarbaz will have a result for Foo as well.然后它将对 Foobarbaz 的引用返回给任何请求 Foo 的人,因为 Foobarbaz 也会有 Foo 的结果。
Something like:就像是:
CalculationManager.add(Foo,Foobar,Foobarbaz);
//The following 3 calls return the same reference to a Foobarbaz object
Foo res1=CalculationManager.get(Foo);
Foobar res2=CalculationManager.get(Foobar);
Foobarbaz res3=CalculationManager.get(Foobarbaz);
CalculationManager.doCalc();
//Iterate over each object to get result with the same method .result()
res1.result(); //---> resultA
res2.result(); //---> resultB
res3.result(); //---> resultC
you would need something like this in each class:你在每个 class 中都需要这样的东西:
Foo:
printParent()
{
super.printParent();
}
FooBar:
printParent()
{
super.printParent();
}
FooBarBaz:
printParent()
{
print(); // this is the top most class
//Incredibly fragile!
}
This is not really possible.这是不可能的。 You will have to instantiate an object of the parent class in order to call its print method.您必须实例化父 class 的 object 才能调用其打印方法。
eg例如
FooBar f = new FooBar();
f.print(); //foobar
Only from within the sub class can you use super keyword to access the super class' method.只有在子 class 中才能使用 super 关键字访问超类的方法。
Design Principle: Favor composition over inheritance设计原则:优先组合优于 inheritance
Get a copy of the Head First Design Patterns book and read about Strategy, Template and maybe also Factory patterns.获取 Head First Design Patterns 这本书的副本,并阅读有关策略、模板以及工厂模式的信息。 Other patterns discussed in there will certainly come in handy elsewhere in this project or projects to come.那里讨论的其他模式肯定会在这个项目的其他地方或即将到来的项目中派上用场。
Possible scenario:可能的情况:
At the point where you worry about the different Calculator implementations have common code you can make use of the Template pattern or have them extend from a BaseCalculator etc.当您担心不同的 Calculator 实现有共同的代码时,您可以使用模板模式或让它们从 BaseCalculator 等扩展。
This should keep you busy for some time but if you do it right and get the hang of it you'll find that using patterns like this helps you in many programming situations.这应该会让你忙碌一段时间,但如果你做得对并掌握了窍门,你会发现使用这样的模式可以在许多编程情况下帮助你。 Hence the name patterns, I guess;-)因此名称模式,我猜;-)
The super calls the method of the parent class超调用父class的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.