Java学习笔记七---父类构造方法有无参数对子类的影响
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java学习笔记七---父类构造方法有无参数对子类的影响,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3066字,纯文字阅读大概需要5分钟。
内容图文
子类不继承父类的构造方法,但父类的构造方法对子类构造方法的创建有影响。具体来说就是:
①.当父类没有无参构造方法时,子类也不能有无参构造方法;且必须在子类构造方法中显式以super(参数)的形式调用父类构造方法。否则会出现如下的错误:
Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor
子类在有参构造方法中显式调用super(参数)后,如果再写一个无参构造方法,则会出现下面的错误:
Implicit super constructor Person() is undefined. Must explicitly invoke another constructor
②.父类有无参构造方法时,子类可以有无参构造方法,也可以有有参构造方法;在有参构造方法中,可以用super显式调用父类构造方法也可以不调用。也就是说,这时候,子类在构造方法的创建上是比较自由的。
下面是简单示例:
有两个类,Person类和Student类,Student类继承自Person类。两个类的构造方法详见代码。
Person类:
package human; public class Person { String name; int age; String gender; private String hobby; public Person() { } public Person(String n, String g) { this.name = n; this.gender = g; } public Person(String n, int a, String g, String h) { this.name = n; this.age = a; this.gender = g; this.hobby = h; } public void setName(String n) { this.name = n; } public void setAge(int a) { this.age = a; } public void setGender(String g) { this.gender = g; } public void setHobby(String h) { this.hobby = h; } public String getName() { return this.name; } public int getAge() { return this.age; } public String getGender() { return this.gender; } public String getHobby() { return this.hobby; } public void informationPrint() { System.out.println("My name is " +this.getName()); System.out.println("I am " + this.getAge() +" years old"); if(this.getGender() == "female") System.out.println("I am a girl"); else if(this.getGender() == "male") System.out.println("I am a boy"); else System.out.println("Something is wrong!"); System.out.println("My hobby is " + this.hobby); } }
Student类:
package human; public class Student extends Person { String stuNumber; int score; public Student() { } public Student(String n, String g) { super(n,g); } public Student(String n, int a, String g, String h) { super(n,a,g,h); } public Student(String sN, int s) { this.stuNumber = sN; this.score = s; } public Student(String n, String g, String sN, int s) { super(n,g); this.stuNumber = sN; this.score = s; } public Student(String n, int a, String g, String h, String sN, int s) { super(n,a,g,h); this.stuNumber = sN; this.score = s; } public void setStuNumber(String num) { this.stuNumber = num; } public void setScore(int s) { this.score = s; } public String getStuNumber() { return this.stuNumber; } public int getScore() { return this.score; } public void informationPrint() { super.informationPrint(); System.out.println("My number is " + this.stuNumber); System.out.println("My score is " + this.score); } }
测试类:
package human; public class TestMain { public static void main(String[] args) { Person xiaoxiP = new Person("xiaoxiP",29,"female","piano"); Person xiaonanP = new Person("xiaonanP","male"); Student xiaoxiS = new Student("xiaoxiS",28,"female","piano","124",90); Student xiaonanS = new Student("xiaonanS","male","123",98); xiaoxiP.informationPrint(); xiaoxiS.informationPrint(); xiaonanP.informationPrint(); xiaonanS.informationPrint(); } }
待学习:访问权限修饰符的问题。
原文:http://www.cnblogs.com/chanchan/p/7679716.html
内容总结
以上是互联网集市为您收集整理的Java学习笔记七---父类构造方法有无参数对子类的影响全部内容,希望文章能够帮你解决Java学习笔记七---父类构造方法有无参数对子类的影响所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。