静态方法是不能被覆盖的! 在SUN的SL-275中明确写道: you CANNOT override a STATIC method but you can hide it!
pensincun(天高任彭翔) 看下面一段代码 class Person { static String name,department; public static void printValue(){System.out.println("name is "+name); System.out.println("department is "+department); } } class teacher extends Person { static int salary; public static void printValue(){ System.out.println("salary is "+salary); } public static void main(String args[]){ teacher t=new teacher(); t.printValue(); } }printValue()算不算被覆盖?
//to:misslyy(礼拜二)下面的程序段才能输出你所想要的结果! class Person { static String name,department; public void printValue(){ //这里必须去掉static修饰符System.out.println("name is "+name); System.out.println("department is "+department); } } class Teacher extends Person { static int salary; public void printValue(){ //这里也必须去掉static修饰符 super.printValue(); System.out.println("salary is "+salary); } public static void main(String args[]){ Teacher t=new Teacher(); t.printValue(); } } //实现了真正的方法覆盖!
//静态方法不可以覆盖 class Person { static String name,department; public static void printValue(){ System.out.println("name is "+name); System.out.println("department is "+department); } } class teacher extends Person { static int salary; public static void printValue(){ System.out.println("salary is "+salary); } public static void main(String args[]){ Person t=new teacher();// t.printValue(); } } 输出 name is null department is null
静态方法不能override,能overload
不可以。出错信息如下: Error #: 455 : static method aMethod() in class DadClass cannot be overridden by method aMethod() in class SonClass at line 21, column 8
override 和 hide 有什么区别?
oveerride 是运行时动态绑定, static 方法是编译器绑定。A extends BA a = new A(); B b = a; b.method(); // if override, here will invoke method in A, if method is static , B.method will be invoked.
在SUN的SL-275中明确写道:
you CANNOT override a STATIC method but you can hide it!
class Person { static String name,department; public static void printValue(){System.out.println("name is "+name); System.out.println("department is "+department); } }
class teacher extends Person {
static int salary; public static void printValue(){
System.out.println("salary is "+salary);
}
public static void main(String args[]){
teacher t=new teacher();
t.printValue();
}
}printValue()算不算被覆盖?
静态方法不参与类对象而调用方法的覆盖机制。
其在编译时就被打包,所以运行时调用那个方
法由句柄类型决定,而不是由它包含的对象类
型来确定。
salary is 0
而不是
name is null
department is null
salary is 0 呢
方法被声明为final,则成为最终方法,不能为覆盖了。
class Person { static String name,department; public void printValue(){ //这里必须去掉static修饰符System.out.println("name is "+name); System.out.println("department is "+department); } }
class Teacher extends Person {
static int salary; public void printValue(){ //这里也必须去掉static修饰符
super.printValue();
System.out.println("salary is "+salary);
}
public static void main(String args[]){
Teacher t=new Teacher();
t.printValue();
}
}
//实现了真正的方法覆盖!
class Person { static String name,department; public static void printValue(){ System.out.println("name is "+name); System.out.println("department is "+department); } }
class teacher extends Person {
static int salary; public static void printValue(){
System.out.println("salary is "+salary);
}
public static void main(String args[]){
Person t=new teacher();//
t.printValue();
}
}
输出
name is null
department is null
Error #: 455 : static method aMethod() in class DadClass cannot be overridden by method aMethod() in class SonClass at line 21, column 8
B b = a;
b.method();
// if override, here will invoke method in A, if method is static , B.method will be invoked.