·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » java 多态与抽象工厂-----------菜鸟学飞第二步

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

类别: JAVA教程  评论数:0 总得分:0
*
* @(#)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




-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1