JAVA基本問題(2)

來源:互聯網
上載者:User

標籤:重載   oid   set   pre   sys   []   stat   ram   class   

1、構造器

/** * test -- 構造器 */public class test {        public static void main(String[] args) {                // 成員變數初始化順序                // a.預設構造器初始化                Person p1 = new Person();                System.out.println(p1.getName() + ":" + p1.getAge());                // b.成員變數顯示初始化                Person p2 = new Person("cc");                System.out.println(p2.getName() + ":" + p2.getAge());                Person p3 = new Person(10);                System.out.println(p3.getName() + ":" + p3.getAge());                // c.通過構造器給成員變數初始化                Person p4 = new Person("cc",10);                System.out.println(p4.getName() + ":" + p4.getAge());                // d.通過set方法初始化成員變數                Person p5 = new Person();                p5.setName("dd");                p5.setAge(22);                System.out.println(p5.getName() + ":" + p5.getAge());        }};class Person {        private String name = "aa";        private int age = 1;        // 構造器Person(變數) 作用        // 1、構成器用於建立對象        Person() {                System.out.println("空參構造器");        }        // 2、構造器用於成員變數初始化        // 3、構造器之間可以重載        Person(String n, int a){                this.name = n;                this.age = a;                System.out.println("含參構造器");        }        Person(String n){                this.name = n;                System.out.println("name構造器");        }        Person(int a){                this.age = a;                System.out.println("age構造器");        }        /**         * @return the name         */        public String getName() {                return name;        };        /**         * @param name the name to set         */        public void setName(String name) {                this.name = name;        };        /**         * @return the age         */        public int getAge() {                return age;        };        /**         * @param age the age to set         */        public void setAge(int age) {                this.age = age;        };};

JAVA基本問題(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.