对象是类(java)的实例 System.out.print(“输入房间号:”); int a4=scan.nextInt(); scan.nextLine(); 预订[计数]=新房间预订(a1、a2、a3、a4); 计数++; if(/*如果对象是RoomBooking的实例(子类)*/){ 对于(int y=0;y

对象是类(java)的实例 System.out.print(“输入房间号:”); int a4=scan.nextInt(); scan.nextLine(); 预订[计数]=新房间预订(a1、a2、a3、a4); 计数++; if(/*如果对象是RoomBooking的实例(子类)*/){ 对于(int y=0;y,java,object,instance,Java,Object,Instance,“如果对象是RoomBooking的实例(子类)” 我如何用java编写 抱歉,如果这不合理,仍在学习 如果你想知道发生了什么,有两门课。 预订(普通预订)和客房预订(延长预订时间)。。 因为我们必须创建一个数组来存储两者的混合,所以我需要检查对象(a4)是否是RoomBooking的实例,以便比较数字 我试过if((RoomBooking.class.isInstance(a4)){…} 但它不起作用。if(预订房间的对象实例)… 还有一个类中的isAssignableFrom方法 Syst

“如果对象是RoomBooking的实例(子类)” 我如何用java编写

抱歉,如果这不合理,仍在学习

如果你想知道发生了什么,有两门课。 预订(普通预订)和客房预订(延长预订时间)。。 因为我们必须创建一个数组来存储两者的混合,所以我需要检查对象(a4)是否是RoomBooking的实例,以便比较数字


我试过if((RoomBooking.class.isInstance(a4)){…} 但它不起作用。

if(预订房间的对象实例)…


还有一个

类中的
isAssignableFrom
方法

System.out.print("Enter Room Number: ");
int a4 = scan.nextInt();
scan.nextLine();
booking[count]= new RoomBooking (a1,a2,a3,a4);
count++;

if (/* if the object is an instance of RoomBooking(subclass) */) {
    for (int y = 0; y < count; y++) {
        if (a4 == (((RoomBooking) booking[y]).getRoomNumber())) {
            System.out.print("Used number, Please Try again");
        }
    }
}
我更喜欢
@assylias
建议的方法,因为它即使在
对象==null
中也能工作,而
isAssignableFrom
如果参数为null,则会抛出错误。因此,必须检查实例是否不为null

if(CabinBooking.class.isAssignableFrom(object.getClass())

如果(RoomBooking的对象实例)对象是否为a4?(预订房间的a4实例)这看起来有点像是一个转载。。。
if(object instanceof CabinBooking.class)