如何通过外部类构造函数访问内部类?

时间:2015-11-10 02:18:26

标签: java

我正在尝试使用代码

在我的main中使用person构造函数创建一个人
Person outerClass = new Person("Anon", new Date(06,03,1991), null);

但它说它找不到类Date。我是否正确地填充了这个构造函数并调用了类间的权利?

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);
            }

1 个答案:

答案 0 :(得分:1)

由于你的要求需要一个内部类,所以让我们留出一些关于Date是否适合成为Person的内部类的问题。

它找不到类Date,因为它是私有的。但是,即使它是公共的,您仍然无法实例化,因为您首先需要Person的实例。一种方法是从构造函数中删除Date参数并改为使用mutators。 e.g:

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);
    }
}

但是,如果内部类独立于外部类的实例存在是有意义的,那么它应该是静态的。这样您就可以在没有现有Date的情况下自行实例化Person。 e.g:

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”结尾。