关于Java语言当中的this关键字
public class CustomerTest{ public static void main(String[] args){ //创建Customer对象 Curtomer c1 = new Customer(); c1.name = "zhangsan"; //c1购物 c1.shopping(); //再创建Customer对象 Customer c2 = new Customer(); c2.name = "lisi"; //c2购物 c2.shopping(); } }
public void shopping(){ //当张三在购物时,输出张三在购物 //当李四在购物时,输出李四在购物 //System.out.println("XXX在购物"); System.out.println(this.name + "在购物"); }
public static void doOther() { Customer c = new Customer(); System.out.println(c.name); //这里访问的name是c的对象中的内容 }
public class test{ private int year; private int month; private int day; //比如程序员需要设置默认日期1979-1-1 public Date(int year, int month, int day){ this.day = day; this.month = month; this.year = year; } public Data(){ /* this.day = 1; this.month = 1; this.year = 1979; */ this(1979, 1, 1); } }
class day3 { public static void doSome(){ System.out.println("do some!"); } public void doOther(){ System.out.println("do other!"); } public static void method1(){ day3 tt = new day3(); day3.doSome(); doSome();//省略 tt.doOther(); } public void method2(){ day3.doSome(); doSome();//省略 doOther(); } public static void main(String[] args) { day3 jj = new day3(); day3.method1(); method1();//省略 jj.method2(); } }