this(name,age);
super(name,age);
两个语句什么意思,分别用在什么时候?区别是什么?
为什么不能同时在一个构造函数中出现?为什么只能作为构造函数中的第一句出现?

解决方案 »

  1.   

    this(name,age);调用本类的两个参数的构造函数
    super(name,age);调用父类的两个参数的构造函数为什么不能同时在一个构造函数中出现?为什么只能作为构造函数中的第一句出现?
    王八的屁股-规定
      

  2.   

    this是指本类
    super是指父类
      

  3.   

    this(name,age);调用本类的两个参数的构造函数
    super(name,age);调用父类的两个参数的构造函数ls的说话太不文明了,却是是规则,但是lz喜欢思考,不错,加油
      

  4.   

    this(name,age);调用本类的两个参数的构造函数
    super(name,age);调用父类的两个参数的构造函数
      

  5.   

    假定父类有private int x ,y;两个参数,且构造器对x,y进行了初始化;
    又假定其子类又增加了一个private z,现在子类的构造器需要对x,y,z三个变量进行初始化,子类的构造器必须先通过super 来调用父类的构造器对x,y初始化,然后才能对z进行初始化。
    为什么要用super呢?因为父类的private int x,y;只能用父类的方法对其进行访问,子类的方法是不能直接对其访问的。有了super关键字,子类就可以通过它来标识调用父类的构造器对x,y进行初始化。this是用来指定使用自身的变量或方法的关键字,有了它可以用来解决变量或方法名字冲突的问题。
      

  6.   

    this(name,age);调用本类的两个参数的构造函数
    super(name,age);调用父类的两个参数的构造函数
      

  7.   

    我是来看 “有一个菜鸟java问题” 那个菜鸟是谁
      

  8.   


    this调用自身的构造函数貌似只能在第一行!!! super随意
      

  9.   

    楼上说的不对,this和super都只能放在构造器里面的第一行,为什么都要第一行呢,因为构造器就是为了创建对象,肯定要先创建对象才能做其他事情,所以要在第一行,而不是所谓的规定,是逻辑上要这样
      

  10.   

    不好意思 写错了,这两个方法都得写在一开始,super随便写即成super.dosomething()调用方法了
      

  11.   

    因为this(msg,name);这个构造函数已经把父类的构造函数给重写了。jvm去找的时候肯定不给子类逾越权限嘛。这个就跟你是你老爸的儿子,你可以调用你老爸的东西,也可以重写你老爸的东西。但是你不能取代你老爸的位置好吧
      

  12.   

    this(name,age);调用本类的两个参数的构造函数
    super(name,age);调用父类的两个参数的构造函数
      

  13.   

    构造函数,this就是当前这个类,super是这个类的父类
      

  14.   

    谁他妈说不是java的规定的?
    java language specification里面8.6.5 Constructor Body节写的明明白白,清清楚楚!
    8.6.5 Constructor Body
    The first statement of a constructor body may be an explicit invocation of another
    constructor of the same class, written as this followed by a parenthesized argument
    list, or an explicit invocation of a constructor of the direct superclass, written
    as super followed by a parenthesized argument list.
    构造函数体的第一条语句可以是对本类的另一个构造函数的明确调用,使用this后面加上括弧和参数列表的形式,或者是对直接的父类构造函数的明确的调用,使用super后面加上括弧和参数列表的形式。
    ConstructorBody:
    { ExplicitConstructorInvocationopt BlockStatementsopt }
    ExplicitConstructorInvocation:
    this ( ArgumentListopt ) ;
    super ( ArgumentListopt ) ;
    It is a compile-time error for a constructor to directly or indirectly invoke
    itself through a series of one or more explicit constructor invocations involving
    this.
    构造函数体里面,使用this通过一个或者多个明确的构造函数的调用来直接或者间接的调用它自己,是一种编译时错误。
    If a constructor body does not begin with an explicit constructor invocation
    and the constructor being declared is not part of the primordial class Object, then
    the constructor body is implicitly assumed by the compiler to begin with a superclass
    constructor invocation “super();”, an invocation of the constructor of its
    direct superclass that takes no arguments.
    如果构造函数体没有以明确的构造函数的调用开头,并且声明构造函数的类不是Object类,那么,构造函数体隐含的以对父类构造函数的调用“super()”开始,这是一个对直接父类的无参的构造函数的调用。
    An explicit constructor invocation statement may not refer to any instance
    variables or instance methods declared in this class or any superclass, or use this
    or super in any expression; otherwise, a compile-time error occurs.
    一个明确的构造函数的调用语句不能够引用本类或者父类的实例变量或者实例方法,或者是带有this或者super关键字的表达式,否则就是编译时错误。
      

  15.   

    this(name,age);
    super(name,age);
    两个语句什么意思,分别用在什么时候?区别是什么?
    //this,是对最近方法名的引用。super代表调用父类。
    为什么不能同时在一个构造函数中出现?为什么只能作为构造函数中的第一句出现?
    //同时在一个构造函数中容易起冲突。