• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 9次 | 进入软件测试论坛讨论

领测软件测试网 *
* @(#)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

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网