java关键字保留字

Java中的“ this”关键字是什么? (What is ‘this’ Keyword in Java?)

  • Java this keyword returns a reference to the current Object.Java这个关键字返回对当前Object的引用。
  • We can access object variable, call the method or constructor of the current object using this keyword.我们可以访问对象变量,使用此关键字调用当前对象的方法或构造函数。
  • Java ‘this’ keyword can be used to refer to any member of the current object from within an instance method or a constructor.Java'this'关键字可用于在实例方法或构造函数中引用当前对象的任何成员。
  • this keyword is mostly used to avoid the confusion between a class attribute and a parameter. For example, if a parameter of a member function has the same name as that of an object variable, then the functionality may not work properly.此关键字主要用于避免类属性和参数之间的混淆。 例如,如果成员函数的参数名称与对象变量的名称相同,则该功能可能无法正常工作。

Java这个关键字示例 (Java this Keyword Example)

Let’s say we have a class like below.

假设我们有一个类似下面的类。

public class  Item{ String name; // Constructor with a parameter and this keywordpublic Item(String name) { this.name = name; } // Call the constructorpublic static void main(String[] args) { Item Obj = new Item("car"); System.out.println(Obj.name);}
}

以“ this”作为当前对象的参考输出 (Output with ‘this’ as referance to current object)

Java this Keyword Example

Java这个关键字示例

The code prints the name of the item as “car”. Now let’s see what happens when we remove ‘this’ keyword from the constructor.

代码将项目名称打印为“ car”。 现在让我们看看从构造函数中删除“ this”关键字时会发生什么。

public class  Item{ String name; // Constructor with a parameter and without this keywordpublic Item(String name) { name = name; } // Call the constructorpublic static void main(String[] args) { Item Obj = new Item("car"); System.out.println(Obj.name);}
}

输出不带“ this”作为当前对象的参考 (Output without ‘this’ as referance to current object)

Output without ‘this’ Keyword

没有“ this”关键字的输出

The above example shows the application of this variable for accessing object’s attribute.

上面的示例显示了变量在访问对象属性中的应用。

We can’t create two local variables with the same name.

我们不能创建两个具有相同名称的局部变量。

However, it is allowed to create one instance variable and one local variable or method parameter with the same name.

但是,允许创建一个具有相同名称的实例变量和一个局部变量或方法参数。

In this case, the local variable will hide the instance variable, which is called Variable Shadowing.

在这种情况下,局部变量将隐藏实例变量,称为变量阴影

To solve this problem, we use this keyword with a field to point to the instance variable instead of the local variable.

为解决此问题,我们将此关键字与字段一起使用以指向实例变量而不是局部变量。

“ this”关键字的一般用法 (General uses of ‘this’ keyword)

  1. Java ‘this’ keyword can be used to refer current class instance variable.Java'this'关键字可用于引用当前的类实例变量。
  2. We can use this() to invoke current class constructor. We can also pass parameters in this() statement.我们可以使用this()来调用当前的类构造函数。 我们还可以在this()语句中传递参数。
  3. Java ‘this’ keyword can be used to invoke current class method (implicitly).Java'this'关键字可用于(隐式)调用当前的类方法。
  4. The ‘this’ keyword can be passed as an argument in method call.'this'关键字可以在方法调用中作为参数传递。
  5. We can use ‘this’ keyword to return current class instance.我们可以使用'this'关键字返回当前的类实例。
  6. We can use ‘this’ keyword to access object attributes in case of variable shadowing.在可变阴影的情况下,我们可以使用'this'关键字访问对象属性。

There is also a special type of constructor called as Copy Constructor. Java does not have a default copy constructor but we can create it explicitly using this keyword.

还有一种特殊类型的构造函数,称为Copy Constructor 。 Java没有默认的副本构造函数,但是我们可以使用此关键字显式创建它。

复制构造函数和display()方法“ this”关键字 (Copy-Constructor & display() method ‘this’ keyword)

package com.journaldev.examples;public class Blog {String name;int popularity;// parameterized constructorpublic Blog(String name, int popularity) {this.name = name;this.popularity = popularity;}// Copy-Constructorpublic Blog(Blog b) {this.popularity = b.popularity;this.name = b.name;}public void display() {System.out.println("name: " + this.name);System.out.println("popularity: " + this.popularity + " %");}public static void main(String[] args) {// parameterized constructor callBlog obj1 = new Blog("JournalDev", 100);obj1.display();// Copy-Constructor callBlog obj2 = new Blog(obj1);obj2.display();}}

复制构造函数和display()方法的输出 (Output of Copy-Constructor & display() method)

Copy Constructor Output

复制构造函数输出

In the above code, we saw the application of ‘this’ keyword in a copy constructor and a method display().

在上面的代码中,我们看到了'this'关键字在复制构造函数中的应用以及方法display()

使用此关键字调用构造函数 (Calling a Constructor using this keyword)

Let’s look at an example to call a constructor using this keyword.

让我们看一个使用此关键字调用构造函数的示例。

package com.journaldev.examples;public class Data {Data() {System.out.println("default constructor");}Data(int i) {this();System.out.println("int parameter constructor");}Data(String s) {this(10);System.out.println("string parameter constructor");}public static void main(String[] args) {Data d = new Data(20);System.out.println("------");Data d1 = new Data("Hi");}}

Output:

输出

default constructor
int parameter constructor
------
default constructor
int parameter constructor
string parameter constructor

使用此关键字调用对象方法 (Using this keyword to call the object method)

Let’s look at an example of calling the object method using this keyword.

让我们看一个使用此关键字调用对象方法的示例。

package com.journaldev.examples;public class Data {Data() {this.foo();}private void foo() {System.out.println("foo method");}public static void main(String[] args) {Data d = new Data();}
}

在方法参数中使用此关键字 (Using this keyword in method argument)

package com.journaldev.examples;public class Data {private int id;public Data(int id) {this.id = id;bar(this);}private void bar(Data data) {System.out.println(data.id);}public static void main(String[] args) {Data d = new Data(20);}
}

Java这个关键字返回当前对象 (Java this Keyword to return Current Object)

Let’s look at a simple example where we will use this keyword to return the current object from a method.

让我们看一个简单的示例,在该示例中,我们将使用此关键字从方法中返回当前对象。

private Data bar(Data data) {System.out.println(data.id);return this;
}

结论 (Conclusion)

Java this keyword is helpful in getting the reference of the current object. It’s useful in accessing object attributes in case of variable shadowing. We can also use it to call the current class constructors.

Java这个关键字有助于获取当前对象的引用。 在变量阴影的情况下访问对象属性很有用。 我们还可以使用它来调用当前的类构造函数。

翻译自: https://www.journaldev.com/30873/java-this-keyword

java关键字保留字

java关键字保留字_Java这个关键字相关推荐

  1. java关键字吗_JAVA常见关键字

    JAVA的关键字都是小写的 在JAVA中目前一共有53个关键字:其中由51+2个保留字=53个关键字 1.JAVA的保留关键字(2个) const--常量,常数:用于修改字段或局部变量的声明. got ...

  2. java关键字说明_JAVA常用关键字及其用法简要说明

    Abstract: 抽象的 一个Java语言中的关键字,用在类的声明中来指明一个类是不能被实例化的,但是可以被其它类继承.一个抽象类可以使用抽象方法,抽象方法不需要实现,但是需要在子类中被实现 bre ...

  3. java中的关键字 保留字_java中的标识符、关键字、保留字

    Java中关键字(keyword)和保留字(reservedword) Keyword :Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等. Reserv ...

  4. java关键字 面试_java面试 关键字

    1. final关键字有哪些用法? 修饰类.方法和变量. (1) final变量是只读的,不允许改变其引用,与static共用可声明常量.JVM会对final变量进行优化,比如常量折叠. (2) fi ...

  5. java super()方法_Java super关键字的使用方法详解

    构造方法中的super关键字 在Java子类的构造方法中可以通过super关键字来调用父类的构造方法.其用法为: 1) super(); 访问父类中的无参构造函数 2) super (paras-); ...

  6. java private 命名_java private关键字用法实例

    这篇文章主要介绍了java private关键字用法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 package java04; /* * 问 ...

  7. java的关键字和保留字_Java关键字和保留字及其含义

    1.java的关键字(keyword)有多少个? 51+2个保留字=53个关键字(java的关键字都是小写的!!) 2.java的保留字(reserve word)有多少个?问题:分别是什么? 2个保 ...

  8. java保留关键字意思_Java关键字和保留字及其含义

    正确识别java语言的关键字(keyword)和保留字(reserved word)是十分重要的.Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等.保留字 ...

  9. case是java关键字吗_Java关键字

    3. 程序控制语句 1) break 跳出,中断 break 关键字用于提前退出 for.while 或 do 循环,或者在 switch 语句中用来结束 case 块. break 总是退出最深层的 ...

最新文章

  1. 从城市大脑到世界数字大脑 构建人类协同发展的超级智能平台
  2. 还是自己写的东西比较放心
  3. client-go删除job同时删除job关联的pod
  4. solr搭建分布式搜索引擎
  5. PUTTY、Xshell远程连接Linux与PUTTY、Xshell密匙认证
  6. for循环10000次花多长时间_我的三菱FX PLC学习之子程序调用与循环
  7. 《数据分析思维手册.pdf》,真的强!
  8. 程序员过关斩将--为微服务撸一个简约而不简单的配置中心
  9. pythonstdin_如何写入Python子进程'stdin?
  10. iOS LLDB调试精解
  11. hive hql文档_大数据学习路线分享hive的运行方式
  12. Mysql 8 驱动问题:报错Unknown system variable 'query_cache_size'
  13. 30-算法训练 最短路 spfa
  14. 利用js解析php的表单数据
  15. JavaWeb开发Filter学习
  16. Arcgis拓扑检查之面空隙(内附具体操作步骤)
  17. Go 为什么选择 Gopher 作为吉祥物?
  18. Unity表情聊天(NGUI图文混排)
  19. 解决XP系统桌面图标蓝底
  20. 法外狂徒——格雷福斯

热门文章

  1. url,href,src区别
  2. 1. 少了一个PermMissingElem Find the missing element in a given permutation.
  3. jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片)
  4. 动态SQL实现批量删除指定数据库的全部进程
  5. 80后开网店卖故事:1500多位为感觉而埋单
  6. Cut Curve randomly
  7. java mail 收发邮件
  8. [原创]在ObjectDataSource中使用自定义Web控件提供查询参数
  9. [转载] 【Python】向json文件中追加新的对象
  10. [转载] Java中的strictfp关键字