JAVA中this的用法
阿新 • • 發佈:2018-12-10
package first;
import java.nio.charset.MalformedInputException;
this
1
public class Studen {
int age; // 年齡
String name; // 姓名
String position; // 職位
String education; // 學歷
public Studen(int age, String name, String position, String education) {
super();
this .age = age;
this.name = name;
this.position = position;
this.education = education;
}
public void print() {
name = "小王";
}
public static void main(String[] args) {
Studen problem=new Studen(13,"xiaoming","teacher" ,"博士");
System.out.println(problem.name);
problem.print();
System.out.println(problem.name);
}
}
2
public class Studen {
String name="xiao";
void doHomework(){
System.out.println(this.name+"正在做作業.......");
}
void speak(){
new Studen().doHomework();//學生B,換為this.dohomework()則為學生A
System.out.println(name+"正在說話......");//學生A
}
public static void main(String[] args) {
Studen student = new Studen();
student.speak();//學生A
}
}