繁体   English   中英

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

[英]How to access a inner class through an outer class constructor?

我正在尝试使用我的 main 中的 person 构造函数和代码创建一个人

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

既然您的需求需要内部类,那么让我们暂且搁置有关Date是否适合作为Person的内部类的问题。

它找不到类Date因为它是私有的。 然而,即使它是公开的,您仍然无法实例化一个,因为您首先需要一个Person的实例。 一种方法是从构造函数中删除Date参数并改用修改器。 例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM