Class Base{ public static void stamethod(){
System.out.print("Base");}
} public class ItsOver extends Base{ public static void main(String[] args)
{ ItsOver so = new ItsOver();
so.stamethod();
}
public static void stamethod(){
System.out.print("aMethod in staOver");
}
}这个我认为是Static被重写了,但是做过SCJP的考卷,答案是Static方法不可以被重写,不懂,希高人指点。
System.out.print("Base");}
} public class ItsOver extends Base{ public static void main(String[] args)
{ ItsOver so = new ItsOver();
so.stamethod();
}
public static void stamethod(){
System.out.print("aMethod in staOver");
}
}这个我认为是Static被重写了,但是做过SCJP的考卷,答案是Static方法不可以被重写,不懂,希高人指点。
这个地方我也没有说太清楚...子类定义和父类的静态,其实是在子类中隐藏父类的静态方法..子类隐藏父类的静态方法和子类覆盖父类实例方法还是有区别的.区别在:运行时..JAVA 虚拟机把静态方法和所属的类绑定,而把实例方法和所属的实例绑定...
class Father{
void method(){
System.out.println("method of father");
}
static void staticMethod(){
System.out.println("static method of father");
}
}public class Son extends Father{
void method(){
System.out.println("method of son");
}
static void staticMethod(){
System.out.println("static method of son");
}
public static void main(String args[]){
Father son1 = new Son();//声明为Father类,son1静态方法和Father类绑
定
son1.method();
son1.staticMethod();
Son son2 = new Son();
son2.method();
son2.staticMethod();
}
}============
method of son
static method of father
method of son
static method of son
2.返回类型必须与方法中的返回类型一致。
3.访问级别的 限制不可以比父类中的强。(可以比他弱)
重载:
在同一一个类中,参数与返回的只不同,方法名相同。
静态的: 由于静态的在内存空间只有一份,所有不可以被重写.
以上写的不知道大家是否满意。
由空加376140058,讨论Java
你写的关于的重载的问题,通过看了你的那个方法我基本上明白了啊!谢谢!
但是我还有一个问题:Father son1 = new Son();//声明为Father类,son1静态方法和Father类绑
定
向上转型还有什么别的好处吗?