---
# System prepended metadata

title: 內部類別，匿名類別

---

# 內部類別，匿名類別
- 三種賦值
    - 看看以下兩個檔案
        - ==Person.java==
        ```java=
        public class Person {
        private int id;
        private String name;
        private String mail;

        public Person() {
            super();
        }

        public Person(int id, String name, String mail) {
            super();
            this.id = id;
            this.name = name;
            this.mail = mail;
        }


        public int getId() {

            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getMail() {
            return mail;
        }

        public void setMail(String mail) {
            this.mail = mail;
        }

        @Override
        public String toString() {
            return "Person [id=" + id + ", name=" + name + ", mail=" + mail + "]";
        }

        }
        ```
        - ==UsePerson.java==
        ```java=
        public class UserPerson {
            // 第一種方法 method輸入
            public void a() {
                Person use = new Person(5,"天竺鼠","gpig@mail.com");
                
                Person use1 = new Person();
                use1.setId(1);
                use1.setName("小熊");
                use1.setMail("bear@mail.com");

                Person[] usex={use,use1}
            }

            // 第二種方法
            public void b() {
                Person use2 = new Person(2, "阿貓", "cat@mail.com");
                System.out.println(use2.toString());

            }

            // 第三種方法 建構子輸入
            public void c() {
                //匿名類別
                Person[] use = { new Person(3, "狗狗", "dog@mail.com"),//自訂型態
                                  new Person(4, "兔仔", "rabit@mail.com"), };
                for(Person use1:use){
                    ...println(use1);
                }
            }
        }
        ```