- /**
- * 9、 有这样三个类,Person、Student、GoodStudent。
- * 其中GoodStudent继承于Student,Student继承于Person。
- * 如何证明创建GoodStudent时是否调用了Person的构造函数?
- * 在GoodStudent中是否能指定调用Student的哪个构造函数?
- * 在GoodStudent中是否能指定调用Person的哪个构造函数?
- *答:如果,创建GoodStudent对象,可以打印出来Person类中定义的语句,及证明了调用了Person的构造函数
- * 在GoodStudent中是可以指定调用Student的那个构造函数,也可以指定调用Person的那个构造函数
- * 想要调用指定的构造函数,可以用super(),也可以实例化对象
- * @author
- *
- */
- public class Test9 {
- public static void main(String[] args) {
- //实例化GoodStudent对象
- GoodStudent gs = new GoodStudent();
- }
- }
- //创建Person,Student,GoodStudent类,并指明继承关系
- class Person {
- //分别调用不同参数的函数
- //无参构造
- Person () {
- System.out.println("这是Person的无参构造函数");
- }
- Person(String str) {
- System.out.println("这是含有一个参数的Person的构造函数");
- }
- Person(String str,String str1){
- System.out.println("这是含有两个参数的Person的构造函数");
- }
- }
- class Student extends Person{
- Student(){
- System.out.println("这是Student类的无参构造函数");
- }
- Student(String str){
- System.out.println("这是含有一个参数的Student类的构造函数");
- }
- Student(String str,String str1){
- System.out.println("这是含有两个参数的Student类的构造函数");
- }
- }
- class GoodStudent extends Student{
- GoodStudent(){
- super();
- System.out.println("GoodStudent");
- //实例化Person对象
- Person p = new Person("First Student");
- //实例化Student对象
- Student s = new Student("First Student","Second Student");
- }
- }
复制代码
这是我做的,你参考一下吧 |