Java的垃圾回收与析构源代码 第2页
TestConstructorPerson.java
public class TestConstructorPerson{
/*定义成员变量*/
String personName;//姓名
int personAge;//年龄
String personSex;//性别
/*定义构造函数TestConstructorPerson ( )*/
public TestConstructorPerson( ){
}
/*定义构造函数TestConstructorPerson(String name,int age,String sex)*/
public TestConstructorPerson(String name,int age,String sex){
personName = name;
personAge = age;
personSex = sex;
}
public String getPersonName( ){
return personName;
}
public int getPersonAge( ){
return personAge;
}
public String getPersonSex( ){
return personSex;
}
public static void main(String args[]){
/*调用第一个构造函数生成对象person1,对象的成员变量抉微系统默认值*/
TestConstructorPerson person1 = new TestConstructorPerson();
//为对象的成员变量赋值
person1.personName = "王义";
person1.personAge = 25;
person1.personSex = "男";
System.out.println("姓名:"+outName+",年龄:"+outAge+",性别:"+outSex);
/*调用第二个构造函数生成对象person2,并为对象的成员变量赋值*/
TestConstructorPerson person2 = new TestConstructorPerson("李玲",20,"女");
outName = person2.getPersonName();
outAge = person2.getPersonAge();
outSex = person2.getPersonSex();
System.out.println("姓名:"+outName+",年龄:"+outAge+",性别:"+outSex);
}
}TestOLConPerson.java
public class TestOLConPerson{
/*定义成员变量*/
String personName;//姓名
int personAge;//年龄
String personSex;//性别
/*定义构造函数TestOLConPerson ( )*/
public TestOLConPerson ( ){
}
/*定义构造函数TestOLConPerson (String name,int age,String sex)*/
public TestOLConPerson (String name, int age, String sex){
personName = name;
personAge = age;
personSex = sex;
}
public static void main(String args[]){
/*调用第一个构造函数生成对象person1,对象的成员变量抉微系统默认值*/
TestOLConPerson person1 = new TestOLConPerson ();
//为对象的成员变量赋值
person1.personName = "王义";
person1.personAge = 25;
person1.personSex = "男";
System.out.println("姓名:"+ person1.personName+",年龄:"+ person1.personAge+",性别:"+ person1.personSex);
/*调用第二个构造函数生成对象person2,并为对象的成员变量赋值*/
TestOLConPerson person2 = new TestOLConPerson ("李玲",20,"女");
System.out.println("姓名:"+ person2.personName+",年龄:"+ person2.personAge+",性别:"+ person2.personSex);
}
}TestOverloadPerson.java
public class TestOverloadPerson{
/*定义成员变量*/
private String personName;//姓名
private int personAge;//年龄
private String personSex;//性别
private String workPlace;//工作单位
private String phoneNum;//电话号码
/*定义成员方法*/
public void setPersonVar(){
personName = "姓名未知";
personAge = 0;
personSex = "性别未知";
workPlace = "工作地点未知";
phoneNum = "电话未知";
}
public void setPersonVar(String name, int age, String sex){
personName = name;
personAge = age;
personSex = sex;
}
public void setPersonVar(String name, int age, String sex, String work, String phone){
personName = name;
personAge = age;
personSex = sex;
workPlace = work;
phoneNum = phone;
}
public static void main(String args[]){
TestOverloadPerson person = new TestOverloadPerson();
person.setPersonVar();
System.out.println("姓名:"+person.personName+",年龄:"+person.personAge+",性别:"+person.personSex+",工作地点:"+person.workPlace+",电话号码:"+person.phoneNum);
person.setPersonVar("王义",25,"男");
System.out.println("姓名:"+person.personName+",年龄:"+person.personAge+",性别:"+person.personSex+",工作地点:"+person.workPlace+",电话号码:"+person.phoneNum);
person.setPersonVar("李玲",20,"女","南京","88888888");
System.out.println("姓名:"+person.personName+",年龄:"+person.personAge+",性别:"+person.personSex+",工作地点:"+person.workPlace+",电话号码:"+person.phoneNum);
}
}