A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 张向辉 于 2013-2-3 11:18 编辑

子类构造函数里的super

13 个回复

正序浏览
类的构造的过程中必须调用其基类的构造方法
*子类可以在自己的构造方法中使用super调用基类的构造方法
(使用this调用本类的另外的构造方法)
如果调用super,必须写在子类构造方法的第一位上
*如果子类没有显示调用基类的构造方法,系统默认使用基类的无参构造方法
*如果子类既无显示调用基类的构造方法,而系统又没有无参构造方法,那编译错误

实例:
//定义测试类PersonText。。。
public class PersonText {

public static void main(String[] args) {

//实例化Person类 p1,p2
  Person p1=new Person("A");
  Person p2=new Person("B","shanghai");

//实例化student s1,s2
  Student s1=new Student("C", "s1");
  Student s2=new Student("C", "shanghai","s2");



  System.out.println(p1.info());
  System.out.println(p2.info());
  System.out.println(s1.info());
  System.out.println(s2.info());
  
}

}

//定义父类Person
class Person {
private String name;

private String location;
Person(String name){       //父类构造函数
  this.name=name;
  location="beijing";
}
Person(String name,String location){
  this.name=name;
  this.location=location;
}
public String info(){     //父类中你的方法info
  return "name:"+name+"\tlocation:"+location+"\t";
}

}

//子类Student 继承父类Person

class Student extends Person{
    private String school;
Student(String name,String school) {//子类中的构造方法
  this(name,"beijing",school);  
}
Student(String n,String l,String school){
  super(n,l);
  this.school=school;
}
    public String info(){
     return super.info()+"school:"+school;
    }
}

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
木有明白楼主的意思啊。
回复 使用道具 举报
不知所云.......
回复 使用道具 举报
本帖最后由 杨浩 于 2013-2-1 23:01 编辑
沙漠里的小鱼 发表于 2013-2-1 22:14
是不是不管子类有没有构造函数  都要首先调用父类的构造函数  如果子类的构造函数带参数  则就是把这个参 ...
受楼上启发,我改了下代码
  1. class Demo01{
  2.         int x;
  3.         String str = "abc";
  4.         Demo01(){System.out.println("这里是老纸!");}
  5.         Demo01(int x){
  6.                 System.out.println("这里是有参数的老纸!");
  7.         }
  8. }

  9. class Demo02 extends Demo01{
  10.         Demo02(){System.out.println("老纸算个叼呀。。。!");}//这里也有默认有一个隐藏的super(),所以下面的有参构造最后依然会调用到父类的无参构造
  11.         Demo02(int x){
  12. //                super();//这里默认有一个隐藏的super();如果你写了this(),那么默认的就没了
  13.                 this();
  14.                 System.out.println("你有参数也没用。。。。");
  15.         }
  16. }

  17. public class Demo{
  18.         public static void main(String[] args){
  19.                 new Demo02();
  20.                 new Demo02(2);
  21.         }
  22. }
复制代码
我也顺便复习了一下,呵呵!
你可以改下,或增加孙子类,看下效果。。。呵呵

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1

查看全部评分

回复 使用道具 举报
这是我做黑马基础测试时的相关答案,希望可以对你有所帮助。
回复 使用道具 举报
/**
* 9、 有这样三个类,Person,Student.GoodStudent。
* 其中Student继承了Person,GoodStudent继承了Student,三个类中只有默认的构造函数,
* 用什么样的方法证明在创建Student类的对象的时候是否调用了Person的构造函数,
* 答案1:运行代码,下面的"Student,来调用你老爸我吧!"输出,则足以证明,创建Student类的对象的时候是否调用了Person的构造函数。
* 在创建GoodStudent类的对象的时候是否调用了Student构造函数?如果在创建Student对象的时候没有调用Person的构造函数,
* 那么采用什么样的手段可以调用父类的构造函数?
* 答案2:如果3个类都只有默认的构造函数,那么在创建GoodStudent类的对象之前,会创建其父类的对象,也就是Student的构
* 造函数会被调用,因为Student是GoodStudent的直接父类,不仅是这样,Person类的构造函数也会被调用,而且在Student之
* 前。其实这样的情形会首先建立Object的对象,因为Object是所有类的父类, 因此最先被调用的构造函数是Object的构造函数。
*
* @author 陈科宇(Keyee Chan)
*/
class PersonA{
    PersonA()
    {
     System.out.println("Student,来调用你老爸我吧!");
    }
}
class StudentA extends PersonA{
  
}
class GoodStudentA extends StudentA{
   
}
public class Test9 {
    public static void main(String args[]){
        StudentA student=new StudentA();
    }
}
回复 使用道具 举报
陈科宇 发表于 2013-2-1 22:19
LZ是提问呢还是传播知识呢?

提问  构造函数那搞不清楚
回复 使用道具 举报
LZ是提问呢还是传播知识呢?
回复 使用道具 举报
杨浩 发表于 2013-2-1 21:54
LZ想表达什么。。。
子类的构造方法里,即使不写,也会先调用父类的构造方法的。
this()和super() 不能在同 ...

是不是不管子类有没有构造函数  都要首先调用父类的构造函数  如果子类的构造函数带参数  则就是把这个参数传递给父类的构造函数   如果父类没有这种参数类型的构造函数  则报错
回复 使用道具 举报
LZ想表达什么。。。
子类的构造方法里,即使不写,也会先调用父类的构造方法的。
this()和super() 不能在同一个里面,那样就变成调用两次了。
如果子类有多个构造方法,那么必须有一个去调用父类的构造方法。
回复 使用道具 举报
默认五参构造函数
回复 使用道具 举报
。。。。。。。。。。。
回复 使用道具 举报
在这里子类未定义构造器,系统将会提供默认构造器
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马