super()语句的应用困惑(知识源至于。成员变量的隐藏和方法的覆盖)
我的代码就是为了试着实现以下的功能: 子类在隐藏了父类的成员变量或重写了父类的方法或,有时还需要用到父类的成员变量,或在重写的方法中被重写的方法以简化代码。
程序代码:
class SuperClass{
int x;
superClass(){
x=3;
System.out.println("in superClass: x="+x);
}
void doSomething(){
System.out.println("in superClass.doSomething()");
}
}
class subclass extends superClass{
int x;
subclass(){
super(); //call constructor of superClass
x=5;
System.out.println("in subclass: x="+x);
}
void doSomething(){
super.doSomething(); //call method of superClass
System.out.println("in subclass.doSomething()");
System.out.println("super x="+super.x+" sub.x="+x);
}
}
public class Inheritance{
public static void main(String[] args){
subclass subC=new subclass();
subC.doSomething();
}
}课本上给的程序代码:结果编译时出现:
E:\JAVA\Test01>javac Inheritance.java
Inheritance.java:3: 方法声明无效;需要返回类型
superClass(){
^
1 错误
我发现课本给的代码错误好多(自认为,也希望高手指出一些课本上代码的错误之处),于是我运用我所学的知识改成了:
程序代码:
class SuperClass{
int x;
void superClass(){
x=3;
System.out.println("in superClass: x="+x);
}
void doSomething(){
System.out.println("in superClass.doSomething()");
}
}
class SubClass extends SuperClass{
int x;
void subClass(){
super(); //call constructor of superClass
x=5;
System.out.println("in subClass: x="+x);
}
void doSomething(){
super.doSomething(); //call method of superClass
System.out.println("in subClass.doSomething()");
System.out.println("super x="+super.x+" sub.x="+x);
}
}
public class Inheritance{
public static void main(String[] args){
SubClass subC=new SubClass();
subC.doSomething();
}
}
结果:
E:\JAVA\Test01>javac Inheritance.java
Inheritance.java:15: 对 super 的调用必须是构造函数中的第一个语句
super(); //call constructor of superClass
^
1 错误
当我把super(); //call constructor of superClass这行注释了,编译通过出现结果:
E:\JAVA\Test01>javac Inheritance.java
E:\JAVA\Test01>java Inheritance
in superClass.doSomething()
in subClass.doSomething()
super x=0 sub.x=0
可是这不是我希望的结果,我希望的结果是:
in superClass:x=3
in subClass:x=5
in superClass.doSomething()
in subclass.doSomething()
super.x=3 sub.x=5
希望高手解释一下:super()等用法:----------------解决方案--------------------------------------------------------
class SuperClass{
int x;
SuperClass(){
x=3;
System.out.println("in superClass: x="+x);
}
void doSomething(){
System.out.println("in superClass.doSomething()");
}
}
class SubClass extends SuperClass{
int x;
SubClass(){
super();
x=5;
System.out.println("in subClass: x="+x);
}
void doSomething(){
super.doSomething(); //call method of superClass
System.out.println("in subClass.doSomething()");
System.out.println("super.x="+super.x+" sub.x="+x);
}
}
public class Inheritance {
public static void main(String[] args) {
SubClass subC = new SubClass();
subC.doSomething();
}
}
ps:
super()是用在构造函数中的
----------------解决方案--------------------------------------------------------
你的问题很简单
class SuperClass{
int x;
superClass(){
大写!
----------------解决方案--------------------------------------------------------
版主,你所说的大写。
我说句实话,我真的看不明白你的意思!
----------------解决方案--------------------------------------------------------
一个类的构造函数要与类名完全一样(注意开头大小写也要一样)
class SuperClass{
int x;
superClass(){
构造函数应该是 SuperClass(){
[本帖最后由 usbboy2009 于 2009-8-6 14:02 编辑]
----------------解决方案--------------------------------------------------------
LS正解
----------------解决方案--------------------------------------------------------
刚运行过了,全部都是大小的引起的错误。可能楼主打错字母了!
superClass(){ //SuperClass
x=3; System.out.println("in superClass: x="+x);
}
----------------解决方案--------------------------------------------------------