java instanceof 与 isInstanceof 的用法
2017-07-07 10:16
295 查看
伪代码如下:
Fruit fruit = new Fruit();
Price price2 = null;
Price price1 = new Price(1,"2",new BigDecimal(2));
boolean instance = Price.class.isInstance(fruit);
System.out.println(instance);
if (price2 instanceof Price) {
System.out.println("haha");
}else {
System.out.println("hehe");
}
System.out.println(new Fruit().getClass() == Fruit.class);
打印结果
false
hehe
true
instanceof判断属性是否属于该类
isinstance这个对象能不能被转化为这个类
public class Fruit {
private Integer id;
private String name;
private String kind;
private String date;
private String datetime;
}
public class Price {
private Integer id;
private String name;
private BigDecimal price;
}
Fruit fruit = new Fruit();
Price price2 = null;
Price price1 = new Price(1,"2",new BigDecimal(2));
boolean instance = Price.class.isInstance(fruit);
System.out.println(instance);
if (price2 instanceof Price) {
System.out.println("haha");
}else {
System.out.println("hehe");
}
System.out.println(new Fruit().getClass() == Fruit.class);
打印结果
false
hehe
true
instanceof判断属性是否属于该类
isinstance这个对象能不能被转化为这个类
public class Fruit {
private Integer id;
private String name;
private String kind;
private String date;
private String datetime;
}
public class Price {
private Integer id;
private String name;
private BigDecimal price;
}
相关文章推荐
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleT
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosi
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh
- java 中 instanceof 的意思及用法
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- java反射错误:object is not an instance of declaring class
- java编译错误处理No enclosing instance of type demo1_1 is accessible.
- HIbernate 3.2 java.lang.IllegalArgumentException: object is not an instance of declaring class
- <java>No enclosing instance of type GraphicFrame is accessible. Must qualify the allocation with an
- java.lang.IllegalArgumentException: object is not an instance of declaring class
- java中No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleT
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh
- java编译错误No enclosing instance of type TestFrame is accessible. Must qualify the allocation with an enclosing instance of type TestFrame (e.g. x.new A(
- Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh