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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM