Java 中this的用法和静态方法的注意事项

 class Person
{
    private String name;
    private int age;

    public Person()
    {
        System.out.println("开始构造");
    }

    public Person(string name)
    {
        this.name=name;
    }

    public Person(string name,int age)
    {
        this(name);
        this.age=age;
    }
}

除了对自身的成员变量或方法的引用外,还可以用来调用构造方法。

this还通常用在其他对象和本身对象的属性来对比时。

public boolean compare(Person p)

{

  retrun this.age==p.age;

}

静态方法的使用;静态方法里面只能访问静态变量,可以根据静态方法的内存分配来记忆,不能访问非静态的方法和成员。

静态方法中不能使用this和supper关键字。成员变量都省略了一个this。

原文地址:https://www.cnblogs.com/lzhp/p/3120118.html