public class Test extends Parent{
int a = 20;
public static void main(String[] args) {
Test test = new Test();
test.run();
}
}class Parent {
int a = 10;
public void run() {
System.out.println(a);
}
}
为什么会输入10?
int a = 20;
public static void main(String[] args) {
Test test = new Test();
test.run();
}
}class Parent {
int a = 10;
public void run() {
System.out.println(a);
}
}
为什么会输入10?
解决方案 »
- java数据库连接不了(数据库为SQL Server 2008,代码如下)
- java显示图片
- craky.componentc.JCFrame 有这个东西吗?
- 跪求用JAVA实现8255仿真代码
- 问一个关于System.exit(0)作用的问题
- 求教 Hibernater中双向 many-to-one关系的插入问题
- 如何做到当鼠标移动到输入框的时候,里头的字就自动被选择?
- 请高手们帮帮忙,"玩"一个大家来找茬的游戏!急!谢谢了!
- 请问把colum对象的数据取出是哪个函数?
- 请教一个“Java代码中的错误原因”.
- 新手请教 我的第二个sql错在哪里 总被catch(SQLException e)捉到
- java 邮件附件中文乱码!
虽然,你在子类中也定义了一个,但是那个并不能覆盖父类的
等于说父类有一个a,子类自己也还有一个a注意,继承的时候变量不可以重写,只有方法才可以下面这样写,你可以返回子类的apublic class Test extends Parent{ int a = 20; public static void main(String[] args) {
Test test = new Test();
test.run();
}
public void run(){
System.out.println(this.a);//返回子类的a,即20
}
} class Parent {
int a = 10;
public void run() {
System.out.println(a);
}
}
Test(){
a=100;
} public static void main(String[] args) {
Test test = new Test();
test.run();
}
} class Parent {
int a = 10;
public void run() {
System.out.println(a);
}
}