this特性
- this是一个关键字;
- this是一个变量,是一个引用,指的是当前类本身;
- this变量中保存了内存地址指向自身;
- this存储在JVM堆内存java对象内部;
- this关键字不能调用static的方法(实例方法)或者变量(实例变量)
- this只能使用在实例方法或者构造函数中this()进行调用。
public class Hello {
public static void main(String[] args) {
Computer com = new Computer();
System.out.println(com.getBrand());
Computer com2 = new Computer();
com2.setBrand("TSL");
System.out.println(com2.getBrand());
Computer.doSome();
}
}
class Computer
{
private String brand;
Computer(){
this("BMW");
}
Computer(String brand){
this.brand=brand;
}
public void setBrand(String brand_) {
this.brand = brand_ ;
}
public String getBrand() {
return brand;
}
public static void doSome()
{
System.out.println("this不能调用static修饰的方法");
}
}
具体介绍