Java 对象中的对象中存在不兼容的操作数类型

Java 对象中的对象中存在不兼容的操作数类型,java,class,object,types,casting,Java,Class,Object,Types,Casting,这里的第一个问题。。我有一个简单的程序,有单独的类(学生、教员、书籍、教室),你可以通过名称、e价格、房间容量等进行搜索。这些对象都存储在一个包类中。我还有一个单独的对象类来扩展学生、教员等类。我不确定我在主袋类中的角色设置哪里出了问题。任何帮助都将不胜感激 package Bag; import java.util.ArrayList; public class Main { private static ArrayList<Object> collection =

这里的第一个问题。。我有一个简单的程序,有单独的类(学生、教员、书籍、教室),你可以通过名称、e价格、房间容量等进行搜索。这些对象都存储在一个包类中。我还有一个单独的对象类来扩展学生、教员等类。我不确定我在主袋类中的角色设置哪里出了问题。任何帮助都将不胜感激

package Bag;

import java.util.ArrayList;

public class Main {

    private static ArrayList<Object> collection = new ArrayList();

    public static void main (String[] args) {
        Student student = new Student("Sameet", 1, 4);
        Faculty faculty = new Faculty("Chen", 1000.00, 1);
        Classroom classRoom = new Classroom(1, 10);
        Book book = new Book("Harry Potter", 10);


        collection.add(student);
        collection.add(faculty);
        collection.add(classRoom);
        collection.add(book);
    }

    public void insert(Object obj){

        collection.add(obj);

    }

    public Student searchForStudent(int ID){
        for (Object obj: collection){
            if (obj instanceof Student && (Student)obj.getID() == ID){
                return (Student)obj;
            }
        }
    }

    public Faculty searchForFaculty(String name){
        for (Object obj : collection) {
            if (obj instanceof Faculty && (Faculty)obj.getName() == name) {
                return (Faculty)obj;
            }
        }
    }

    public Classroom searchForClass(int capacity){
        for (Object obj : collection) {
            if (obj instanceof Classroom && (Classroom)obj.getCapacity() == capacity) {
                return (Classroom)obj;
            }
        }
    }
    public Book searchForBook(int price){
        for (Object obj : collection) {
            if (obj instanceof Book && (Book) obj.getPrice() == price) {
                return (Book)obj;
            }
        }
    }
}
包装袋;
导入java.util.ArrayList;
公共班机{
私有静态ArrayList集合=新建ArrayList();
公共静态void main(字符串[]args){
学生=新生(“Sameet”,1,4);
教员=新教员(“Chen”,1000.00,1);
教室=新教室(1,10);
图书=新书(《哈利波特》,第10集);
收藏。添加(学生);
收藏。添加(教员);
收藏。添加(教室);
收藏。添加(书籍);
}
公共空白插入(对象obj){
集合。添加(obj);
}
公共学生搜索学生(int ID){
用于(对象对象对象:集合){
if(学生的obj实例&&(学生)obj.getID()==ID){
返回(学生)obj;
}
}
}
public Faculty searchForFaculty(字符串名称){
用于(对象对象对象:集合){
if(函数组的obj实例&(函数组)obj.getName()==name){
返回(教员)obj;
}
}
}
公共教室searchForClass(国际容量){
用于(对象对象对象:集合){
if(教室的obj实例&(教室)obj.getCapacity()==容量){
返回(教室)obj;
}
}
}
公共图书搜索ForBook(国际价格){
用于(对象对象对象:集合){
if(Book的obj实例&(Book)obj.getPrice()==price){
返回(书本)obj;
}
}
}
}

实际上,这段代码有很多问题

1) 铸件处理不当:

(Faculty) obj.getName() // not working
((Faculty) obj).getName() // this is the proper way of casting
2) 搜索方法中缺少返回语句。您应该抛出异常或返回某些内容。在我的示例中,我返回了null

3) 使用
==
而不是
equals()
进行字符串比较。如果要比较字符串是否相等和不相同,则应始终将字符串与
相等()。
=
比较对象(非基本类型)的标识,如果它们共享相同的内存地址,则返回true

4) 有一些设计问题,最好将每个搜索方法保留在相关类中,而不是将它们全部收集在一个类中。但我真的不知道你的目的和你想要解决的任务,所以让我们集中精力解决其他问题

有一段代码修复了所有编译时问题:

import java.util.ArrayList;

public class Bag {

    private static ArrayList<Object> collection = new ArrayList();

    public static void main(String[] args) {
        Student student = new Student("Sameet", 1, 4);
        Faculty faculty = new Faculty("Chen", 1000.00, 1);
        Classroom classRoom = new Classroom(1, 10);
        Book book = new Book("Harry Potter", 10);


        collection.add(student);
        collection.add(faculty);
        collection.add(classRoom);
        collection.add(book);
    }

    public void insert(Object obj) {
        collection.add(obj);
    }

    public Student searchForStudent(int ID) {
        for (Object obj : collection) {
            if (obj instanceof Student && ((Student) obj).getId() == ID) {
                return (Student) obj;
            }
        }
        return null;
    }

    public Faculty searchForFaculty(String name) {
        for (Object obj : collection) {
            if (obj instanceof Faculty && name.equals(((Faculty) obj).getName())) {
                return (Faculty) obj;
            }
        }
        return null;
    }

    public Classroom searchForClass(int capacity) {
        for (Object obj : collection) {
            if (obj instanceof Classroom && ((Classroom) obj).getCapacity() == capacity) {
                return (Classroom) obj;
            }
        }
        return null;
    }

    public Book searchForBook(int price) {
        for (Object obj : collection) {
            if (obj instanceof Book && ((Book) obj).getPrice() == price) {
                return (Book) obj;
            }
        }
        return null;
    }
}
import java.util.ArrayList;
公厕袋{
私有静态ArrayList集合=新建ArrayList();
公共静态void main(字符串[]args){
学生=新生(“Sameet”,1,4);
教员=新教员(“Chen”,1000.00,1);
教室=新教室(1,10);
图书=新书(《哈利波特》,第10集);
收藏。添加(学生);
收藏。添加(教员);
收藏。添加(教室);
收藏。添加(书籍);
}
公共空白插入(对象obj){
集合。添加(obj);
}
公共学生搜索学生(int ID){
用于(对象对象对象:集合){
if(学生的obj实例&((学生)obj).getId()==ID){
返回(学生)obj;
}
}
返回null;
}
public Faculty searchForFaculty(字符串名称){
用于(对象对象对象:集合){
if(obj instanceof Faculty&&name.equals(((Faculty)obj.getName())){
返回(教员)obj;
}
}
返回null;
}
公共教室searchForClass(国际容量){
用于(对象对象对象:集合){
if(教室的obj实例&((教室)obj).getCapacity()==容量){
返回(教室)obj;
}
}
返回null;
}
公共图书搜索ForBook(国际价格){
用于(对象对象对象:集合){
if(Book的obj实例&((Book)obj).getPrice()==price){
返回(书本)obj;
}
}
返回null;
}
}

运算符优先级,添加括号。但是,在Java中,这不是比较字符串的正确方法。