2015-11-10 196 views
-1

我試圖創建一個使用在我的主要的人的構造人與代碼如何通過外部類構造函數訪問內部類?

Person outerClass = new Person("Anon", new Date(06,03,1991), null); 

,但它說,它無法找到該類日期。我是否正確地填寫這個構造函數並調用了類間?

public class Person implements Cloneable 
    { 
     private String name; 
     private Date born; 
     private Date died;//null indicates still alive. 

     public Person(String initialName, Date birthDate, Date deathDate) 
     { 
      if (consistent(birthDate, deathDate)) 
      { 
       name = initialName; 
       born = new Date(birthDate); 
       if (deathDate == null) 
        died = null; 
       else 
        died = new Date(deathDate); 
      } 
      else 
      { 
       System.out.println("Inconsistent dates. Aborting."); 
       System.exit(0); 
      } 
     } 
     private class Date 
     { 
      private String month; 
      private int day; 
      private int year; //a four digit number. 

      public Date() 
      { 
       month = "January"; 
       day = 1; 
       year = 1000; 
      } 

      public Date(int monthInt, int day, int year) 
      { 
       setDate(monthInt, day, year); 
      } 
+2

爲什麼在所有的事情聖潔的名稱,你會讓日期私人內部類?爲什麼不簡單地使它成爲一個獨立的公共類(儘管改變它的名字以避免與java.util.Date混淆)? –

+0

多數民衆贊成我也在想,但任務要求它:/ – rreg101

+0

請顯示實際的全部要求。你可能會誤解他們。 –

回答

1

由於您的要求需要一個內部類,讓我們拋開有關它是否是合適的問題,爲Date是一個內部類的Person

它找不到類Date,因爲它是私人的。然而,即使它是公開的,你仍然不能實例化一個,因爲你首先需要一個Person的實例。一種方法是從構造函數中刪除Date參數,並使用mutators。例如爲:

public class Person { 
    private final String name; 
    private Date birthDate; 

    public class Date { 
     private final int year, month, day; 

     public Date(final int year, final int month, final int day) { 
      this.year = year; 
      this.month = month; 
      this.day = day; 
     } 

     public String toString() { 
      // look at me, I can access attributes of the enclosing Person 
      return name + " is associated with the year " + year; 
     } 
    } 

    public Person(final String name) { 
     this.name = name; 
    } 

    public void setBirthDate(final Date birthDate) { 
     this.birthDate = birthDate; 
    } 

    public static final void main(final String... arguments) throws Exception { 
     final Person person = new Person("name"); 
     final Date birthDate = person.new Date(2015, 11, 9); 
     person.setBirthDate(birthDate); 
    } 
} 

然而,如果是有意義的內部類獨立存在外類的一個實例的,那麼它應該是靜態的。這將允許您在沒有現有的Person的情況下自行實例化Date。例如: -

public class Person { 
    private final String name; 
    private final Date birthDate; 

    public static class Date { 
     private final int year, month, day; 

     public Date(final int year, final int month, final int day) { 
      this.year = year; 
      this.month = month; 
      this.day = day; 
     } 

     public String toString() { 
      // I do not know about any Person 
      return "year: " + year; 
     } 
    } 

    public Person(final String name, final Date birthDate) { 
     this.name = name; 
     this.birthDate = birthDate; 
    } 

    public static final void main(final String... arguments) throws Exception { 
     final Person person = new Person("name", new Date(2015, 11, 9)); 
    } 
} 

注意,這是在功能上與聲明Date作爲自己的頂級類不同的是,現在的完全限定類名「Person.Date」結束。

相關問題