Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 内部阶级。它的目的是什么?_Java_Oop_Inner Classes - Fatal编程技术网

Java 内部阶级。它的目的是什么?

Java 内部阶级。它的目的是什么?,java,oop,inner-classes,Java,Oop,Inner Classes,有人能告诉我拥有内部类的目的是什么吗?我可以想到一些,但可能它们不是使用内部类的好理由。我的理由是,当您想要使用其他类无法使用的类时,内部类是有用的。还有什么?在内部使用链表存储元素的列表实现可以充分利用内部类来表示列表中的节点。我认为你说的一针见血,你会使用这样一个类,你想在类内部使用它,但不想让它公开——一个“一次性”的类,它只在“这里”真正有用。当我学习Java时,我们使用内部类来处理GUI事件类。它是一种“一次性使用”类,不需要对其他类可用,只与它所在的类相关。在多个类(通过继承而无关)

有人能告诉我拥有内部类的目的是什么吗?我可以想到一些,但可能它们不是使用内部类的好理由。我的理由是,当您想要使用其他类无法使用的类时,内部类是有用的。还有什么?

在内部使用链表存储元素的列表实现可以充分利用内部类来表示列表中的节点。我认为你说的一针见血,你会使用这样一个类,你想在类内部使用它,但不想让它公开——一个“一次性”的类,它只在“这里”真正有用。

当我学习Java时,我们使用内部类来处理GUI事件类。它是一种“一次性使用”类,不需要对其他类可用,只与它所在的类相关。

在多个类(通过继承而无关)具有概念上相似的实现细节的情况下,我使用内部类(在C++中),它们构成了公共接口的隐式部分,应该以类似的方式命名

class lib::Identifier { ... };

class lib::Person {
public:
    class Identifier : public lib::Identifier { ... };
};

class lib::File {
public:
    class Identifier : public lib::Identifier { ... };
};

这使得在适当的范围内将
Identifier
Person::Identifier
File::Identifier
简单地称为
Identifier

我使用内部类来定义一个结构,该结构最好由包含的类来表示,但是使用单独的外部类来表示结构并不一定有意义

举个例子,我有一个类,它表示一种特定类型的网络设备,并且该类具有可以在该设备上运行的特定类型的测试。对于每个测试,还可以发现一组潜在的错误。每种类型的设备可能有不同的错误结构

有了这个,你可以做像这样的事情

List<Error> errors = RemoteDeviceA.getErrors();
当然还有其他方法可以做到这一点,这只是一种内部类方法

上述的简化(亦称不完整)代码:

public class RemoteDeviceA {

    private String host;
    private String user;
    private String password;
    private static List<Error> errors;

    public RemoteDeviceA(String user, String host, String password) {
        this.host = host;
        this.user = user;
        this.password = password;

        login();
    }

    private void login() {
        // Logs in
    }

    public void runTestA() {

        List<Error> errorList = new ArrayList<Error>();

        //loop through test results

        if (!value.equals("0")) {
            Error error = new Error(node, rackNum, shelfNum, slotNum, monType, value);
            if (error.isError()) {
                errorList.add(error);
            }
        }
        setErrors(errorList);
    }

    private static void setErrors(List<Error> errors) {
        RemoteDeviceA.errors = errors;
    }

    public List<Error> getErrors() {
        return errors;
    }

    public class Error {

        private String monType;
        private String node;
        private String rack;
        private String shelf;
        private String slot;
        private String value;
        private boolean error = false;
        private boolean historyError = false;
        private boolean critical = false;
        private boolean criticalHistory = false;

        Error(String node, String rack, String shelf, String slot,
                String monType, String value) {
            parseAlarm(node, rack, shelf, slot, monType, value);
        }

        private void parseAlarm(String node, String rack, String shelf,
                String slot, String monType, String value) {

            String modType = "";

            if (monType.startsWith("ES_15") && !value.equals("0")) {
                setMonType("ES_15");
                setError(true);
            } else if (monType.startsWith("SES_15") && !value.equals("0")) {
                setMonType("SES_15");
                setError(true);
            } else if (monType.startsWith("BBE_15") && !value.equals("0")) {
                setMonType("BBE_15");
                setError(true);
            } else if (monType.startsWith("UT_15") && !value.equals("0")) {
                setMonType("UT_15");
                setError(true);
                setCritial(critical);
            } else if (monType.startsWith("ES_24") && !value.equals("0")) {
                setMonType("ES_24");
                setHistoryError(true);
                setError(true);
            } else if (monType.startsWith("SES_24") && !value.equals("0")) {
                setMonType("SES_24");
                setHistoryError(true);
                setError(true);
            } else if (monType.startsWith("BBE_24") && !value.equals("0")) {
                setMonType("BBE_24");
                setHistoryError(true);
                setError(true);
            } else if (monType.startsWith("UT_24") && !value.equals("0")) {
                setMonType("UT_24");
                setHistoryError(true);
                setError(true);
                setCriticalHistory(true);
            } else if (monType.startsWith("UT_15") && !value.equals("0")) {
                setMonType("UT_15");
                setError(true);
                setCritial(true);
            } else if (monType.startsWith("LASPWR")) {

                float laserPwr = Float.valueOf(value);

                if (node.startsWith("LEM_EM")) {
                    if ((laserPwr < 8.0) || (laserPwr > 12.0)) {
                        setMonType("LASERPWR");
                        setError(true);
                    }
                } else if (node.startsWith("LEM10")) {
                    if ((laserPwr < 18.0) || (laserPwr > 22.0)) {
                        setMonType("LASERPWR");
                        setError(true);
                    }
                }
            }

            if (isError()) {
                setNode(node);
                setRack(rack);
                setShelf(shelf);
                setSlot(slot);
                setValue(value);
                setError(true);
            }
        }

        private void setMonType(String monType) {
            this.monType = monType;
        }

        public String getMonType() {
            return monType;
        }

        private void setNode(String node) {
            this.node = node;
        }

        public String getNode() {
            return node;
        }

        public void setRack(String rack) {
            this.rack = rack;
        }

        public String getRack() {
            return rack;
        }

        public void setShelf(String shelf) {
            this.shelf = shelf;
        }

        public String getShelf() {
            return shelf;
        }

        public void setSlot(String slot) {
            this.slot = slot;
        }

        public String getSlot() {
            return slot;
        }

        private void setValue(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        private void setError(boolean error) {
            this.error = error;
        }

        public boolean isError() {
            return error;
        }  

        public void setCritial(boolean critical) {
            this.critical = critical;
        }   

        public boolean isCritical() {
            return critical;
        }   

        public void setCriticalHistory(boolean criticalHistory) {
            this.criticalHistory = criticalHistory;
        }  

        public boolean isCriticalHistory() {
            return criticalHistory;
        }  

        public void setHistoryError(boolean historyError) {
            this.historyError = historyError;
        }

        public boolean isHistoryError() {
            return historyError;
        }
    }
}
公共类RemoteDeviceA{
私有字符串主机;
私有字符串用户;
私有字符串密码;
私有静态列表错误;
公共RemoteDeviceA(字符串用户、字符串主机、字符串密码){
this.host=host;
this.user=用户;
this.password=密码;
登录();
}
私有void登录(){
//登录
}
公共无效runTestA(){
List errorList=new ArrayList();
//循环测试结果
如果(!value.equals(“0”)){
Error Error=新错误(节点、rackNum、shelfNum、slotNum、monType、值);
if(error.isError()){
errorList.add(错误);
}
}
设置错误(错误列表);
}
私有静态void setErrors(列表错误){
RemoteDeviceA.errors=错误;
}
公共列表getErrors(){
返回错误;
}
公共类错误{
私有字符串monType;
私有字符串节点;
专用线架;
私人货架;
专用串槽;
私有字符串值;
私有布尔错误=false;
私有布尔历史错误=false;
私有布尔临界值=false;
私有布尔临界历史=false;
错误(字符串节点、字符串机架、字符串托架、字符串插槽、,
字符串(类型、字符串值){
解析报警(节点、机架、机架、插槽、monType、值);
}
专用警报(字符串节点、字符串机架、字符串托架、,
字符串槽、字符串monType、字符串值){
字符串modType=“”;
if(monType.startsWith(“ES_15”)&&!value.equals(“0”)){
setMonType(“ES_15”);
设置错误(真);
}else if(monType.startsWith(“SES_15”)&&!value.equals(“0”)){
setMonType(“SES_15”);
设置错误(真);
}else if(monType.startsWith(“BBE_15”)&&!value.equals(“0”)){
setMonType(“BBE_15”);
设置错误(真);
}else if(monType.startsWith(“UT_15”)&&!value.equals(“0”)){
setMonType(“UT_15”);
设置错误(真);
设置临界(临界);
}else if(monType.startsWith(“ES_24”)&&!value.equals(“0”)){
setMonType(“ES_24”);
setHistoryError(真);
设置错误(真);
}else if(monType.startsWith(“SES_24”)&&!value.equals(“0”)){
setMonType(“SES_24”);
setHistoryError(真);
设置错误(真);
}else if(monType.startsWith(“BBE_24”)&&!value.equals(“0”)){
setMonType(“BBE_24”);
setHistoryError(真);
设置错误(真);
}else if(monType.startsWith(“UT_24”)&&!value.equals(“0”)){
setMonType(“UT_24”);
setHistoryError(真);
设置错误(真);
setCriticalHistory(真);
}else if(monType.startsWith(“UT_15”)&&!value.equals(“0”)){
setMonType(“UT_15”);
设置错误(真);
setCritial(真);
}else if(monType.STARTSWITS(“LASPWR”)){
float laserPwr=float.valueOf(值);
if(node.startsWith(“LEM_EM”)){
如果((激光波比<8.0)| |(激光波比>12.0)){
setMonType(“LASERPWR”);
设置错误(真);
}
}else if(node.startsWith(“LEM10”)){
如果((激光波比<18.0)| |(激光波比>22.0)){
setMonType(“LASERPWR”);
设置错误(真);
}
}
}
if(isError()){
setNode(node);
设置机架(机架);
固定架;
固定批次(槽);
设定值(
public class RemoteDeviceA {

    private String host;
    private String user;
    private String password;
    private static List<Error> errors;

    public RemoteDeviceA(String user, String host, String password) {
        this.host = host;
        this.user = user;
        this.password = password;

        login();
    }

    private void login() {
        // Logs in
    }

    public void runTestA() {

        List<Error> errorList = new ArrayList<Error>();

        //loop through test results

        if (!value.equals("0")) {
            Error error = new Error(node, rackNum, shelfNum, slotNum, monType, value);
            if (error.isError()) {
                errorList.add(error);
            }
        }
        setErrors(errorList);
    }

    private static void setErrors(List<Error> errors) {
        RemoteDeviceA.errors = errors;
    }

    public List<Error> getErrors() {
        return errors;
    }

    public class Error {

        private String monType;
        private String node;
        private String rack;
        private String shelf;
        private String slot;
        private String value;
        private boolean error = false;
        private boolean historyError = false;
        private boolean critical = false;
        private boolean criticalHistory = false;

        Error(String node, String rack, String shelf, String slot,
                String monType, String value) {
            parseAlarm(node, rack, shelf, slot, monType, value);
        }

        private void parseAlarm(String node, String rack, String shelf,
                String slot, String monType, String value) {

            String modType = "";

            if (monType.startsWith("ES_15") && !value.equals("0")) {
                setMonType("ES_15");
                setError(true);
            } else if (monType.startsWith("SES_15") && !value.equals("0")) {
                setMonType("SES_15");
                setError(true);
            } else if (monType.startsWith("BBE_15") && !value.equals("0")) {
                setMonType("BBE_15");
                setError(true);
            } else if (monType.startsWith("UT_15") && !value.equals("0")) {
                setMonType("UT_15");
                setError(true);
                setCritial(critical);
            } else if (monType.startsWith("ES_24") && !value.equals("0")) {
                setMonType("ES_24");
                setHistoryError(true);
                setError(true);
            } else if (monType.startsWith("SES_24") && !value.equals("0")) {
                setMonType("SES_24");
                setHistoryError(true);
                setError(true);
            } else if (monType.startsWith("BBE_24") && !value.equals("0")) {
                setMonType("BBE_24");
                setHistoryError(true);
                setError(true);
            } else if (monType.startsWith("UT_24") && !value.equals("0")) {
                setMonType("UT_24");
                setHistoryError(true);
                setError(true);
                setCriticalHistory(true);
            } else if (monType.startsWith("UT_15") && !value.equals("0")) {
                setMonType("UT_15");
                setError(true);
                setCritial(true);
            } else if (monType.startsWith("LASPWR")) {

                float laserPwr = Float.valueOf(value);

                if (node.startsWith("LEM_EM")) {
                    if ((laserPwr < 8.0) || (laserPwr > 12.0)) {
                        setMonType("LASERPWR");
                        setError(true);
                    }
                } else if (node.startsWith("LEM10")) {
                    if ((laserPwr < 18.0) || (laserPwr > 22.0)) {
                        setMonType("LASERPWR");
                        setError(true);
                    }
                }
            }

            if (isError()) {
                setNode(node);
                setRack(rack);
                setShelf(shelf);
                setSlot(slot);
                setValue(value);
                setError(true);
            }
        }

        private void setMonType(String monType) {
            this.monType = monType;
        }

        public String getMonType() {
            return monType;
        }

        private void setNode(String node) {
            this.node = node;
        }

        public String getNode() {
            return node;
        }

        public void setRack(String rack) {
            this.rack = rack;
        }

        public String getRack() {
            return rack;
        }

        public void setShelf(String shelf) {
            this.shelf = shelf;
        }

        public String getShelf() {
            return shelf;
        }

        public void setSlot(String slot) {
            this.slot = slot;
        }

        public String getSlot() {
            return slot;
        }

        private void setValue(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        private void setError(boolean error) {
            this.error = error;
        }

        public boolean isError() {
            return error;
        }  

        public void setCritial(boolean critical) {
            this.critical = critical;
        }   

        public boolean isCritical() {
            return critical;
        }   

        public void setCriticalHistory(boolean criticalHistory) {
            this.criticalHistory = criticalHistory;
        }  

        public boolean isCriticalHistory() {
            return criticalHistory;
        }  

        public void setHistoryError(boolean historyError) {
            this.historyError = historyError;
        }

        public boolean isHistoryError() {
            return historyError;
        }
    }
}