您的位置:首页 > 编程语言 > Java开发

【转载】java继承中的构造方法(super)

2017-01-06 21:23 204 查看
class Tip1{ private String title; private String content; private int publishtime; Tip1(){ System.out.println("我输出的是无参的构造方法"); } Tip1(String title){ System.out.println("我输出的有参构造的构造方法"); this.title = title; } }
class topic1 extends Tip1{ private String topicId; private String boardId; topic1(){ super("canshu"); System.out.println("我是子类的无参构造方法"); } topic1(String topicId){ System.out.println("我是子类的有参构造方法"); this.topicId = topicId; } } public class TestSuper {
public static void main(String[] args) { // TODO Auto-generated method stub topic1 t1 = new topic1("hello"); topic1 t2 = new topic1();  }
} ***输出结果*** 我输出的是无参的构造方法 我是子类的有参构造方法 我输出的有参构造的构造方法 我是子类的无参构造方法     http://blog.sina.com.cn/s/blog_685ab04101012qeq.html     该例子摘抄自以上网址。     个人暂时得出结论:当子类中存在对父类的继承时,若所调用的子类方法中无Super(),则默认继承父类中的无参构造方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐