关于类的位置问题~~谢过牛人~~

dundun1513 2010-02-11 03:51:15
先贴个正确的~~


class Father {
public int age;
void info() {
age = 100;
System.out.println(age);
}
}

class Son extends Father {
public int age;
void info() {
super.info();

age = 10;
System.out.println("son: "+age+" "+this.age+" "+super.age);
}
}

public class TestOverRide {
public static void main(String[] args) {
// TODO Auto-generated method stub
Father father = new Father();
Son son = new Son();

father.info();
son.info();
}

}
但是改动了下类的位置后,就出错了,这是为什么呢? 先谢过~~ 下面是错误程序

public class TestOverRide {
class Father {
public int age;
void info() {
age = 100;
System.out.println(age);
}
}

class Son extends Father {
public int age;
void info() {
super.info();

age = 10;
System.out.println("son: "+age+" "+this.age+" "+super.age);
}
}


public static void main(String[] args) {
// TODO Auto-generated method stub
Father father = new Father();
Son son = new Son();

father.info();
son.info();
}

}
...全文
37 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dundun1513 2010-02-11
  • 打赏
  • 举报
回复
谢谢你了,了解了~~
magicbu 2010-02-11
  • 打赏
  • 举报
回复
改后father和son都变成了内部类,在外部类未实例化的情况下,内部类如何实例化!
knightzhuwei 2010-02-11
  • 打赏
  • 举报
回复
public class TestOverRide {
static class Father {
public int age;

void info() {
age = 100;
System.out.println(age);
}
}

static class Son extends Father {
public int age;

void info() {
super.info();

age = 10;
System.out
.println("son: " + age + " " + this.age + " " + super.age);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Father father = new Father();
Son son = new Son();

father.info();
son.info();
}

}

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧