标签:重载 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;
};
};
标签:重载 oid set pre sys [] stat ram class
原文地址:http://blog.51cto.com/f1yinsky/2133068