public class Base {
 public static final String FOO = "foo";
 public static void main(String[] args) {
  Base b = new Base();
  Sub s = new Sub();
  System.out.print(Base.FOO);
  System.out.print(Sub.FOO);
  System.out.print(b.FOO);
  System.out.print(s.FOO);
  System.out.print(((Base) s).FOO); // 把s对象强制转换为 Base 类型
 }
}
class Sub extends Base {
 public static final String FOO = "bar";
}

输出:

foobarfoobarfoo

System.out.print(); // 输出语句

(Base) s // 把s对象强制转换为 Base 类型

((Base) s).FOO // 获取s对象的 FOO成员属性

相关文章:

  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
猜你喜欢
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-01-22
  • 2021-04-15
相关资源
相似解决方案