簡體   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