Java/Language Basics/Intanceof

Материал из Java эксперт
Перейти к: навигация, поиск

Finding Out of what Class an Object is Instantiated

   <source lang="java">

import java.util.ArrayList; import java.util.Vector; public class Main {

 public static void main(String[] args) {
   Object testObject = new Vector();
   if (testObject instanceof Vector)
     System.out.println("Object was an instance of the class java.util.Vector");
   else if (testObject instanceof ArrayList)
     System.out.println("Object was an instance of the class java.util.ArrayList");
   else
     System.out.println("Object was an instance of the " + testObject.getClass());
 }

} //Object was an instance of the class java.util.Vector

 </source>
   
  
 
  



Get a class of an object

   <source lang="java">


import java.util.ArrayList; import java.util.List; public class Main {

 public static void main(String[] args) {
   Person p = new Person("A");
   Animal a = new Animal("B");
   Thing t = new Thing("C");
   String text = "hello";
   Integer number = 1000;
   List<Object> list = new ArrayList<Object>();
   list.add(p);
   list.add(a);
   list.add(t);
   list.add(text);
   list.add(number);
   for (int i = 0; i < list.size(); i++) {
     Object o = list.get(i);
     if (o instanceof Person) {
       System.out.println("My name is " + ((Person) o).getName());
     } else if (o instanceof Animal) {
       System.out.println("I live in " + ((Animal) o).getHabitat());
     } else if (o instanceof Thing) {
       System.out.println("My color is " + ((Thing) o).getColor());
     } else if (o instanceof String) {
       System.out.println("My text is " + o.toString());
     } else if (o instanceof Integer) {
       System.out.println("My value is " + ((Integer) o));
     }
   }
 }

} class Person {

 private String name;
 public Person(String name) {
   this.name = name;
 }
 public String getName() {
   return name;
 }

} class Animal {

 private String habitat;
 public Animal(String habitat) {
   this.habitat = habitat;
 }
 public String getHabitat() {
   return habitat;
 }

} class Thing {

 private String color;
 public Thing(String color) {
   this.color = color;
 }
 public String getColor() {
   return color;
 }

}

 </source>
   
  
 
  



The difference between instanceof and class

   <source lang="java">

// : c10:FamilyVsExactType.java // The difference between instanceof and class // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. class Base { } class Derived extends Base { } public class FamilyVsExactType {

 static void test(Object x) {
   System.out.println("Testing x of type " + x.getClass());
   System.out.println("x instanceof Base " + (x instanceof Base));
   System.out.println("x instanceof Derived " + (x instanceof Derived));
   System.out.println("Base.isInstance(x) " + Base.class.isInstance(x));
   System.out.println("Derived.isInstance(x) "
       + Derived.class.isInstance(x));
   System.out.println("x.getClass() == Base.class "
       + (x.getClass() == Base.class));
   System.out.println("x.getClass() == Derived.class "
       + (x.getClass() == Derived.class));
   System.out.println("x.getClass().equals(Base.class)) "
       + (x.getClass().equals(Base.class)));
   System.out.println("x.getClass().equals(Derived.class)) "
       + (x.getClass().equals(Derived.class)));
 }
 public static void main(String[] args) {
   test(new Base());
   test(new Derived());
 }

} ///:~


 </source>