简体   繁体   中英

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.

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? 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 . Before af(100) is called, af() is called, which sets x to 20 . While calling af(100) , the local variable x gets set to 25 in the method f(int x) , not the instance variable x . The instance variable remains 20 .

For problem #7, the variable b , which is a Beta , gets assigned to c , an Alpha reference. 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 . 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 . 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'. 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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