简体   繁体   English

如何从java中的两个不同类调用相同的方法

[英]How to call same method from two different classes in java

I have two classes Apple and Orange like this: 我有两个类AppleOrange这样:

public final  class Apple{
    int getJuice();
}
public final class Orange{
    int getJuice();
}

and I cannot change them . 我无法改变它们 I have third class fruitsManeger like this: 我有这样的第三类fruitsManeger

class FruitManeger {
     Apple apple;
     Orange orange;

     enum Fruits{
        Apple,Orange
     }

     Fruits favorFruits;    

     int getJoice(){
         if(favorFruits==Fruits.Apple){
               apple.getJuice();
         }else if(favorFruits==Fruits.Orange){
               orange.getJuice();
         } 
     }

}

My Question: what is best way to implement getJuice method in FruitManeger class?. 我的问题:在FruitManeger类中实现getJuice方法的最佳方法是什么?

as you can see if I have a lot of fruits I should add a lot of if else expiration. 你可以看到,如果我有很多水果,我应该添加很多if else expiration。

of course, I can use reflection to call methods by name but it's not a good idea when getJuice method return an object and you want to do something same with that. 当然,我可以使用反射来按名称调用方法,但是当getJuice方法返回一个对象而你想要做同样的事情时,这不是一个好主意。

is there any better way? 有没有更好的方法?

is there any better way? 有没有更好的方法?

Yes, Refactor that application into a more oop way... 是的,将该应用程序重写为更加明确的方式......

Remember: you cannot Modify Apple or Orange, but OOP allows you extending those classes for sure since they are not final... 记住:你不能修改Apple或Orange,但OOP允许你扩展这些类,因为它们不是最终的...

class CorrectApple extends Apple implement IJuiceble{
    @Override
    int getJuice();
}
class CorrectOrange extends Orange  implement IJuiceble{
    @Override
    int getJuice();
}

now the 现在

interface IJuiceble{
    int getJuice();
}

and finally the manager: 最后经理:

class FruitManeger {
     private CorrectApple apple;
     private CorrectOrange orange;

     int getJoice(IJuiceble correctfruit){
         return correctfruit.getJuice();
     }
}

You can use switch instead of if/else. 您可以使用switch而不是if / else。

class FruitManager {
    Apple apple;
    Orange orange;

    enum Fruits {
        Apple, Orange
    }

    Fruits favorFruits;

    int getJoice() {
        switch (favorFruits) {
        case Apple:
            apple.getJuice();
            break;
        case Orange:
            orange.getJuice();
            break;

        default:
            System.err.println("Fruit not found. " + favorFruits);
        }
    }

By referring this Answer 通过参考这个答案

interface IScalable {
   void setScale(int scale);
   int getScale();
}

class DataSizeAction extends EncoderAction implements IScalable {
   ...
}

class SomeoneElse {
   private int scale = 2;

   public void setScale(IScalable scalable) {
      scalable.setScale(this.scale);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 java 中的不同类调用相同方法的最佳方法 - Best way to call same method from different classes in java 调用两个不同类的相同方法 - Call same method of two different classes Java多态性-如何在不同的类中调用相同的方法 - Java polymorphism - how to call same method in different classes 如何在Java中为两个不同的对象调用相同的方法 - How to call the same method for two different objects in Java 如何从同一个java项目中的不同类调用另一个方法 - How to call another method from a different class in the same java project 如何从java中不同包中的同一对象调用方法? - how to call a method from same object in different packages in java? 如何在不同的包中为两个生成的类编写一个通用方法,但在 Java 中都是相同的方法 - how to write a common method for two generated classes in different packages but all same methods in Java Java:如何在从不同类实例化的不同对象中调用具有相同名称的方法? - Java : how to call methods with the same name in different objects instantiated from different classes? 从两个不同的类有条件地调用具有相同名称的方法 - Conditionally invoke method with same name from two different classes 如何在两个不同的类中使用相同的add()方法 - How to use the same add() method in two different classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM