其他分享
首页 > 其他分享> > instanceof运算符

instanceof运算符

作者:互联网

首先讲一下为什么使用instanceof运算符。
总是可以将一个子类的实例转换为一个父类的变量(向上转换),因为子类永远是父类的实例。而当把一个父类的实例转化为它的子类变量(向下转换)就必须使用转化记号"(子类名)"进行显示转化,向编译器说明转化的类型。为了是转化成功,就必须确保转化的对象是一个子类的实例。如果父类对象不是子类的一个实例,就会报一个的异常错误。所以在尝试转化之前确保该对象是另一个对象的实例就用instanceof来实现。
举个例子:
m(new Student)等价于
Object o = new Student();
m(o);
由于Student的实例也是Object的实例,所以转化是合法的,也被称为隐式转化。
假设使用下面的语句把对象引用o赋值给Student类型的变量,
Student b =o;
将会发生编译错误
比如:

package instanceoper;

public class Instance {
	public static void main(String[] args) {
		m(new GraduateStudent());
		m(new Student());
		m(new Person());
		m(new Object());
		Object o = new Student();
		Student b = o;
	}
	public static  void m(Object x) {
		System.out.println(x.toString());
	}

}
class GraduateStudent extends Student{
	
}
class Student extends Person{
	public String toString() {
		return "Student";
	}
}

class Person extends Object{
	public String tostring() {
		return "Person";
	}
}

运行结果:
在这里插入图片描述
为什么会错误呢?
Student 对象总是Object的实例,但是Object对象不一定是Student 的实例,Java虚拟机里的编译器识别不了就报错了。

写个简单例子:
SimpleGeometricObject类:

package instanceoper;

import java.util.Date;

public class SimpleGeometricObject {
	private String color="white";
	private boolean filled;
	private Date dataCreated;
	public SimpleGeometricObject() {
		dataCreated  =new Date();
	}
	public SimpleGeometricObject(String color,boolean filled) {
		dataCreated  =new Date();
		this.color  =color;
		this.filled = filled;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public boolean isFilled() {
		return filled;
	}
	public void setFilled(boolean filled) {
		this.filled = filled;
	}
	public Date getDataCreated() {
		return dataCreated;
	}
	public void setDataCreated(Date dataCreated) {
		this.dataCreated = dataCreated;
	}
	public String tostring() {
		return "create on" + dataCreated + "\ncolor: " +  color + "and color:" + filled;
	}

}

CircleFromSimpleGeometricObject类:

package instanceoper;

public class CircleFromSimpleGeometricObject extends SimpleGeometricObject{
	private double radius;
	public final double PI= 3.14;
	public CircleFromSimpleGeometricObject() {}
	public CircleFromSimpleGeometricObject(double radius) {
		this.radius  =radius;
	}
	public CircleFromSimpleGeometricObject(double radius ,String color,boolean filled) {
		this.radius = radius;
		setColor(color);
		setFilled(filled);
		
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public double getArea() {
		return radius *radius*PI;
	}
	public double getDiameter() {
		return 2*radius;
	}
	public void printCircle() {
		System.out.println("The circle is created " + getDataCreated() +"and the radius is" + radius);
	}
}

RectangleFromSimpleGeometricObject类:

package instanceoper;

public class RectangleFromSimpleGeometricObject extends SimpleGeometricObject{
	private double width;
	private double height;
	public RectangleFromSimpleGeometricObject() {}
	public RectangleFromSimpleGeometricObject(double width,double height) {
		this.width =width;
		this.height = height;
	}
	public RectangleFromSimpleGeometricObject(double width,double height,String color,boolean filled) {
		this.width =width;
		this.height = height;
		setColor(color);
		setFilled(filled);
	}
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public double getArea() {
		return width *height;
	}
	public double getPerimeter() {
		return 2*(width +height);
	}

}

测试类:

package instanceoper;
import instanceoper.CircleFromSimpleGeometricObject;
import instanceoper.RectangleFromSimpleGeometricObject;
public class CastDemo {
		public static void main(String[] args) {
			Object object1 = new  CircleFromSimpleGeometricObject(1);
			Object object2 = new RectangleFromSimpleGeometricObject(1,1);
		
			displayObject(object1);
			displayObject(object2);
		}
		public static void displayObject(Object object) {
			if(object instanceof CircleFromSimpleGeometricObject) {
				System.out.println("The circle area is " +
						((CircleFromSimpleGeometricObject)object).getArea());
				System.out.println("The circle diameter is " +
						((CircleFromSimpleGeometricObject)object).getDiameter());
			}else if(object instanceof RectangleFromSimpleGeometricObject) {
				System.out.println("The rectangle area is " +
						((RectangleFromSimpleGeometricObject)object).getArea());
			}
	}

}

运行结果:
在这里插入图片描述
Over!

标签:instanceof,return,color,double,运算符,radius,public,filled
来源: https://blog.csdn.net/qq_42321336/article/details/97272407