1、实例的属性的值取父类还是子类并不取决于创建对象的类型,而是取决于变量定义的类型。

例:

public class Father{

  public String value="Father";

}

public class Son extends Father{

  public String value="Son ";

  public static void main(String args[]){

    Father f=new Son();

    System.out.println(f.value);//输出"Father"

  }

}

 

2、在成员函数中访问成员变量时,成员变量的值取父类还是子类取决于是调用父类还是子类的成员函数。

public class Father{

  public String value="Father";

  public String getValule(){

    return this.value;

  }

  public String getValule2(){

    return this.value;

  }

}

public class Son extends Father{

  public String value="Son ";

  public String getValule2(){

    return this.value;

  }

  public static void main(String args[]){

    Son s=new Son();

    System.out.println(s.value);//输出"Son"

    System.out.println(s.getValue());//输出"Father"

    System.out.println(s.getValue2());//输出"Son"

  }

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-05-08
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-17
  • 2022-12-23
  • 2021-12-30
  • 2021-07-24
  • 2022-12-23
  • 2022-02-04
  • 2022-02-05
相关资源
相似解决方案