this
this关键字:
1.this中文意思: 这 这里
2.this在Java系统中可以作为当前类的类名,还可以作为当前类对象的引用.
this.属性或this.方法名(参值...):
1.在"this.属性或this.方法名(参值...)"表示类对象的引用
2.this用于解决全局变量与局部变量名称冲突时的就近原则问题. 实现在方法体中调用属性.
3.this不能用于静态方法体中,防止使用类名调用静态方法后,this找不到引用的对象.
this([参值,...]):
1.在"this([参值,...])"中this表示当前类的类名. 类名()==构造方法()=this()
2.Java系统为防止构造函数之间直接相互调用形成死循环,因此不允许直接使用"构造方法()"相互调用. 必须借助this关键字解决.
3.this([参值,...])只能编写在当前类中的其他构造方法中,且必须为第1行有效代码.
public class User { String name="小花";//属性: 全局变量 public void getMyInfo(){//参数: 局部变量 String name="小慧";//局部变量 System.out.println("我的信息"); //就近原则: 局部变量[参数] System.out.println("name:"+name); System.out.println("name:"+this.name);//当前类的对象.name } public User() { // User(1); this(1);//类名(1)==User(1) System.out.println("无参构造方法!"); } public User(int i) { System.out.println("有参构造方法!"); } public static void main(String[] args) { //创建对象 User user = new User(); //操作对象 user.getMyInfo(); } }

浙公网安备 33010602011771号