1. 程式人生 > >Java中什麽是匿名對象,空參構造方法輸出創建了幾個匿名對象,屬性聲明成static

Java中什麽是匿名對象,空參構造方法輸出創建了幾個匿名對象,屬性聲明成static

es2017 ava cit 得到 定義 屬性 自增 alt spa

package com.swift;
//使用無參構造方法自動生成對象,序號不斷自增
public class Person {
    private static int count; //如果在定義類時,使用的是靜態的屬性,則得到的結果是不同的。count生命周期長,與類相同
    public int id;
    public String name;
    public int age;
    public String city;
    public Person() {
        super();
        count++;
        this.id=count;
        
this.name="NoName"+count; this.age=20; this.city="蜀國"; System.out.println("生產了 "+count+" 個實例化對象");//調用空參構造方法時輸出 } public Person(int id ,String name,int age,String city) { this.id=id; this.name=name; this.age=age; this.city=city; }
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 int getAge() { return age; } public void
setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getInfo() { return "The Person is id=" + id + ", name=" + name + ", age=" + age + ", city=" + city ; } }

技術分享

Java中什麽是匿名對象,空參構造方法輸出創建了幾個匿名對象,屬性聲明成static