黑马程序员技术交流社区
标题:
求解this关键字 的用法之一 this(name);
[打印本页]
作者:
山鹰
时间:
2013-5-24 09:55
标题:
求解this关键字 的用法之一 this(name);
本帖最后由 山鹰 于 2013-5-24 13:16 编辑
this(name);这样的形式在什么情况下使用?和this.name = name;有什么区别?代码如下
public class Person {
private String name;
private int age;
Person(String name){
this.name = name;
}
Person(String name, int age) {//有参构造函数
this(name);//this关键字
this.age = age;
}
public void ShowInfo() {//行为方法
System.out.println(name + "---->" + age);
}
}
public class PersonDemo1 {
public static void main(String[] args) {
Person p = new Person("张三", 23);
Person p1 = new Person("李四", 24);
p.ShowInfo();
p1.ShowInfo();
}
}
作者:
student
时间:
2013-5-24 10:17
本帖最后由 student 于 2013-5-24 10:21 编辑
this(name)
表示调用当前对象的构造方法:
Person(String name){
this.name = name;
}
复制代码
this.name = name
表示:将实参name的值复制给Person对象的成员变量name。
你在创建Person对象:
Person p = new Person("张三", 23)
时,其实调用了两次构造方法。
第一次
,
new Person("张三", 23),调用有两个参数的构造方法
,如下:
Person(String name, int age) {//有参构造函数
this(name);//this关键字
this.age = age;
}
复制代码
第二次
,上面的构造方法中出现 this(name),调用一个参数的构造方法
,如下:
Person(String name){
this.name = name;
}
复制代码
如果将有两个参数的构造方法的this(name)改为this.name=name效果是一样的,只不过只调用一次构造方法。
作者:
金辉
时间:
2013-5-24 10:22
Person(String name, int age) {//有参构造函数
this(name);//this关键字 这里就相当于上面的 this.name = name;
this.age = age;
}
this(name)是调用自身的含有一个参数的构造方法,这里的name只是参数,如果有两个参数,就代表调用有两个参数的构造方法
this.name = name;是局部变量name赋值给当前对象的name。
this代表的是一个对象。哪个对象调用this所在的函数,this就代表哪个对象
this就是当前对象的一个引用。
this表现形式一:当局部变量和成员变量同名的时候,可以用this来区分
表现形式二:构造函数之间的调用可以使用this关键字,后面跟上小括号,制定具体的实参即可明确要调用的构造函数。
特殊情况:调用本类中构造函数的this语句必须定义在构造函数的第一行
因为初始化动作要先完成。
作者:
王永贺
时间:
2013-5-24 10:23
this(name)的意思是调用带有一个参数的构造方法也就是程序中的
Person(String name){
this.name = name;
}方法从而也实现了this.name = name
作者:
x378320002
时间:
2013-5-24 10:23
this(name),意思是调用本引用所指对象的,以(name)为参数的构造方法,
可能咋看起来不习惯,楼主应该熟悉super()吧,就是调用父类的空参构造函数
,super(name)就是调用父类name单参数构造函数,同理this(),和this(name)就是
调用相应构造函数.对比起来,比较好理解.
作者:
棉/mg花/x糖
时间:
2013-5-24 10:25
楼主,您好,不妨参考一下我的这个帖子,应该就会明白了。
http://bbs.itheima.com/thread-51403-1-1.html
作者:
孙茜茜
时间:
2013-5-24 10:26
this(name);
在构造函数之间调用对方的返回值时必须这样写,不能用普通函数的调用格式,并且这句话也只能在构造函数间使用。
this.name = name;
在类中成员变量和局部变量名称相同时,前面那个赋值对象带一个this表示它是对象里的name变量,后面那个name只是形参。
综:this(name);的this代表构造函数,this.name = name;的this代表传进来操作的那个对象。
作者:
陈潜
时间:
2013-5-24 10:45
public class Person {
private String name;
private int age;
//构造方法1
Person(String name){
this.name = name;
}
//构造方法2
Person(String name, int age) {//有参构造函数
this(name);//this()就是表示构造方法,this(name)表示调用传入name参数的构造方法,其实就是调用构造方法1;
this.age = age;//this.age = age表示把传入的age参数的值 赋给 本类的age(就是this.age)
}
public void ShowInfo() {//行为方法
System.out.println(name + "---->" + age);
}
}
复制代码
this()用在构造方法的相互调用。
作者:
Super_Class
时间:
2013-5-24 12:35
this.name = name; 是赋值
this(name);是调用当前类中参数为String类型的构造函数
当然你也可以使用
Person(String name ,int age){
Person(name) ; //这里与this(name);等效
this.age = age;
}
作者:
王瀛
时间:
2013-5-24 12:42
class Person {
private String name;
private int age;
Person(String name){
this.name = name;
}
Person(String name, int age) {
this(name);//"this语句",仅用于构造函数间进行调用。
this.age = age;
}
public void ShowInfo() {
System.out.println(name + "---->" + age);
}
}
class PersonDemo1 {
public static void main(String[] args) {
Person p = new Person("张三", 23);
Person p1 = new Person("李四", 24);
p.ShowInfo();
p1.ShowInfo();
}
}
复制代码
类中的成员之间,都是通过对象进行调用的。this代表的是调用其所在函数的对象。
即:this代表其所在函数所属对象的引用。谁调用this所在函数,this就代表哪个对象。
Person类中定义了两个构造函数Person(String name){}和Person(String name,int age)。
其中都包含this.name = name 代码重复。
这时,把这部分"提取出来",以this(name)的形式表现。
比如:main函数中Person p = new Person("张三",23)这句话执行时,调用了Person类,所以this就代表p。
当对对象进行初始化时,调用了Person(String name,int age)这个构造函数,此函数第一句可以看作p(name),于是又跳转到Person(String name)中进行初始化;于是Person(String name)的第一句中的this这时就可看作p,即:p.name=name,从而完成初始化动作。
总之记住:
在类内部,成员间的调用都是由对象来完成的。谁调用this所在函数,谁this就代表谁(对象)。
this()这种写法是为了提高提高代码复用性,表示共性的初始化语句。这种写法仅仅存在于类中构造函数之间的调用。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2