java接口用法学习----------------java菜鸟学飞第一步
2024-07-13 09:54:59
供稿:网友
/*
* @(#)clsusage.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.clsusage;
import java.util.*;
class clsusage {
public clsusage() {
}
public static void main(string args[]) {
system.out.println("starting clsusage...");
isplitpage isp=new userpage(12);
isp.firstpage();
while(isp.haspages())
{
isp.nextpage();
}
}
}
interface isplitpage
{
void firstpage();
void nextpage();
boolean haspages();
}
class dateobj
{
private string name;
private int age;
private boolean sex;
private int id;
public static int personid;
static
{
personid=1000;
}
public dateobj(string na,int ag,boolean sx)
{
name=na;age=ag;sex=sx;
id=dateobj.personid++;
}
public dateobj(string na,int ag)
{
name=na;age=ag;sex=true;
id=dateobj.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 this.sex;
}
public void setsex(boolean sx)
{
sex=sx;
}
public string tostring()
{
return ""+id+"-"+name+"-"+age+"-"+sex;
}
}
class userpage implements isplitpage
{
// which can be accessed with itself or son
//与自身或子类所在的包无关
protected int pagecount;
protected int pageindex;
protected java.util.vector list;
public userpage(int pages)
{
if(pages<=0)
this.pagecount=10;
int age=20;
boolean sex=false;
list=new vector();
for(int i=0;i<pages;i++)
{
string na=""+i+"";
dateobj ob=new dateobj(na,age+i,sex);
sex=!sex;
list.addelement(ob);
}
this.pagecount=pages;
this.pageindex=1;
}
public void firstpage()
{
pageindex=1;
dateobj ob=(dateobj)list.get(pageindex-1);
system.out.println("第一页"+ob.tostring());
}
public void nextpage()
{
pageindex++;
if (pageindex<=this.pagecount)
{
dateobj ob=(dateobj)list.get(pageindex-1);
system.out.println("第"+pageindex+"页"+ob.tostring());
}
}
public boolean haspages()
{
return pageindex<=pagecount;
}
}