Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以在继承自super的子类中设置布尔值吗?_Java - Fatal编程技术网

Java 我可以在继承自super的子类中设置布尔值吗?

Java 我可以在继承自super的子类中设置布尔值吗?,java,Java,我有一个Gift的父类和一堆扩展它的子类。我希望保修布尔值对某些人是正确的,对其他人是错误的。 我已更新代码以反映…更新 现在看来Gift正在被实现,但我得到一个错误,说明它不是 public class CustomerGifts { public static void giveGift() { } public static void main(String[] args) { int[] crowd = new int[100];

我有一个Gift的父类和一堆扩展它的子类。我希望保修布尔值对某些人是正确的,对其他人是错误的。 我已更新代码以反映…更新

现在看来Gift正在被实现,但我得到一个错误,说明它不是

public class CustomerGifts {

    public static void giveGift() {

    }

    public static void main(String[] args) {

        int[] crowd = new int[100];

        for (int person : crowd) {

            int prize = (int)(Math.random() * 11);

            if(prize >= 0 && prize <= 3) {
                System.out.println("Computer");
                Computer computer = new Computer(true);
                computer.hasWarranty();
            } else if(prize >= 4 && prize <=7) {
                System.out.print("Entertainment");
            } else if (prize >= 8 && prize <= 10) {
                System.out.print("Office");
            }

            System.out.println(prize);
        }

    }

}

class Customer {
    int returns;
}

interface Gift {
    boolean hasWarranty(boolean arg);
}

class Computer implements Gift {
    boolean processor = true;
    boolean warranty;

    Computer(boolean returnable) {
        this.warranty = returnable;
    }

    boolean hasWarranty() {
        return this.warranty;
    }
}
我建议您将Gift定义为在类中实现的接口。比如:

interface Gift {
    boolean hasWarranty();
}

class Computer implements Gift {
    private boolean warranty;

    @Override
    public boolean hasWarranty() {
        return warranty;
    }
}

class GreetingCard implements Gift {
    @Override
    public boolean hasWarranty() {
        return false;
    }
}

所以,也许可以在你的承包商中设置它。很抱歉,我的评论没有任何帮助。保修只取决于礼物的类型,还是你仍然可以拥有一台没有保修的电脑?如果仅取决于类型,则应将抽象方法设置为公共抽象布尔值;在Gift中,并在Computer这样的子类中重写它:@override public boolean isWarranty{return true;}@ErwinBolwidt谢谢。这听起来更像我要找的。我会试试看。欢迎来到SO。扩展到@ ErWiBulvt的评论:你可能会考虑让礼物成为一个界面而不是一个类。那么,isWarranty可能是一种需要实现的方法。它甚至可以有一个返回默认值的默认实现。