当然没有任何问题
this一般有强调背景的作用,如在一个类中
class Demo extends Applet {
private int t;
public void foo(int t){
this.t = t; // 这里this强调了左边的t是类Demo的成员t,右边的t是函数参数,通过函数调用传来
}
}
this一般有强调背景的作用,如在一个类中
class Demo extends Applet {
private int t;
public void foo(int t){
this.t = t; // 这里this强调了左边的t是类Demo的成员t,右边的t是函数参数,通过函数调用传来
}
}
int a;
int b;
public AA(int a,int b) {
this.a = a;
this.b = b;
}
}
int a;
int b;
public AA(int a,int b) {
this.a = a;
this.b = b;
}
}只用来在不记得它的成员函数时用this,怎么可能不记得他的成员函数
有问题,因为this是对当前对象的引用, 而你只要在当前对象的范围内,那么是可以不加this的 它默认为当前对象的成员. 我只用来在不记得它的成员函数时用this, 或者: public class AA {
int a;
int b;
public AA(int a,int b) {
this.a = a;
this.b = b;
}
}只用来在不记得它的成员函数时用this,怎么可能不记得他的成员函数
this.method()基本上可以全部去掉
但是
class A{
void method(A a){
....
}
class B{
void innerMethod(){
method(A.this); //这个OuterClassName.this是不可以省的,它是转型
}
}
}
如果离开了当前的类范围当然只有声明你所要引用的对象啦:
AA.java
public class AA {
private int a;
private int b;
public AA(int a,int b) {
this.a = a;
this.b = b;
} BB.java
public class BB {
public BB() {
AA aa(3,5);//如果此处用this,引用的肯定是BB啦!!
}
public class AA {
private int a;
private int b;
public AA(int a,int b) {
this.a = a;
this.b = b;
} BB.java
public class BB {
public BB() {
AA aa(3,5);//如果此处用this,引用的肯定是BB啦!!
}public class BB {
public BB() {
AA aa(3,5);//如果此处用this,引用的肯定是BB啦!!
}
是什么用法
1、在必须明确指出当前的object reference究竟为何时,才需要动用关键字this:
例如:当向要返回目前的对象时,需要在return中这么写:
public class Leaf {
int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
} ///:~2、在构造函数中调用构造函数,如果某个class中有多个构造函数,有时候在需要在某个构造函数中调用另一个构造函数,以免重复代码,也可以使用this。
例如:public class Flower {
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
} ///:~3、如果方法的参数s和数据成员s的名称相同,会出现模棱两可的情况,也需要用到this,这种情况就是以上各位说的。 比如: wlz47(秋水沉舟)。
要是,class you extnds yourfather{
this()//<------你的动作
super()//<--------你老爸的动作
}
this的这种用法那位高人能句个例子,在下孤陋寡闻,没见国