Java中this关键字的常见用法:
1、this指当前对象的成员变量,如下面的构造函数
class Person { String name; int age; Person(String name,int age){ this.name = name; this.age = age; } }
2、调用当前对象的其他构造函数,如下:
class Person { String name; int age; Person(String name){ this.name = name; } Person(String name,int age){ this(name); this.age = age; } }
时间: 2024-11-10 21:41:15