this的调用
在一类里面两个方法互相调用
package text;
import java.util.*;
public class text {
void a(String x){
System.out.println(x);
//new text().b("我是王伟");
this.b("调用方式"); }
void b(String X){
System.out.println(X);
System.out.println("调用了B");
}
public static void main(String[]args){
text f=new text();
f.a("C");
}
}
在构造器1里面使用this调用构造器2,this必须放在构造器的起始处。
package text;
import java.util.*;
public class text {
text(){
this(1);
System.out.println("我是构造器1");
}
text(int a){
System.out.println("我是构造器2");
}
public static void main(String[]args){
text f=new text(); } }