继承类中的Java重写

继承类中的Java重写,java,oop,inheritance,Java,Oop,Inheritance,我面临的问题是,每当有人想要.switchOff()一个SmartDevice时,如果它是SmartFridge对象,它应该重写SmartDevice类中的switchOff()方法 问题是java给了我以下错误:操作符!=未为参数类型double定义,null 我不知道如何解决这个问题 @Override public void switchOff() { if(this.getCurrentTemperature()!= null) { this.setSwi

我面临的问题是,每当有人想要.switchOff()一个SmartDevice时,如果它是SmartFridge对象,它应该重写SmartDevice类中的switchOff()方法


问题是java给了我以下错误:操作符!=未为参数类型double定义,null

我不知道如何解决这个问题

@Override
public void switchOff() {
    if(this.getCurrentTemperature()!= null) {
            this.setSwitchedOn(true);
            //if the object is  SmartFridge leave switchedOn value to true

        }
    }
继承的类:

package SmartHomeApp;

public class SmartFridge extends SmartDevice{

    private double currentTemperature;


    public SmartFridge(String name, double location, boolean switchedOn, double currentTemperature)
    {

        super(name, location, switchedOn);
        setLocation(location);
        setName(name);
        setSwitchedOn(true);
        setCurrentTemperature(currentTemperature);
    }

    public double getCurrentTemperature(){ return currentTemperature;}
    public void setCurrentTemperature(double value){ currentTemperature = value;}
智能设备类别:


public class SmartDevice {
    private String name;
    private double location;
    private boolean switchedOn;

    public SmartDevice(String name, double location, boolean switchedOn) {
        setName(name);
        setLocation(location);
        setSwitchedOn(switchedOn);
    }

    //YOU CANT ACCESS the 'private classes' so you need to GET them
    public void setName(String value) {name = value;}
    public void setLocation(double value) {location = value;}
    public void setSwitchedOn(boolean value) {switchedOn = value;}

    public String getName() {return name;}
    public double getLocation() {return location;}
    public boolean getSwitchedOn() {return switchedOn;}

    public void switchOn() {this.switchedOn=true;}
    public void switchOff() {this.switchedOn=false;}

检查“null”的类型不是对象。这种类型是原语,Java为它们设置默认值


您应该查看本文以了解更多信息:

“运算符!=对于参数类型double,null未定义”-函数
getCurrentTemperature
返回
double
,该函数不能为null。所以不能将其与null进行比较。double是原语,而不是对象。你可以用Double来代替。那么我如何检查这个对象是否为空呢?(上面没有currentTemp变量)什么是“空”?如果0为“空”,如果温度实际上为0怎么办?“没有温度”吗?@luk2302我有一个智能家居,可以容纳智能设备和智能冰箱。假设我执行switchOff()方法。该方法如何检查其智能设备还是智能冰箱?