2015-11-10 137 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”结束。

相关问题