当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

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

*
* @(#)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?"男":"女");
}
abstract void sayHello();

}
class boy extends person
{

public boy(String na,int age)
{
super(na,age);
this.setSex(true);
}
public boy(String na)
{
super(na);
this.setSex(true);

}
public void sayHello()
{
System.out.println(this.toString());
}

}
class girl extends person
{
public girl(String na,int age)
{
super(na,age);
this.setSex(false);
}
public girl(String na)
{
super(na);
this.setSex(false);

}
public void sayHello()
{
System.out.println(this.toString());
}

}
class personFactory
{
protected person pBoyGirl;
public personFactory(String na,boolean sx,int ag)
{
if(sx)
pBoyGirl=new boy(na,ag);
else
pBoyGirl=new girl(na,ag);



}
public person GetPerson()
{
return pBoyGirl;
}

}
============================
本人刚学java没几天,不对之处请GGJJDDMM多多指教
若有兄弟不弃,愿意一起成长的请留QQ;另请注明:java study

相关内容
赞助商链接