/*
方法重写的注意事项:
A:父类中私有方法不能被重写
因为父类私有方法子类根本就无法继承
B:子类重写父类方法时,访问权限不能更低
最好一致
C:父类静态方法,子类也必须通过静态方法进行重写
其实这个算不上方法重写,但是现像如此,至于为什么算不上方法重写,多态中理解
子类重写父类的时候,最好声明一模一样
*/
class Father{
//private void show(){}
/*
public void show(){
System.out.println("show Father");
}
*/
void show(){
System.out.println("show Father");
}
/*
public static void mathod(){
}
*/
public void mathod(){
}
}
class Son extends Father{
//private void show(){}
/*
public void show(){
System.out.println("show Son");
}
*/
public void show(){
System.out.println("show Son");
}
public static void mathod(){
}
/*
public void mathod(){
//Son 中的 mathod() 无法覆盖 Father 中的 mathod();被覆盖的方法为 static
//public void mathod(){
//1 错误
}
*/
}
class ExtendsDemo10{
public static void main(String[] args){
//创建对象
Son s = new Son();
s.show();
}
}
面向对象_方法重写的注意事项
原创
©著作权归作者所有:来自51CTO博客作者wx62dfdc6aea345的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:面向对象_static的内存图解
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++读取NC格式数据的方法与注意事项
本文介绍基于C++ 语言的netCDF库读取.nc格式的栅格文件时,出现数据无法读取、数据读取错误、无法依据维度提取变量等情况的原因与解决方法~
NC格式数据 栅格数据 .nc格式 netCDF C++ -
02Java面向对象基础-9. 构造方法的注意事项
02Java面向对象基础-9. 构造方法的注意事项*
java 构造方法 自定义 初始化 -
java中重写Object类的equals方法和Hashcode方法的注意事项
1、对称性:如果x.equals(y)返回是“true”,那么y.equals(x)也应该返回是“true”。 2、反射性:x.equals(x)必须返回是“true”。
Hashcode java中重写 Object类的 equals方法和 方法的注意事项