this_本类
package projict05; /* * 1.this * 意思:当前对象 * 构造器中,指正要创建的对象 * 普通方法:调用该方法的对象 * 用法: * this.属性 : 成员变量(属性)与局部变量(属性)重名时,成员变量前面加this * this.方法 :当前对象的其他成员方式时,可以使用this,或者省略 * this():访问本类的无参构造器 * this(实参列表):访问本类的有参构造器 * static方法不能使用this; */ public class test19 { public static void main(String[] args) { MyTeacher t = new MyTeacher("wang");//wang is a teacher t.teach(); //qiao is teaching } } class MyTeacher { String name; int id; //构造方法中,this总是指向正要初始化的对象; public MyTeacher(String name) { this.name = name;//this.属性 System.out.println(this.name+" is a teacher"); } public void sleep() { System.out.println("sleep"); } //普通方法中,this总是指向调用该方法的对象; public void teach() { System.out.println(this.name+" is teaching"); this.sleep();//this.方法,可省略 sleep(); } }
posted on 2020-02-23 17:19 happygril3 阅读(138) 评论(0) 收藏 举报
浙公网安备 33010602011771号