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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 山鹰 中级黑马   /  2013-5-24 09:55  /  2931 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 山鹰 于 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();
               
        }
}

9 个回复

倒序浏览
本帖最后由 student 于 2013-5-24 10:21 编辑

this(name)表示调用当前对象的构造方法:
  1. Person(String name){
  2.                 this.name = name;
  3. }
复制代码


this.name = name表示:将实参name的值复制给Person对象的成员变量name。

你在创建Person对象: Person p = new Person("张三", 23)时,其实调用了两次构造方法。

第一次new Person("张三", 23),调用有两个参数的构造方法,如下:
  1. Person(String name, int age) {//有参构造函数
  2.                 this(name);//this关键字
  3.                 this.age = age;
  4.         }
复制代码

第二次,上面的构造方法中出现 this(name),调用一个参数的构造方法,如下:
  1. Person(String name){
  2.                 this.name = name;
  3.         }
复制代码

如果将有两个参数的构造方法的this(name)改为this.name=name效果是一样的,只不过只调用一次构造方法。
回复 使用道具 举报
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语句必须定义在构造函数的第一行
             因为初始化动作要先完成。
回复 使用道具 举报
this(name)的意思是调用带有一个参数的构造方法也就是程序中的
  Person(String name){
                this.name = name;
        }方法从而也实现了this.name = name
回复 使用道具 举报
this(name),意思是调用本引用所指对象的,以(name)为参数的构造方法,
可能咋看起来不习惯,楼主应该熟悉super()吧,就是调用父类的空参构造函数
,super(name)就是调用父类name单参数构造函数,同理this(),和this(name)就是
调用相应构造函数.对比起来,比较好理解.
回复 使用道具 举报
楼主,您好,不妨参考一下我的这个帖子,应该就会明白了。
http://bbs.itheima.com/thread-51403-1-1.html
回复 使用道具 举报
this(name);
在构造函数之间调用对方的返回值时必须这样写,不能用普通函数的调用格式,并且这句话也只能在构造函数间使用。
this.name = name;
在类中成员变量和局部变量名称相同时,前面那个赋值对象带一个this表示它是对象里的name变量,后面那个name只是形参。

综:this(name);的this代表构造函数,this.name = name;的this代表传进来操作的那个对象。
回复 使用道具 举报
陈潜 中级黑马 2013-5-24 10:45:07
8#
  1. public class Person {
  2.         private String name;
  3.         private int age;     
  4.         //构造方法1
  5.         Person(String name){
  6.                 this.name = name;
  7.         }
  8.         //构造方法2
  9.          Person(String name, int age) {//有参构造函数
  10.                 this(name);//this()就是表示构造方法,this(name)表示调用传入name参数的构造方法,其实就是调用构造方法1;
  11.                 this.age = age;//this.age = age表示把传入的age参数的值 赋给 本类的age(就是this.age)
  12.         }
  13.         public void ShowInfo() {//行为方法
  14.                
  15.                 System.out.println(name + "---->" + age);
  16.         }
  17. }
复制代码
this()用在构造方法的相互调用。
回复 使用道具 举报
this.name = name; 是赋值

this(name);是调用当前类中参数为String类型的构造函数
当然你也可以使用

Person(String name ,int age){
  Person(name) ;  //这里与this(name);等效
this.age = age;
}
回复 使用道具 举报
  1. class Person {
  2.         private String name;
  3.         private int age;
  4.         Person(String name){
  5.                 this.name = name;
  6.         }
  7.          Person(String name, int age) {
  8.                 this(name);//"this语句",仅用于构造函数间进行调用。
  9.                 this.age = age;
  10.         }
  11.         public void ShowInfo() {
  12.                 System.out.println(name + "---->" + age);
  13.         }
  14. }
  15. class PersonDemo1 {
  16.         public static void main(String[] args) {
  17.                 Person p = new Person("张三", 23);
  18.                 Person p1 = new Person("李四", 24);
  19.                 p.ShowInfo();
  20.                 p1.ShowInfo();
  21.         }
  22. }
复制代码
类中的成员之间,都是通过对象进行调用的。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()这种写法是为了提高提高代码复用性,表示共性的初始化语句。这种写法仅仅存在于类中构造函数之间的调用。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马