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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

李锐

注册黑马

  • 黑马币:

  • 帖子:

  • 精华:

© 李锐 注册黑马   /  2011-9-18 16:52  /  1627 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class A {
String s = "Hello";
public A(String s) {
System.out.println("s = " + s);
System.out.println("1 -> this.s = " + this.s);
this.s = s;
System.out.println("2 -> this.s = " + this.s);
}

public static void main(String[] args) {
new A("HelloWorld!");
}
}


运行结果:
s = HelloWorld!
1 -> this.s = Hello
2 -> this.s = HelloWorld!
自己在看到运行结果的时候,不明白this的用法,后面的打印结果为什么会变呢??求解释!如果,能够顺便把this的用法讲解一下就更好了!
[ 本帖最后由 李锐 于 2011-09-18  16:53 编辑 ]

评分

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

查看全部评分

7 个回复

正序浏览
黑马网友  发表于 2011-9-20 22:01:42
7#

this的用法

打印结果二楼说的很详细,在这里我就不再说了。我主要说一下this的几种用法。
this主要有三种用法:
1、表示对当前对象的引用
2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量重名时进行区分。
3、用于在构造方法中引用满足指定参数类型的构造器(也就是构造方法),但是只能引用一个构造方法且必须位于开始。

评分

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

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-9-19 08:39:26
地板

回复 地板 的帖子

这个我也有写模糊,不过现在学习了,程兄的回答很详细,很赞
回复 使用道具 举报
黑马网友  发表于 2011-9-18 22:40:35
报纸

回复 楼主 的帖子

让楼主看一个小例子,给你分享一下JAVA中“this”的用法!

/**
*  本示例为了说明this的三种用法!
*/
package test;
public class ThisTest {
private int i=0;

    //第一个构造器:有一个int型形参
    ThisTest(int i){
       this.i=i+1;//此时this表示引用成员变量i,而非函数参数i
       System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);
       System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));
       //从两个输出结果充分证明了i和this.i是不一样的!
}

    //  第二个构造器:有一个String型形参
    ThisTest(String s){
       System.out.println("String constructor:  "+s);
    }
    //  第三个构造器:有一个int型形参和一个String型形参
    ThisTest(int i,String s){
       this(s);//this调用第二个构造器
       //this(i);
       /*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。
       但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调

       用一个且仅一次构造器!*/

       this.i=i++;//this以引用该类的成员变量

       System.out.println("Int constructor:  "+i+"\n"+"String constructor:  "+s);

    }

    public ThisTest increment(){

       this.i++;

       return this;//返回的是当前的对象,该对象属于(ThisTest)

    }

    public static void main(String[] args){

       ThisTest tt0=new ThisTest(10);

       ThisTest tt1=new ThisTest("ok");

       ThisTest tt2=new ThisTest(20,"ok again!");

      

       System.out.println(tt0.increment().increment().increment().i);

       //tt0.increment()返回一个在tt0基础上i++的ThisTest对象,

       //接着又返回在上面返回的对象基础上i++的ThisTest对象!

    }

}



运行结果:



Int constructor i——this.i:  10——11

String constructor:  ok

String constructor:  ok again!

Int constructor:  21

String constructor:  ok again!

14


细节问题注释已经写的比较清楚了,这里不在赘述,只是总结一下,其实this主要要三种用法:

1、表示对当前对象的引用!

2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特例,比较常用,所以那出来强调一下。

3、用于在构造方法中引用满足指定参数类型的构造器(其实也就是构造方法)。但是这里必须非常注意:只能引用一个构造方法且必须位于开始!

还有就是注意:this不能用在static方法中!所以甚至有人给static方法的定义就是:没有this的方法!虽然夸张,但是却充分说明this不能在static方法中使用!

评分

参与人数 1技术分 +2 收起 理由
wangfayin + 2 回答的很好!

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-9-18 18:00:56
板凳
你的this.s = s;中,this.s是在没有赋值前是“hello”,而=s的s是你传入的参数, 即new A("HelloWorld!"); 中的helloworld。
this的用法:
一、表示对当前对象的引用,在你的这个例子中就是 A;
二、表示用类的成员变量,而非函数的参数
其实你这个例子这样改一下你就更明白了。
package heima.javase;
/**
*
* @author ila
*
*/
public class Test {
        String s="hello";
        public Test(String args){
                System.out.println("s = " + s);
                System.out.println("1 -> this.s = " + this.s);
                this.s = args;
                System.out.println("2 -> this.s = " + this.s);
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
                new Test("hello world");
        }

}

评分

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

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-9-18 17:48:34
藤椅
this是指当前对象自己;
①第一个打印结果,是构造函数里的s,也就是实例的时候,传进来的Helloworld;
②第二个打印结果,是当前对象中s的值,也就是Hello;
③第三个打印结果,是用传进来的参数对对象A的变量s,即this.s进行操作后的打印结果,this.s = s。
总结,我感觉我说的是对的,嘿嘿:loveliness:

评分

参与人数 1技术分 +1 收起 理由
wangfayin + 1 不错!

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-9-18 17:29:53
沙发

回复 楼主 的帖子

楼主你自己别把自己绕住了  你看看 我的代码也许有相应的改变,能帮助你理解
public class dd {
String s = "Hello";
public dd(String Str) {
System.out.println("s = " + Str);
System.out.println("1 -> this.s = " + this.s);
this.s = Str;
System.out.println("2 -> this.s = " + this.s);
}

public static void main(String[] args) {
new dd("你好");
}
}
this.s这里的s是你定义的变量s,而this.s=[color=Red]s[/color]这个红色的s是参数也就是我的Str,刚开始学,建议不要这样学。对你的面向对象彻底不没好的帮助。这样的教程也不建议看。。。

评分

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

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马