this关键字的作用
this关键字表示的是“当前对象”,当出现在类的实例方法中,代表的是调用该方法的对象;当出现在类的构造方法中时,代表的是调用该构造方法创建的对象。在下列两种情况下,必须使用this:
方法的形式参数与类的成员变量同名:
例如:
class Person{
String name;
int age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
}
一个类中的构造器调用同一个类中的另一个构造器:
调用的方法:
this([参数列表]) //处于构造器的第一行位置上
例如:
class Person{
String name;
int age;
public Person(String name){
this.name=name;
}
public Person(String name, int age){
this(name); //调用另一构造器,必须放在第一条语句位置上
this.age=age;
}
}
方法的形式参数与类的成员变量同名:
例如:
class Person{
String name;
int age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
}
一个类中的构造器调用同一个类中的另一个构造器:
调用的方法:
this([参数列表]) //处于构造器的第一行位置上
例如:
class Person{
String name;
int age;
public Person(String name){
this.name=name;
}
public Person(String name, int age){
this(name); //调用另一构造器,必须放在第一条语句位置上
this.age=age;
}
}
【this关键字的作用】相关文章
1. this关键字的作用
2. this关键字的含义
4. JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
5. super关键字的用法
6. static关键字的用法
8. What is the purpose of Void class? Void类的作用是什么?
10. swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上
本文来源:https://www.mianshiwenti.com/a12492.html
进入下载页面
上一篇:如何清空Session
下一篇:JVM是一个编译程序还是解释程序