Home  >  Article  >  Java  >  Simple example of instanceof keyword in Java

Simple example of instanceof keyword in Java

黄舟
黄舟Original
2017-01-17 15:11:051561browse

happy smile

The wife always suspected that her husband was having an affair, so she looked through his diary when her husband was not at home and found sufficient evidence.

After the husband came home from get off work, the wife cried and asked, "Who is your wife?"

The husband felt baffled and replied, "Besides you, who else can Who is it? Madam! ”

“Humph! What you say is better than what you sing. Why do you call a person named ‘Curie’ Madam?”

Ask a question

What is the function of instanceof keyword in Java? ? ? How to use

to solve the problem

instanceof maintains the concept of type, which refers to: "Are you this class? Or are you a derived class of this class?"

[code]package com.evada.de;

class AAA{
    String a_id;
    String b_name;

}
//BBB是AAA的子类
class BBB extends AAA{
    String b_id;
    String b_name;
}
//CCC是BBB的子类
class CCC extends BBB{
    String c_id;
    String c_name;
}

public class InstanceOfTest{

//main函数测试用
public static void main(String[] args) {

    AAA aaa = new AAA();
    BBB bbb = new BBB();
    CCC ccc = new CCC();
    //由于bbb是AAA的子类(派生类)
    System.out.println(bbb instanceof AAA);//true
    //由于aaa是AAA这个类        
    System.out.println(aaa instanceof AAA);//true
    //由于ccc是AAA的子类(派生类)    
    System.out.println(ccc instanceof AAA);//true
    //aaa既不是BBB这个类,也不是BBB的子类(派生类)
    System.out.println(aaa instanceof BBB);//fasle
    //类似
    System.out.println(ccc instanceof BBB);//true

}
}

Reading Insights

From "One Liter of Tears"

If we can overcome the pain now, there will definitely be colorful happiness waiting for us on the other side.

Don’t be anxious, don’t be greedy, don’t give up

Let’s take it step by step,

No matter how little things are, I want to be a useful person to others

In this case, I will have nowhere to go

People shouldn’t live in the past, just do what they can now

ma OK, wa OK, It has become difficult to pronounce the sounds of "ba" and "n".

I can't make a sound and can only exhale air, so I can't communicate with the other party. I have been talking to myself more recently. Although I didn't like it before, I still have to practice it in order to practice my voice. Do more and I will not give up talking

Today, I had a dream, a dream I have always had. In the dream, I could walk, I could run, I could move freely just like when I first met you. Like that, but today's dream is not like this. I am sitting in a wheelchair. In the dream, I also become a person with limited mobility. Even if I want to admit my physical condition, in my heart, maybe I still don't admit it. That's it.

The above is the content of a simple example of the instanceof keyword in Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn