当前位置: 首页 > 编程笔记 >

此关键字在Java中有什么用?

凌善
2023-03-14
本文向大家介绍此关键字在Java中有什么用?,包括了此关键字在Java中有什么用?的使用技巧和注意事项,需要的朋友参考一下

Java中的“ this”关键字是对当前类对象的引用。使用它,您可以引用类的字段,方法或构造函数。

使用“ this”关键字引用字段

如前 ,您可以使用“ this” 关键字从实例方法或构造函数中引用类的实例字段/变量。 

即如果一个方法的名称相同的实例变量一个局部变量的话,你可以使用从局部变量区分实例变量这个 它。

示例

在下面的Java示例中,Student类具有两个私有字段name和age,分别具有setter和getter方法。setter方法具有与实例变量同名的局部变量,我们使用“ this”关键字来区分它们。 

public class Student {
   private String name;
   private int age;
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void display() {
      System.out.println("name: "+getName());
      System.out.println("age: "+getAge());
   }
   public static void main(String args[]) {
      //从用户读取值
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      System.out.println("Enter the age of the student: ");
      int age = sc.nextInt();

      //调用setter和getter方法
      ThisExample obj = new ThisExample();
      obj.setName(name);
      obj.setAge(age);
      obj.display();
   }
}

输出结果

Enter the name of the student:
Krishna
Enter the age of the student:
22
name: Krishna
age: 22

使用“ this”关键字引用构造函数

您也可以使用“ this”关键字(明确地)从另一个构造函数引用/调用类的构造函数。

示例

在下面的示例中,Student类具有两个私有变量name和age。在本文中,我们正在编写四个构造:一个接受两个变量,一个仅接受名称,一个仅接受年龄,一个不接受年龄。

在每个构造函数中,我们都使用此 关键字调用参数化的构造函数(接受两个变量)。 

public class StudentData {
   private String name;
   private int age;
   public StudentData(String name, int age){
      this.name = name;
      this.age = age;
   }
   public StudentData(){
      this(null, 0);
   }
   public StudentData(String name) {
      this(name, 0);
   }
   public StudentData(int age) {
      this(null, age);
   }
   public void display(){
      System.out.println("Name of the Student: "+this.name );
      System.out.println("Age of the Student: "+this.age );
   }
   public static void main(String args[]) {
      //从用户读取值
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      System.out.println("Enter the age of the student: ");
      int age = sc.nextInt();
      System.out.println(" ");
      //调用接受两个值的构造函数
      System.out.println("Display method of constructor that accepts both values: ");
      new StudentData(name, age).display();
      System.out.println(" ");
      //调用接受名称的构造函数
      System.out.println("仅接受名称的构造方法的显示方法:");
      new StudentData(name).display();
      System.out.println(" ");
      //调用接受年龄的构造函数
      System.out.println("Display method of constructor that accepts only age: ");
      new StudentData(age).display();
      System.out.println(" ");
      //调用默认构造函数
      System.out.println("Display method of default constructor: ");
      new StudentData().display();
   }
}

输出结果

Enter the name of the student:
Krishna
Enter the age of the student:
22
Display method of constructor that accepts both values:
Name of the Student: Krishna
Age of the Student: 22
仅接受名称的构造方法的显示方法:
Name of the Student: Krishna
Age of the Student: 0
Display method of constructor that accepts only age:
Name of the Student: null
Age of the Student: 22
Display method of default constructor:
Name of the Student: null
Age of the Student: 0

引用使用“ this”关键字的方法

您还可以使用从另一个实例方法调用(实例)方法。

示例

在下面的示例中,Student类具有一个私有变量名称,以及setter和getter方法,使用setter方法,我们已经为name变量分配了值。并且我们从名为的实例方法中使用“ this”关键字调用了getter方法display()

public class ThisExample_Method {
   private String name;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void display() {
      System.out.println("name: "+this.getName());
   }
   public static void main(String args[]) {
      ThisExample_Method obj = new ThisExample_Method();
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      obj.setName(name);
      obj.display();
   }
}

输出结果

Enter the name of the student:
Krishna
name: Krishna
 类似资料:
  • 问题内容: 今天的工作中,我遇到了volatileJava中的关键字。不太熟悉,我找到了这种解释。 鉴于该文章详细解释了所讨论的关键字,您是否曾经使用过它,或者是否曾见过可以正确使用此关键字的情况? 问题答案: volatile具有内存可见性的语义。基本上,volatile字段的值对所有读取器(尤其是其他线程)可见,在该字段上完成写操作之后。没有volatile,读者可能会看到一些未更新的值。 要

  • 问题内容: 我正在学习如何将sqlite3与python结合使用。我要遵循的教科书中的示例是一个数据库,其中每个“国家/地区”记录都有一个“地区”,“国家/地区”和“人口”。 这本书说: 以下代码段使用CONSTRAINT关键字来指定要创建的表中没有两个条目的地区和国家/地区具有相同的值: 请您在这里解释一下。如果我删除它,似乎仅PRIMARY KEY语句就可以确保每个国家/地区在该地区都有唯一的

  • 问题内容: 为什么在Java中不使用const关键字? 您能看到在Java语法中使用某些可传递const或不可变关键字的任何缺点,还是为什么选择了常见的繁琐方法? 您能看到关闭请求的 原因 ,Sun提供了任何解释吗? 问题答案: 您能看到关闭请求的原因吗,Sun是否提供任何解释? 是。Sun在请求本身中提供了三个为什么不对请求执行操作的原因。我引用: “目前尚无计划将此功能添加到Java中。除了蔓

  • 问题内容: 我正在网上关注Java教程,尝试学习该语言,并且它在使用数组的两种语义之间反弹。 和: 该教程从未真正提到过为什么它会在两者之间来回切换,所以我对该主题进行了一些搜索。我目前的理解是,操作员正在创建“ longs数组”类型的对象。我 不 明白的是为什么我要那个,那有什么后果? 是否存在某些特定于“数组”的方法,除非它是“数组对象”,否则这些方法对数组不起作用? 有什么我 不能 用普通数

  • 问题内容: 怎么办?例如在函数中: 问题答案: 如果使用(或-ea简称)启动程序,则此语句 相当于 如果不使用此选项启动程序,则assert语句将无效。 例如,;在你的问题中发布的,等同于 (如果你启动的是。) Java语言规范assert正式为:14.10。该声明说: 14.10。的声明 的断言是一个含有一个布尔表达式语句。断言是启用还是禁用。如果启用了断言,则断言的执行将导致对布尔表达式进行求

  • 问题内容: 似乎几乎没有人意识到这一点,但是Java中的“ thisreference”是最终的。在正常的编程天中,我认为可以通过在自己的类中重新定义此引用来重新定义整个实例: 为什么此引用在Java中是最终的? 问题答案: 问题不在于它是参考,而是它本身根本不是参考。是一个 关键字 ,“表示一个值,该值是对为其调用实例方法或默认方法的对象的引用”(JLS§15.8.3)。 此外,从可以重新分配变