gpt4 book ai didi

java - 为什么我们不能覆盖java中的变量

转载 作者:行者123 更新时间:2023-11-29 03:37:32 25 4
gpt4 key购买 nike

<分区>

为什么我们不能覆盖java中的变量,它隐藏了变量

class A {
int a = 4;
public void test(){
System.out.println("Test method of A" );
}
}

class B extends A {
int a = 5;
public void test(){
System.out.println("Test method of B" );
}
public static void main(String s[]){
A a= new B();
System.out.println("Value of a : " a.a );
System.out.println("Method result " a.test() );
}
}

输出::

Value of a : 4
Mthod result :Test method of B

当 B 的类测试方法被调用时,变量是从父类(super class)引用访问的

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com