【问题标题】:Incompatible operand types in object within object对象内对象中的操作数类型不兼容
【发布时间】:2017-11-13 06:31:57
【问题描述】:

这里的第一个问题.. 我有一个简单的程序,其中包含单独的班级(学生、教师、书籍、教室),您可以在其中按姓名、价格、房间容量等进行搜索。这些对象都存储在 Bag 类中。我还有一个单独的对象类来扩展学生、教师等类。我不确定在主要 Bag 类中的铸造哪里出了问题。任何帮助将不胜感激。

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 中比较字符串的正确方法。

标签: java class object types casting


【解决方案1】:

其实这段代码有很多问题。

1) 铸件处理不当:

(Faculty) obj.getName() // not working
((Faculty) obj).getName() // this is the proper way of casting

2) 搜索方法中缺少返回语句。你应该抛出一个异常或返回一些东西。我在我的示例中返回了空值。

3) 字符串比较 == 而不是 equals()。如果要比较字符串是否相等而不是相同,则应始终将字符串与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;
    }
}

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 2018-04-21
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2021-10-10
    • 2019-02-08
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多