Created
July 25, 2014 07:12
-
-
Save Tikitoo/5d45ec3d631182567066 to your computer and use it in GitHub Desktop.
This 的用法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tikitoo.java.base; | |
/** | |
* Created by Tikitoo1 on 2014/7/25. | |
*/ | |
public class ThisTest { | |
String name; | |
int age; | |
String sex; | |
public static void main(String[] args) { | |
String strName = new ThisTest( "ttdd").getName(); | |
System.out.println( "strName = " + strName ); | |
} | |
public ThisTest() { | |
System.out.println("默认构造方法被调用"); | |
} | |
public ThisTest(String name) { | |
// 1. 调用其他构造方法 this(参数); | |
this(); | |
// 2. 当局部变量 与 成员变量同名情况下,成员变量无法被访问,使用 this.成员变量 | |
// 当然,局部变量 与 成员变量 不同名的情况下 可以不用 this, 用了也没错 | |
this.name = name; | |
} | |
public String getName() { | |
// 3. 在函数中,无论在什么地方,调用类的当前对象时,直接用this. | |
this.getName(); | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这里更详细,有this,super 的用法