简体   繁体   English

将参数传递给void方法(整数或对象)

[英]Passed parameter into void method (integer or object)

I'm trying to find out what is the output of the following code and I almost got all of them right except 2 lines. 我试图找出以下代码的输出,除了两行外,我几乎都正确了。 Line 3 and 7. 3号线和7号线

The expected output would be: 预期的输出将是:

1 : 10 
2 : 20 
3 : 20 
4 : 15 
5 : 40 
6 : 75 
7 : 35 
8 : 75 
9 : 20

As I mentioned I got everything right except 3 and 7. Line 3 for me was 25 and Line 7 was 40. Could you guys please explain why is line 3: 20 and line 7: 35? 正如我提到的,除了3和7外,我一切都正确。对于我来说,第3行是25,而第7行是40。请您解释一下为什么第3行:20和第7行:35? I tried to look it up before but couldn't find a good explanation. 我曾尝试过查找它,但找不到很好的解释。

It's not a homework! 这不是功课!

public class Alpha {
  protected int x;

  public Alpha() { this(10); }
  public Alpha(int x) { this.x = x; }

  public void f() { x = 20; }
  public void f(int x) { x = 25; }
  public void g(Object a) { x = 30; }
  public void h(Object a) { x = 50; }
  public void h(Integer a) { x = 55; }
}

public class Beta extends Alpha {
  public Beta() { super(15); }
  public void g(Object a) { x = 35; }
  public void g(Integer a) { x = 40; }
  public void h(Object b) { x = 70; }
  public void h(Integer b) { x = 75; }

  public static void main(String[] args) {
    Alpha a = new Alpha();
    System.out.println("1 : " + a.x);
    a.f();
    System.out.println("2 : " + a.x);
    a.f(100);
    System.out.println("3 : " + a.x);

    Beta b = new Beta();
    System.out.println("4 : " + b.x);
    b.g(200);
    System.out.println("5 : " + b.x);
    b.h(300);
    System.out.println("6 : " + b.x);

    Alpha c = b;
    c.g(400);
    System.out.println("7 : " + c.x);
    c.h(500);
    System.out.println("8 : " + c.x);
    c.f();
    System.out.println("9 : " + b.x);
  }
}

For problem #3, the variable a is an Alpha . 对于问题3,变量aAlpha Before af(100) is called, af() is called, which sets x to 20 . 在调用af(100)之前,将调用af() ,它将x设置为20 While calling af(100) , the local variable x gets set to 25 in the method f(int x) , not the instance variable x . 在调用af(100)局部变量x在方法f(int x)设置为25 ,而不是实例变量 x The instance variable remains 20 . 实例变量仍为20

For problem #7, the variable b , which is a Beta , gets assigned to c , an Alpha reference. 对于问题7,将变量b (即Beta )分配给Alpha引用c When cg(400) is called, the compiler must make a call to g(Object) , because that is the only overload of g that is available in Alpha . 调用cg(400) ,编译器必须调用g(Object) ,因为这是Alphag唯一可用的重载。 At runtime, polymorphism dictates that because the object is a Beta , the override of g(Object) in Beta is chosen, which sets x to 35 . 在运行时,多态决定,由于对象是Beta ,的重写g(Object)Beta被选择,它设置x35 This is because the compiler chooses the method overload while the method override is chosen at runtime with polymorphism. 这是因为编译器选择方法重载,而在运行时使用多态选择方法重写。

The reason line 3 is 20 is because you are changing the value of 'x' passed into the method and not the class level variable 'x'. 第3行之所以是20,是因为您要更改传递到方法中的'x'的值,而不是类级别变量'x'。 Change your code to the below. 将您的代码更改为以下内容。 If you read up on variable scope you'll get a better understanding too. 如果您阅读可变范围的书,您也会得到更好的理解。

public class Alpha {
  protected int x;

  public Alpha() { this(10); }
  public Alpha(int x) { this.x = x; }

  public void f() { x = 20; }
  public void f(int x) { this.x = 25; }
  public void g(Object a) { x = 30; }
  public void h(Object a) { x = 50; }
  public void h(Integer a) { x = 55; }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM