Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use instancesof in Java

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use instancesof in Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use instancesof in Java" article.

Instanceof

1. Instanceof is a keyword in Java, and the keys in Java are all lowercase.

2. The function of the instanceof keyword is to determine whether the left object is the right class (many people here say it is an object, so note that this is the class, not the object). The boolean type, true and false returned by the instance (that is, the subclass object, or the object of the right class itself).

For example:

Public class Dome {class Perse extends Object {} class Student extends Perse {} / this method determines whether it is an instance of Perse, using instanceof to determine. Void f (Object o) {/ / Object can accept any type. If (o instanceof Perse) System.out.println ("the object you entered is an instance of Perse"); else System.out.println ("the object you entered is not an instance of Perse"); public static void main (String [] args) {Dome dome = new Doem (); Perse perse = new Perse (); Student student = new Student () Object object = new Object (); dome.f (/ * shutdent or perse and object*/); / / result: / / input is student and perse, then instanceof judgment will be true,if and execute print statement System.out.println ("the object you entered is an instance of Perse"); / / then input object natural instanceof judgment is false. The reason has been made very clear at the beginning. / / the function of the instanceof keyword is to determine whether the object on the left is an instance of the object on the right (easy to understand: subclass, or object of the right class itself)}}

Use environment

Public class CastingDemo {public static void displayObject (Object o) {if (o instanceof Yuan) {/ / determine whether it is a circle or not. If not, execute the judgment else if System.out.println ("the area of the circle is:" (Yuan) o) .mianJi) / / here (Yuan) Object) prevents the parameter o from being a subclass of Yuan, and this method gets the area. System.out.println ("the diameter of the circle is:" + ((Yuan) o) .zhiJing); / / this method obtains the diameter. } else if (o instanceof ChangFangXing) {System.out.println ("the area of the rectangle is:" + ((ChangFangXing) o));}} public static void main (String [] args) {Object yuan = new Yuan (/ * radius: * / 1) / / Yuan is the object of the circle, and the parameter is the radius, the area and diameter of the circle. Detailed category will not be written, just understand it. Object changFangXing = new ChangFangXing (/ * length and width: * / 1J1); / / ChangFangXing is rectangular, the parameters are length and width, the area / / because it is a static method and is directly used in this class, omitting the class name. DisplayObject (yuan); displayObject (changFangXing);}}

Results: the area of the circle is 3.1415926.

The diameter of the circle is 2.0

The area of the rectangle is: 1.0

The above is about the content of this article on "how to use instancesof in Java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report