首页 > 学院 > 开发设计 > 正文

java 多态与抽象工厂-----------菜鸟学飞第二步

2019-11-18 14:39:03
字体:
来源:转载
供稿:网友

  win2000 AdvSer
JCreator PRo 2.5
JDK 1.4.2
================================
/*
* @(#)ClsDyna.java 1.0 04/12/25
*
* You can modify the template of this file in the
* Directory ../JCreator/Templates/Template_1/Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ../JCreator/Template/. Use the other
* templates as examples.
*
*/
package myprojects.clsdyna;
import java.util.*;

class ClsDyna {

public ClsDyna() {

}

public static void main(String args[]) {
System.out.println("Starting ClsDyna...");
person pr=new boy("万青");
person pg=new girl("李敏");
System.out.println(pr.toString());
System.out.println(pg.toString());
System.out.println("==========");
personFactory pc=new personFactory("吴波",true,33);
person tc=pc.GetPerson();
System.out.println(tc.getName());
System.out.println(tc.toString());


}
}
abstract class person
{
protected String name;
protected boolean sex;
protected int age;
protected int id;
private static int pid;
static {
pid=1000;
}
public static int PersonID()
{
return pid++;
}
public person(String na,boolean sx,int ag)
{
/*
//it also can write like this
name=na;sex=sx;
age=ag;
id=person.PersonID();

*/
this(na,ag);
if(!sx)
this.setSex(false);



}
public person(String na,int ag)
{
name=na;age=ag;sex=true;//this(na);
id=person.PersonID();
}
public person(String na)
{
name=na;age=18;sex=true;
id=person.PersonID();
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public void setName(String na)
{
name=na;
}
public int getAge()
{
return age;
}
public void setAge(int dg)
{
age=dg;
}
public boolean isSex()
{
return sex;
}
protected void setSex(boolean sx)
{
sex=sx;
}
public String toString()
{
return "ID:"+id+";Name:"+name+";Age:"+age+";Sex:"+ (sex?"男":"女");

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表