简体   繁体   English

调用子类的父类方法 class object java

[英]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:可能的情况:

  • have the Foo, FooBar and FooBarBaz classes extend from an abstract class AbstractFoo让 Foo、FooBar 和 FooBarBaz 类从抽象 class AbstractFoo 扩展
  • give AbstractFoo an attribute of type Calculator myCalc (and getter and setter to go with that)给 AbstractFoo 一个 Calculator myCalc 类型的属性(以及 go 的 getter 和 setter)
  • make Calculator an interface with method doCalc() to be implemented by classes that implement this interface使计算器成为具有方法 doCalc() 的接口,由实现此接口的类实现
  • create all possible Calculator implementations (CalculatorX, CalculatorY, ..) that implement their own version of doCalc()创建所有可能的 Calculator 实现(CalculatorX、CalculatorY、..),它们实现了自己的 doCalc() 版本
  • give AbstractFoo a public int calc() method that calls on the myCalc.doCalc() method to get the results you need给 AbstractFoo 一个公共 int calc() 方法,该方法调用 myCalc.doCalc() 方法来获得您需要的结果

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.

相关问题 如何在Java中使用Child对象调用Parent类的方法 - How to call method of Parent class using Child object in Java 如何在JAVA中从父类的内部类中调用子类中的重写方法? - How can I call the overridden method in a child class from a parent class's inner class in JAVA? 有没有办法从java中的子类对象调用父类方法而不修改方法 - is there any way to call parent class method from child class object in java without modifying methods 从父类对象调用子类方法 - Call a child class method from a parent class object JAVA:如何在Object的子类中调用方法? - JAVA: How to call a method in a child class of an Object? 父类可以在Java中调用动态创建的子类方法吗? - Can Parent Class call Dynamically created child class method in java? Java:使用接口从父类调用子类方法 - Java : call child class method from parent class using interface 为什么在父类中必须强制有一个子方法以引用父类的引用来调用它,该父类引用子类的对象? - Why is it mandatory to have a child method in Parent class to call it with a reference of parent class which is referring to an object of child class? 使Java父类方法返回子类对象的方法 - Way to make Java parent class method return object of child class java中从子类对象调用父类方法 - calling parent class method from child class object in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM