【问题标题】:When I change arguments in a method other classes which are calling same very method get disturbed当我更改方法中的参数时,其他调用相同方法的类会受到干扰
【发布时间】:2021-09-04 20:02:29
【问题描述】:

我现在有三个班,一个是主班,

A 类:

class A{
  int result;    
  
  int getHeightBase() {
    return result;
  }

  void setHeightBase(int h, int b) {
   result=h*b;
  }
}

B 类:

这个类将执行与 A 不同的计算

class B{
  int result;

  int getHeightBase() {
    return result;
  }

  void setHeightBase(int h, int b) {
   result=12*h*b;
  }
}

主类:

public class Main {    

  public static void main(String[] args) {

   A a= new A();    
   B b= new B();    
  calculateArea(a,b);    
  }

void calculateArea(A obj1, B obj2){    
  obj1.setHeightBase(5,6);    
  obj2.setHeightBase(5,6);
}

}

现在,当我更改 A 类方法 setHeightBase 或 B 类方法 setHeightBase 的参数时,由于某种原因,我必须更改 MainClass 以及发送整数值的位置,我认为这与设计模式相反,谁能指导我如何摆脱这个,我想稍后更改参数,但我不想影响任何其他类或更改任何其他类,我尝试使用接口但没有得到如何摆脱这个问题 非常感谢,任何帮助将不胜感激

【问题讨论】:

  • “我认为这与设计模式相反”,不,不是,这里没有使用设计模式。您必须发送预期的参数,这是其工作原理的基础。 “我想稍后更改参数,但我不想影响任何其他类”使用更改的签名创建一个新的重载方法
  • 请提供示例,我是初学者,所以这对我有很大帮助,但正如您所说,假设其他 20 个类正在调用此方法并将 Integer 值作为 MainClass 发送,如果我更改方法并减少或增加一些参数,那么我必须更改所有其他 20 个文件才能使其正常工作,你不认为这是一个不好的做法吗?
  • 那是怎样的“坏习惯”?你期望调用不存在的方法是好的做法吗?
  • 不,我的意思是有没有一种方法我们只更改一个文件,而所有其他文件都将自动更新,任何采用该函数的接口都可以更改其参数?
  • 正如我所说:重载你的方法。我在我的回答中展示了一个例子

标签: java class methods interface arguments


【解决方案1】:

假设你有这个类:

public void printNumbers(int one, int two) {
  int result = one + two;
  System.out.println("result = " + result);
}

这是从多个其他类中调用的。您不能只更改签名(返回类型、方法名称、参数列表)并期望一切正常。

现在,假设您不希望它自动打印一个总和,但您希望能够选择要执行加法或减法的操作。

对于当前方法,不要更改任何内容。它被用于添加。因此,您将添加一个接受附加参数的重载方法:

public void printNumbers(int one, int two) {
  int result = one + two;
  System.out.println("result = " + result);
}

public void printNumbers(int one, int two, String action) {
  // action is either add or sub
  if ( !"add".equals(action) && !"sub".equals(action) ) {
    System.out.println("Error");
  } else if ("add".equals(action) {
    int result = one + two;
    System.out.println("result = " + result);
  } else {
    int result = one - two;
    System.out.println("result = " + result);
  }
}

完成此操作后,您可以将原始方法更改为:

public void printNumbers(int one, int two) {
  printNumbers(one, two, "add");
}

public void printNumbers(int one, int two, String action) {
  // action is either add or sub
  if ( !"add".equals(action) && !"sub".equals(action) ) {
    System.out.println("Error");
  } else if ("add".equals(action) {
    int result = one + two;
    System.out.println("result = " + result);
  } else {
    int result = one - two;
    System.out.println("result = " + result);
  }
}

曾经提供的功能将保持不变,而您无需更改任何其他现有代码

【讨论】:

    【解决方案2】:

    你可以使用方法重载,

      void setHeightBase(int h, int b) {
         result=12*h*b;
      }
    
      void setHeightBase(double h, double b) {
         result=12.0*h*b;
      }
    

    这将保存以前的方法,并创建一个具有准确名称和不同参数的方法。

    【讨论】:

      【解决方案3】:

      我会使用一个抽象类作为基础并从中扩展其他类。抽象类中的公共方法将充当与外界的契约。将来,如果您想公开其他方法,您可以将它们添加到接口中,并且根据功能,您可以提供默认实现。在现实世界的场景中,您可以避免更改现有合同。

      举个例子,你的类可以这样写

      abstract class Shape{
          int height;
          int base;
          
          Shape(int height, int base){
              super();
              this.height = height;
              this.base = base;
          }
          
          public int area(){
              return height * base;
          }
          
      }
      
      class Shape1 extends Shape{
      
          Shape1(int height, int base){
              super(height, base);
          }
      }
      
      class Shape2 extends Shape{
          
          Shape2(int height, int base){
              super(height, base);
          }
          
          @Override
          public int area(){
              return 12 * height * base;
          }
      }
      
      public class Test{
          public static void main(String args[]){
              Shape s1 = new Shape1(2,3);
              Shape s2 = new Shape2(2,3);
              System.out.println(s1.area());
              System.out.println(s2.area());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-16
        • 2017-06-25
        • 1970-01-01
        • 2012-06-05
        • 1970-01-01
        • 2011-01-27
        • 1970-01-01
        • 2013-05-10
        • 2013-01-07
        相关资源
        最近更新 更多