您的位置:首页 > 编程语言 > Java开发

Java中什么是匿名对象,空参构造方法输出创建了几个匿名对象,属性声明成static

2018-01-01 00:39 519 查看
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 ;
}

}




 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐