Java 静态方法不能重写但可以被子类静态方法覆盖

强调

静态方法是属于类的,只存在一份,会被该类的所有对象共享。不可以被重写。

静态方法可以被子类继承,但是不可以被子类重写

class door{
	
}
class wood_Door extends door{
	
}

class math{
	static public door getMes() {
		return new door();
	}
}


public class HelloWorld extends math {
	
	public static wood_Door getMes() {
		return new wood_Door();
	}
	
	public static void main(String[] args) {
		math m=new HelloWorld();
		System.out.println(m.getMes());

	}
}
//输出为door@161cd475

子类定义的方法与超类的私有方法或者静态方法同名
对超类的私有方法,由于无法派生给子类,因此子类在定义与该类方法同名的方法时不存在任何前提限制。他们的关系可以描述为:重定义,即重新定义类中的成员。
子类不能通过继承重写父类的静态方法,但是可以隐藏父类的方法,如下


class door{
	
}
class wood_Door extends door{
	
}

class math{
	static public door getMes() {
		return new door();
	}
}


public class HelloWorld extends math {
	
	public static wood_Door getMes() {
		return new wood_Door();
	}
	
	public static void main(String[] args) {
		HelloWorld m=new HelloWorld();
		System.out.println(m.getMes());

	}
}
//输出为wood_Door@532760d8

posted @ 2018-10-30 13:27  灰太狼的喜羊羊  阅读(3645)  评论(1编辑  收藏  举报