this关键字调用本类构造函数
- this关键字调用类的重载构造函数
- this关键字必须位于构造函数的第一行
public class Person{String name;int age;public Person(int age){this.age=age;}public Person(String name){this(1); //this关键字访问本类构造函数this.name=name;}public static void main(String[] args){Person p1=new Person("出生婴儿1");Person p2=new Person("出生婴儿2");}}
this关键字调用本类方法
表示当前对象自己的方法this.方法名public class Student{public void eat(){System.out.println("同学先吃点事物");}public void talk(){this.eat();System.out.println("同学吃完再说");}}
