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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 焦明坤 中级黑马   /  2014-12-12 21:14  /  1999 人查看  /  20 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这个问题困扰了好久,还是不明白,请教一下各位大神,他到底什么时候用?最好是有固定格式

评分

参与人数 1黑马币 +10 收起 理由
不淡定,小学生 + 10 淡定

查看全部评分

20 个回复

倒序浏览
这个视频上不是有讲啊?




回复 使用道具 举报
看了好几遍,有点地方老是看不明白。。。。。
回复 使用道具 举报
this在类中哪个对象需要调用方法this就是哪个对象,一般打代码时都经常用,只不过是隐式的罢了。
回复 使用道具 举报
这个说不太清楚 ,你可以在网上查查,然后结合课本和视频一点点的理解一下
回复 使用道具 举报
推荐毕向东视频
回复 使用道具 举报
this相当于一个指针,他所指向的是本类对象。
回复 使用道具 举报
看代码。
  1. /**
  2. * @author 史云龙
  3. * @Descripe
  4. *三种用法:
  5. *1、表示对当前对象的引用!
  6. *2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!
  7. *                        其实这是第一种用法的特例,比较常用,所以那出来强调一下。
  8. *3、用于在构造方法中引用满足指定参数类型的构造器(其实也就是构造方法)。
  9. *                        但是这里必须非常注意:只能引用一个构造方法且必须位于开始!
  10. */
  11. public class Test {
  12.         private int i=0;
  13.         Test(int i){
  14.                 this.i=i;//第二种用法
  15.                 System.out.println("Int构造函数:"+i);
  16.         }
  17.         Test(String s){
  18.                 System.out.println("String构造函数:");
  19.         }
  20.         Test(int i,String s){
  21.                 this(s);//调用第二个构造函数。第三种用法
  22.                 this.i=i++;
  23.                 System.out.println("Int和String构造函数:"+i+"<--i+s-->"+s);
  24.         }
  25.         public Test getTest(){
  26.                 return this;//第一种用法
  27.         }
  28.         public int getI(){
  29.                 return i;
  30.         }
  31.         public static void main(String args[]){
  32.                 Test t = new Test(0);
  33.                 System.out.println("getTest:"+t.getTest().getI());
  34.                 System.out.append("getI:"+t.getI());
  35.                 Test t1 = new Test(10,"第二个");
  36.                 System.out.println("getTest:"+t1.getTest().getI());
  37.                 System.out.append("getI:"+t1.getI());
  38.                
  39.         }
  40. }
复制代码
回复 使用道具 举报
I空空 中级黑马 2014-12-12 22:54:41
9#
this表示当前对象,经常用于方法间的调用,尤其是构造方法初始化时,其实这个this也困扰了我很久,多写写代码,慢慢的就能悟道了
回复 使用道具 举报
this表示当前对象,经常用于方法间的调用,这个问题也困扰了我很久,其实多看看毕老师的那节视频,多写写,自然而然就明白了
回复 使用道具 举报

总结一下:1.返回对象的引用;2.引用成员变量;3.调用构造方法。
回复 使用道具 举报
this代表你所在的对象名
回复 使用道具 举报
感谢哥们啊
回复 使用道具 举报
this应用在构造函数间调用; 用于区分同名情况下的应用。
回复 使用道具 举报
this 代表的就是所在类 的对象的引用,当成员变量命 与所传替的参数名字相同时就不可以省略,因为要起到区分的作用。
回复 使用道具 举报
kenhe 中级黑马 2014-12-14 09:45:06
16#
this是本类对象的引用
回复 使用道具 举报
是当前对象的引用
回复 使用道具 举报
这个多看看代码,差不多就懂了,不好说。
回复 使用道具 举报
1.当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量。(this是当前对象自己)
class ThisDemo
{
public static void main(String[] args)
{
  //A a = new A(3);
  B b = new B(3);
}
}
class A
{
int i=2;
A(int i)
{
  System.out.println("i="+i);//3     这里的i表示的是传进去的参数。
  System.out.println("this.i="+this.i);//2      这里的this。i表示的是该方法所在类的成员变量
  this.i=i;
  System.out.println("i="+i);//3
  System.out.println("this.i="+this.i);//3

}
}
class B extends A
{
int j=3;
B(int j)
{
  super(j);//这里如果不指定super(j),第一行默认是super()但是父类没有这个,会报错,所以要手动指定相应参数构造函数。
  this.j=j;
  System.out.println("j="+j);//3
  System.out.println("this.j="+this.j);//3
}
}
------------------------------------------------------------下面的和上面的是一个意思,
class B extends A
{
B(int i)
{
  super(i);//除了指定子类的有一参数的构造函数,还有一个作用,就是调用父类赋值方法,this.i=i;
  System.out.println("i="+i);//3
  System.out.println("this.i="+this.i);//3
}
}

结果是:如图注释

==================================================================================================================================
2.把自己当作参数传递时,也可以用this.(this作当前参数进行传递)

class A {
public A() {
  调用B的方法
}
public void print() {
System.out.println("A!");
  }
}
class B {
    A a;
    public B(A b) {
       this.a = b;
    }
    public void print() {
       a.print();//调用A的方法
       System.out.println("B!");
    }
}
class HelloA {
    public static void main(String[] args) {
       A aaa = new A();
       aaa.print();
       B bbb = new B(aaa);
       bbb.print();
    }
}
/*
HelloAA from A!
HelloAB from B!
HelloAA from A!
HelloAA from A!
HelloAB from B!
*/
===============================================================================================================================
3.关于匿名内部类中使用this 访问外部类
interface Inter
{
public static final int x=4;//内部类成员函数
void method();
}
class Test
{
int x=3;//外部类成员函数
    Inter function()
{
   int x=0;//外部类局部函数
  return new Inter()
   
  {
   public void method()
  {
   System.out.println("Inner method"+Test.this.x);//访问外部类成员函数
   System.out.println("Inner method"+this.x);//访问内部类成员函数
   }
  };
}
}

class InnerClassTest
{
public static void main(String[] args)
{
  new Test().function().method();
}
  
}
/*
Inner method3
Inner method4
*/
===================================================================================================================================
4.this用来指定调用构造函数,
public class ThisTest {
    ThisTest(String str) {
       System.out.println(str);
    }
    ThisTest() {
       this("this测试成功!");//调用有一个参数都构造函数
    }

    public static void main(String[] args) {
       ThisTest thistest = new ThisTest();
    }
}
/*
this测试成功!
*/
-----------------------------------------------------------------------------
class ThisTest2
{
public static void main(String[] args)
{
  A a1 = new A("zs",20);
  A a2 = new A("ls",20);
  A a3 = new A("ww");
  }
}
class A
{
private String name;
private int age;
A()
{
  String name="null";
  int age=0;
      System.out.println("还没出生"+"姓名:"+name+"     "+"年龄:"+age);

}
A(String name)
{
  this();//调用无参构造函数
  this.name=name;
  System.out.println("刚出生"+"姓名:"+name+"     "+"年龄:"+age);
}
A(String name,int age)
{
  this(name);//调用有一个参数构造函数
  //this.name=name;
  this.age=age;
  System.out.println("出生很久了"+"姓名:"+name+"     "+"年龄:"+age);
}
}
/*
还没出生姓名:null     年龄:0
刚出生姓名:zs     年龄:0
出生很久了姓名:zs     年龄:20
还没出生姓名:null     年龄:0
刚出生姓名:ls     年龄:0
出生很久了姓名:ls     年龄:20
还没出生姓名:null     年龄:0
刚出生姓名:ww     年龄:0
*/
=================================================================================================================================
5.this传递当前对象
public class ThisTest3 {
    int x;
    int y;

     void showtest(ThisTest3 tt) {//实例化对象
       System.out.println(tt.x + " " + tt.y);
    }
    void seeit() {
       showtest(this);
    }

    public static void main(String[] args) {
       ThisTest3 p = new ThisTest3();
       p.x = 9;
       p.y = 10;
       p.seeit();
    }
}
/*
9  10
*/
总结:当成员变量和局部变量同名是,用this指定访问的变量; 把自己当做参数传递,此时this代表类类型变量; this在匿名内部类中指定访问的外部类;
      this用来指定要调用的构造函数; this传递当前对象实体;

回复 使用道具 举报
class Person{
        private String name;                                         //人这个类具有的属性
        private int age;
        Person(int age){
                this.age=age;                    //初始化具有年龄
}
        Person(String name) {
                this.name = name;
}                                                //为了使函数更具有阅读性,把传递参数取名为name,这就与成员变量重名,加this关键字
        public void speak(){
                System.out.println("name="+name+"..age="+age);   //其实这里name前省略了this关键字
}

        public boolean compare(Person p){        //比较是不是同龄人的功能,与另一个人比较。
                return this.age==p.age;
}
       
}
class PersonDemo3{
        public static void main(String[] args){
                Person p = new Person("lisi");
                p.speak();
                Person p1 = new Person("zhang san");      //创一个新person类名字叫zhangsan,堆内存中名字p1
                p.speak();
                boolean g= p.compare(p1);             //用到类中的比较方法,将结果存于变量g后输出
                System.out.println(g);
}
}
/*
上方this区分了成员变量的name变量和传递参数name,是代表本类函数的引用。

this是代表了对象中的name变量。例如p一创建,在盏内存中有一个name成员变量,this就代表了这个name。。

总之,类中的数据都是为对象服务,而this关键字就代表了本类对象。
       
this的应用,假如人具有一个功能就是比较是否是同龄人,上方compare方法就是。
compare方法中 传递参数p与建立对象的名字相同,此时就可以在age前方加入this表示用到这个方法的对象。

构造函数之间调用要用this

Person(){}

Person(String name){
        this(Person);
        this name = name;
}

构造函数间调用要用this关键字,this关键在必须在构造函数第一句,既是初始化最先执行。
*/
这是我自己的一个小笔记,可以看一下。
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马