首页 > Java > java教程 > 正文

Java中的instanceOf

WBOY
发布: 2024-08-30 15:44:53
原创
959 人浏览过

Java中的InstanceOf用于在Java代码片段中实现继承概念时确定对象与其类的关系。一般来说,instanceOf类适用于允许继承的面向对象编程语言,它以布尔结果的形式返回输出值,即true或false。如果 instanceOf 类变量具有 NULL 值,则该类将输出返回为“false”。由于此类用于比较目的,因此也称为“类型比较运算符”。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

instanceOf 类用于检查对象是否属于任何类。

obj instanceOf object
登录后复制

以上是instanceOf类的标准语法。这里, obj 是之前创建的对象的名称(引用)。 instanceOf 是关键字,object 是我们与 obj 对象匹配的类或接口。

在各种情况下,instanceOf可以被证明是有主要用途的,比如我们有一个对象集合,而你不确定它引用的是哪个对象。这种情况的一个例子是带有许多控件的简单表单。

此外,如果我们将 instanceOf 与具有 NULL 值的变量一起使用,它肯定会返回 false。

实例如何工作?

java中的instanceOf运算符适用于简单的is-a关系。简单地说,is-a 关系是一个面向对象的概念,我们在抽象之间比较或者说处理抽象之间的关系,其中类 A 是类 B 的子类。这是一种完全基于继承的关系。换句话说,这就像在说“X是Y类型”。

现在,让我们了解 instanceOf 的工作原理以及相应的代码。

首先,我们将创建一个名为 Parent 的类。

代码:

class Parent{
}
//Then let’s add a simple main class.
public static void main(String args[]) {
}
登录后复制

然后我们将创建 Parent 类的一个实例。

Parent child = new Parent();
登录后复制

最后,我们将使用instanceOf运算符来检查child和Parent之间的关系。如下所示: child instanceOf Parent

现在,如前所述,instanceOf 的语法来自于必须检查的对象,然后是instanceOf 关键字,然后是要测试给定对象的类/接口。

在类声明中遇到关键字“extends”或“implements”的任何时候,这都是正在使用 is-a 关系的明显标志。

Java中instanceOf的例子

以下示例演示了instanceOf的单行使用。

class instanceof_java{
public static void main(String args[]) {
instanceof_java s = new instanceof_java();
System.out.println(s instanceOf instanceof_java);
}
}
登录后复制

代码解读:从创建一个简单的类instanceof_java开始。在类instanceof_java中,我们有我们的主类,并且在我们的主类中,我们创建了一个对象。这个对象s是instanceof_java类型的。然后为了实现instanceOf的工作,我们提供了一个带有对象s的输出语句。在最后一行中,我们将 s 与 instanceof 关键字和父类一起传递。执行后,代码将返回“true”,因为对象 s 是 instanceof 类型。

Java中的instanceOf

更进一步,如果我们有一个已知类或接口的对象,但没有为同一个对象分配任何值,它必然会返回 false,即使我们属于同一类。

class instanceof_sample{
public static void main(String args[]) {
instanceof_sample new = null;
System.out.println(new instanceOf instanceof_sample);
}
}
登录后复制

这里我们有一个与前面的示例类似的代码,但具有空值对象。执行时,此代码将返回 false。正如我们所看到的,对象new是instanceof_sample的实例,但new被分配了一个空值,返回false。

Java中的instanceOf

instanceOf 运算符的规则

基于对象 ref 是否不为 null 以及引用类型的实例。当 X 是所引用对象的简单类,Y 是已解析的类或接口类型的数组时。

  • When X is a simple class, then:
  • If Y is a class type, then the X must be a subclass of Y, or X must the same class as Y.
  • If Y is an interface type, then the X class must implement interface T.
  • When X is type interface, then:
  • If Y is a class type, then the Y must be an Object.
  • Y can be the same as the interface as X or super interface of X if Y is an interface type.
  • If X is a class, which is representing the array type SC[], which is an array of type SC components, then:
  • If Y is a class type, then Y must be an object.
  • If Y is an interface type, then Y must be of interface type implemented by arrays.

Finally, we will demonstrate an instanceOf program to understand that the parent object cannot be an instance of the child class.

Program

class Subject {  }
class Topic extends Subject { }
class instanceof_java
{
public static void main(String[] args)
{
Subject history = new Subject ();
if (history instanceof Topic)
System.out.println("history is instance of Topic");
else
System.out.println("history is NOT instance of Topic");
}
}
登录后复制

Code Interpretation: For the purpose of understanding the instanceOf operator in different scenarios, we wrote the above code. We created a simple class Subject and then another class Topic, which extends class Subject, making the class Topic as child and class Subject as Parent here. Then another class with the main method. Within the main method, we created a new instance of parent class Subject. In the IF ELSE loop, we checked the instance object’s condition with the parent class Subject. If the condition was fulfilled, it would return “history is an instance of Topic” and “history is NOT an instance of Topic” if the condition fails.

Upon executing the above code, the output will be “history is NOT an instance of Topic”, meaning the condition passed in IF fails. It happened because we tried to check the parent of the object “history” with the class Topic. We know that the class Topic extends the class Subject, meaning Topic is a subclass to Subject. And when we tried to check the type of history with class Topic, it returns false (NOT). This means the Parent Object cannot be an instance of a class.

 Output:

Java中的instanceOf

Conclusion

We have learned about instanceOf class in Java, which simply decides if the object is of the given type. We understood how is-a relationship impacts on instanceOf operator. Also known as a comparison operator, instanceOf is based on inheritance.

以上是Java中的instanceOf的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!