Java instanceof()方法 找两个非常简单的例子
时间: 2023-10-29 13:05:48 浏览: 33
例子1:判断对象是否为某个类的实例
```java
String str = "Hello World";
if (str instanceof String) {
System.out.println("str is an instance of String");
}
```
输出结果为:
```
str is an instance of String
```
例子2:判断对象是否为某个基本数据类型的实例
```java
int num = 10;
if (num instanceof Integer) {
System.out.println("num is an instance of Integer");
}
```
这段代码会编译失败,因为基本数据类型不能使用 instanceof 进行判断。
相关问题
java instanceof()方法 找两个相关例子
例子1:判断对象是否为某个类或其子类的实例
```java
public class Animal {
public void eat() {
System.out.println("Animal eat");
}
}
public class Cat extends Animal {
public void meow() {
System.out.println("Cat meow");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("Dog bark");
}
}
public class InstanceOfExample {
public static void main(String[] args) {
Animal animal1 = new Cat();
Animal animal2 = new Dog();
if (animal1 instanceof Cat) {
((Cat) animal1).meow();
}
if (animal2 instanceof Dog) {
((Dog) animal2).bark();
}
}
}
```
输出结果为:
```
Cat meow
Dog bark
```
例子2:判断对象是否为某个接口的实现类的实例
```java
public interface Shape {
void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle draw");
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle draw");
}
}
public class InstanceOfExample {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Rectangle();
if (shape1 instanceof Circle) {
shape1.draw();
}
if (shape2 instanceof Rectangle) {
shape2.draw();
}
}
}
```
输出结果为:
```
Circle draw
Rectangle draw
```
java如何重写方法当两个量相等时
在Java中,可以通过重写equals()方法来判断两个对象是否相等。equals()方法在Object类中定义,因此可以在任何一个类中重写它。当两个对象的内容相等时,应该返回true,否则返回false。以下是一个示例:
```
public class MyClass {
private int value;
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof MyClass)) return false;
MyClass other = (MyClass) obj;
return this.value == other.value;
}
// other methods and fields
}
```
在这个例子中,我们重写了equals()方法来比较两个MyClass对象的value字段。如果两个对象的value值相等,那么equals()方法返回true。否则,返回false。注意,我们还检查了obj是否为null,以及obj是否为MyClass的实例。这些检查是为了避免抛出NullPointerException和ClassCastException异常。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)