java中的instanceof和isInstance

Tue, Jun 16, 2020 2-minute read

我对 instanceof 和 isInstance 太困惑了,现在来理一下。

interface A {
}

class B{

}

class C implements A {

}

class D implements B {

}

public class Solution {

    public static void main(String[] args) {
        C s1 = new C();
        D s2 = new S();

        //孩子是不是父辈,祖辈的啊?要站在一起才能看到
        System.out.println(s1 instanceof C);
        System.out.println(s1 instanceof A);

        System.out.println(C.class.inStanceof(s1));
        System.out.println(A.class.isInstanceof(s1));


    }
}
class Employee {
}

class Developer extends Employee {
}

class DataAnalyst extends Employee {
}

for (Employee employee : employees) {
            System.out.println(Developer.class.isInstance(employee) ? "DEV" : DataAnalyst.class.isInstance(employee) ? "DA" : "EMP");
        }

   for (Employee employee : employees) {
            if (employee.getClass() == Developer.class) {
                System.out.println("DEV");
            } else if (employee.getClass() == DataAnalyst.class) {
                System.out.println("DA");
            } else {
                System.out.println("EMP");
            }
        }

       for (Employee employee : employees) {
            switch (employee.getClass().getSimpleName()) {
                case "Employee":
                    System.out.println("EMP");
                    break;
                case "Developer":
                    System.out.println("DEV");
                    break;
                case "DataAnalyst":
                    System.out.println("DA");
                    break;
                default:
                    System.out.println("I can't do that, Dave");
            }
        }

You are given 4 classes — Shape, Polygon, Square, Circle.

Classes Polygon and Circle both extend the class Shape, the class Square extends the class Polygon.

You need to implement a method that takes Shape array and adds every element to one of the provided Lists based on their class.

public static void sortShapes(Shape[] array,
                              List<Shape> shapes,
                              List<Polygon> polygons,
                              List<Square> squares,
                              List<Circle> circles) {
  // shape => Polygon => Square
  // shape => Circle
        for (Shape shape : array) {
          String name = shape.getClass().getSimpleName();
          switch(name) {
            case "Polygon": polygons.add((Polygon)shape); break;
            case "Square": squares.add((Square)shape); break;
            case "Circle": circles.add((Circle)shape); break;
            default: shapes.add(shape); break;
        }
        }
}
public static void sortShapes(Shape[] array,
                                  List<Shape> shapes,
                                  List<Polygon> polygons,
                                  List<Square> squares,
                                  List<Circle> circles) {

        for (Shape shape:array) {
            if (shape.getClass() == Shape.class) {
                shapes.add(shape);
            } else if (shape.getClass() == Polygon.class) {
                polygons.add((Polygon) shape);
            } else if (shape.getClass() == Square.class) {
                squares.add((Square) shape);
            } else if (shape.getClass() == Circle.class) {
                circles.add((Circle) shape);
            }
        }

    }
class Shape { }
class Polygon extends Shape { }
class Square extends Polygon { }
class Circle extends Shape { }