在子类对象java上调用父类的方法 [英] call parent class's method on a child class object java

查看:175
本文介绍了在子类对象java上调用父类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个扩展其他类的类.它们都有通用的方法result.如果我有foobarbaz的实例,是否可以调用其父/祖父母的result方法?

I have several classes that extend others. They both have a common method result. If I have an instance of foobarbaz, is it possible to call its parent's/grandparent's result method?

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;      }
}

我要解决的问题是,每个类除了其父类之外,还要进行一些额外的计算.如果用户希望获得所有3个对象的结果,则CalculationManager知道仅需要初始化和计算Foobarbaz.然后,它将向任何请求Foo的人返回对Foobarbaz的引用,因为Foobarbaz也会为Foo得到结果.

The problem I'm trying to solve is that each class does some extra calculation, besides the one of its parent. If user wants results from all 3 objects, the CalculationManager knows only Foobarbaz needs to be inited and calculated. Then it returns an reference to Foobarbaz to whoever is asking for Foo, because Foobarbaz will have a result for Foo as well.

类似:

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 

推荐答案

设计原理:继承时偏重组成

获取一份"Head First设计模式"书的副本,并阅读有关策略",模板"以及工厂模式"的信息.在那里讨论的其他模式肯定会在该项目或其他项目中派上用场.

Get a copy of the Head First Design Patterns book and read about Strategy, Template and maybe also Factory patterns. Other patterns discussed in there will certainly come in handy elsewhere in this project or projects to come.

可能的情况:

  • 具有Foo,FooBar和FooBarBaz类,它们是从抽象类AbstractFoo扩展而来的
  • 为AbstractFoo提供了计算器myCalc类型的属性(以及与此对应的getter和setter)
  • 将Calculator设置为具有方法doCalc()的接口,该接口将由实现此接口的类来实现
  • 创建实现自己的doCalc()版本的所有可能的Calculator实现(CalculatorX,CalculatorY等)
  • 赋予AbstractFoo一个公共的int calc()方法,该方法调用myCalc.doCalc()方法以获得所需的结果

当您担心不同的Calculator实现具有通用代码时,您可以使用Template模式或从BaseCalculator等扩展它们.

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.

这应该使您忙上一段时间,但是如果您正确地做并掌握了它,您会发现在许多编程情况下使用这种模式都可以帮助您.因此,我猜是名称模式;-)

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 ;-)

这篇关于在子类对象java上调用父类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆