标签:列表 相同 function prot print 函数 重复 ide 父类
public class SomeTrying{ public static void main(String[] args){ new Son(); new Son().Father(); } } class Father{ //父类Father的无参构造函数 public Father (){ System.out.println("Father Construtor"); } //父类Father的带参构造函数 ,构造函数不是成员函数,它只代表类本身,所以一个类可以有多个构造函数 public Father (int x){ System.out.println("Father Construtor " + x); } /* 父类Father的成员函数,就是类中的普通函数,所以是不可以重复的,如果再写一个如下, * 编译器会报 Duplicate method Father() in type Father 错误,原因是两个成员函数发生了冲突。 public int Father (){ System.out.println("Father member function"); */ public void Father (){ System.out.println("Father member function"); } } //类son是继承父类Father的子类 class Son extends Father { //在子类的构造函数中调用父类构造函数, public Son (){ super(1); System.out.println("Son Construtor"); } //重写父类的成员函数 public void Father(){ System.out.println("Inherited Father member function."); super.Father(); } }
1、
因为类中显式的声明了一个带参数构造器,所以默认的构造器就不存在了,但是你在子类的构造器中并没有显式
的调用父类的构造器(创建子类对象的时候,一定会去调用父类的构造器,这个不用问为什么),没有显式调用
的话,虚拟机就会默认调用父类的默认构造器,但是此时你的父类的默认构造器已经不存在了,这也就是为什么父
类中必须保留默认构造器的原因。
PS.应该养成良好的编程习惯,任何我们自己定义的类,都显式的加上默认的构造器,以后更深入的学习之后,
会发现有很多好处
2、方法重写规则
参考:http://www.runoob.com/java/java-override-overload.html
http://blog.csdn.net/yuxiaohui78/article/details/43887373
http://www.cnblogs.com/by-dream/p/3967123.html
标签:列表 相同 function prot print 函数 重复 ide 父类
原文地址:http://www.cnblogs.com/WXBai/p/6136883.html