2010-08-02 104 views
5

有人可以告诉我什么是有内部类的目的吗?我能想到一些,但可能它们不是使用内部类的好理由。我的推理是,当你想使用一个没有其他类可以使用的类时,内部类是有帮助的。还有什么?内部类。它的目的是什么?

+0

也许到另一种结构的相对复杂的类的内部工作,而不使他们暴露于普通大众?还是要描述一个复杂的数据类型,只有在包含类的上下文中才有意义? – tdammers 2010-08-02 23:03:07

回答

1

内部使用链表来存储元素的列表实现可以很好地利用内部类来表示列表中的节点。我认为你已经达成共识,说你会使用这样一个班级,你希望在班级内部使用这样的班级,但不想让班级暴露出来 - 这是一个“一次性”的班级,只是非常有用'这里'。

3

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

+0

你应该详细说明......具体来说,在这种情况下*不使用内部类的危险是什么? – Nitrodist 2010-08-03 00:11:00

1

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

class lib::Identifier { ... }; 

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

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

这可以方便地参考IdentifierPerson::IdentifierFile::Identifier简单地Identifier,在适当的范围。

2

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

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

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

List<Error> errors = RemoteDeviceA.getErrors(); 

用方法是从内部类,像

for (Error error : errors) { 
     System.out.println("MOnitor Type: " + error.getMonType()); 
     ... 
    } 

当然还有其他方法可以做到这一点,这只是一个内部一流的方法。

简体(又名不完全)为上面的代码:

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; 
     } 
    } 
} 
相关问题