假定父类有private int x ,y;两个参数,且构造器对x,y进行了初始化; 又假定其子类又增加了一个private z,现在子类的构造器需要对x,y,z三个变量进行初始化,子类的构造器必须先通过super 来调用父类的构造器对x,y初始化,然后才能对z进行初始化。 为什么要用super呢?因为父类的private int x,y;只能用父类的方法对其进行访问,子类的方法是不能直接对其访问的。有了super关键字,子类就可以通过它来标识调用父类的构造器对x,y进行初始化。this是用来指定使用自身的变量或方法的关键字,有了它可以用来解决变量或方法名字冲突的问题。
谁他妈说不是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关键字的表达式,否则就是编译时错误。
super(name,age);调用父类的两个参数的构造函数为什么不能同时在一个构造函数中出现?为什么只能作为构造函数中的第一句出现?
王八的屁股-规定
super是指父类
super(name,age);调用父类的两个参数的构造函数ls的说话太不文明了,却是是规则,但是lz喜欢思考,不错,加油
super(name,age);调用父类的两个参数的构造函数
又假定其子类又增加了一个private z,现在子类的构造器需要对x,y,z三个变量进行初始化,子类的构造器必须先通过super 来调用父类的构造器对x,y初始化,然后才能对z进行初始化。
为什么要用super呢?因为父类的private int x,y;只能用父类的方法对其进行访问,子类的方法是不能直接对其访问的。有了super关键字,子类就可以通过它来标识调用父类的构造器对x,y进行初始化。this是用来指定使用自身的变量或方法的关键字,有了它可以用来解决变量或方法名字冲突的问题。
super(name,age);调用父类的两个参数的构造函数
this调用自身的构造函数貌似只能在第一行!!! super随意
super(name,age);调用父类的两个参数的构造函数
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关键字的表达式,否则就是编译时错误。
super(name,age);
两个语句什么意思,分别用在什么时候?区别是什么?
//this,是对最近方法名的引用。super代表调用父类。
为什么不能同时在一个构造函数中出现?为什么只能作为构造函数中的第一句出现?
//同时在一个构造函数中容易起冲突。