找回密码
 立即注册
企业管理 2022-09-23 123 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
定义一个Person类,生成一个目标,请求里面有名字、年纪、成果属性,以后把此目标保留在文件当中,可以在屏幕上显示人员的信息,也可以修复信息,可是请求有一个完整的菜单呈现。
假如终结此程序需求有几个类呢?
1、Person类
2、文件的操作类
3、输入数据的类
4、详细的操作Person的类
使用Eclipse进行开发
先分别定义以下几个类:
Person类
redking.blog.51cto/attachment/200903/3/27212_1236090918Qbgr.png
redking.blog.51cto/attachment/200903/3/27212_1236090923alT4.png
定义Person类属性
redking.blog.51cto/attachment/200903/3/27212_12360909272E5e.png
PersonOperate类
redking.blog.51cto/attachment/200903/3/27212_123609093157j8.png
redking.blog.51cto/attachment/200903/3/27212_1236090936J1FD.png
InputData输入数据类
redking.blog.51cto/attachment/200903/3/27212_12360909401hog.png
定义三个方法:
redking.blog.51cto/attachment/200903/3/27212_1236090945OKx1.png
Menu菜单显示类:
redking.blog.51cto/attachment/200903/3/27212_1236090950A7RJ.png
redking.blog.51cto/attachment/200903/3/27212_1236090954v0sf.png
FileOperate文件操作类
redking.blog.51cto/attachment/200903/3/27212_1236090959KBvW.png
redking.blog.51cto/attachment/200903/3/27212_12360909637WGZ.png
Main主类测试客户端:
redking.blog.51cto/attachment/200903/3/27212_12360909685PHV.png
redking.blog.51cto/attachment/200903/3/27212_1236090973PaVU.png
定义好这五个类后我们就要分别写代码哈~
首先是Person类
我们要写上Setter和Getter方法
redking.blog.51cto/attachment/200903/3/27212_12360909802KLB.png
redking.blog.51cto/attachment/200903/3/27212_12360909878eYR.png
redking.blog.51cto/attachment/200903/3/27212_1236090993K3CS.png
然后写上两个结构方法
redking.blog.51cto/attachment/200903/3/27212_1236091001FJR5.png
redking.blog.51cto/attachment/200903/3/27212_1236091005ePJP.png
redking.blog.51cto/attachment/200903/3/27212_1236091012aP76.png
去掉正告CTRL+1
redking.blog.51cto/attachment/200903/3/27212_12360910185k8q.png
redking.blog.51cto/attachment/200903/3/27212_1236091026JU7y.png
这样Person类就写完了哈~~~
InputData类写代码
一切的输入数据的代码实际上都是很类似的,所以在此处设计一个InputData类,此类专门用于获得输入的各种数据类型。
redking.blog.51cto/attachment/200903/3/27212_1236091033HtRK.png
这样就终结了输入数字的功能,我们在Test中进行验证
redking.blog.51cto/attachment/200903/3/27212_1236091035ulC2.png
redking.blog.51cto/attachment/200903/3/27212_1236091038nQSY.png
Person类完整代码:
packageorg.redking.demo.vo;
importjava.io.Serializable;
@SuppressWarnings(serial)
publicclassPersonimplementsSerializable{
privateStringname;
privateintage;
privatefloatscore;
publicPerson(){}
publicPerson(Stringname,intage,floatscore){
super();
this.name=name;
this.age=age;
this.score=score;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicfloatgetScore(){
returnscore;
}
publicvoidsetScore(floatscore){
this.score=score;
}
publicStringtoString(){
return名字:+this.name+,年纪:+this.age+,成果:+this.score;
}
}
PersonOperate类完整代码:
packageorg.redking.demo.op;
importorg.redking.demo.util.FileOperate;
importorg.redking.demo.util.InputData;
importorg.redking.demo.vo.Person;
publicclassPersonOperate{
privateInputDatainput=null;
publicPersonOperate(){
this.input=newInputData();
}
//终结详细的Person目标操作
publicvoidadd(){
//要使用输入数据的类
Stringname=null;
intage=0;
floatscore=0.0f;
System.out.print(输入名字:);
name=this.input.getString();
System.out.print(输入年纪:);
age=this.input.getInt();
System.out.print(输入成果:);
score=this.input.getFloat();
//生成Person目标,把目标保留在文件中
Personp=newPerson(name,age,score);
try{
newFileOperate().save(p);
System.out.println(数据保留成功!);
}catch(Exceptione){
System.out.println(数据保留失败!);
}
}
publicvoidshow(){
//从文件中把内容读进来
Personp=null;
try{
p=(Person)newFileOperate().read();
}catch(Exceptione){
System.out.println(内容显示失败,请断定数据是不是存在!);
}
if(p!=null){
System.out.println(p);
}
}
publicvoidupdate(){
abbr_266ac41fdcf35af9f41c6793b6acffb8.doc

(Define a Person class, generate a target, the request has name, age, and achievement attributes, and later keep this target in the file, you can display the information of the person on the screen, and you can repair the information, but the request has a complete menu presentation .
How many classes are required to terminate this program?
1. Person class
2. File operation class
3. Type of input data
4. Detailed operation of the Person class
Develop with Eclipse
First define the following classes:
Person class
redking.blog.51cto/attachment/200903/3/27212_1236090918Qbgr.png
redking.blog.51cto/attachment/200903/3/27212_1236090923alT4.png
Define Person class properties
redking.blog.51cto/attachment/200903/3/27212_12360909272E5e.png
PersonOperate class
redking.blog.51cto/attachment/200903/3/27212_123609093157j8.png
redking.blog.51cto/attachment/200903/3/27212_1236090936J1FD.png
InputData input data class
redking.blog.51cto/attachment/200903/3/27212_12360909401hog.png
Define three methods:
redking.blog.51cto/attachment/200903/3/27212_1236090945OKx1.png
Menu menu display class:
redking.blog.51cto/attachment/200903/3/27212_1236090950A7RJ.png
redking.blog.51cto/attachment/200903/3/27212_1236090954v0sf.png
FileOperate file operation class
redking.blog.51cto/attachment/200903/3/27212_1236090959KBvW.png
redking.blog.51cto/attachment/200903/3/27212_12360909637WGZ.png
Main main class test client:
redking.blog.51cto/attachment/200903/3/27212_12360909685PHV.png
redking.blog.51cto/attachment/200903/3/27212_1236090973PaVU.png
After defining these five classes, we have to write the code separately~
First is the Person class
We're going to write Setter and Getter methods
redking.blog.51cto/attachment/200903/3/27212_12360909802KLB.png
redking.blog.51cto/attachment/200903/3/27212_12360909878eYR.png
redking.blog.51cto/attachment/200903/3/27212_1236090993K3CS.png
Then write two structure methods
redking.blog.51cto/attachment/200903/3/27212_1236091001FJR5.png
redking.blog.51cto/attachment/200903/3/27212_1236091005ePJP.png
redking.blog.51cto/attachment/200903/3/27212_1236091012aP76.png
Remove yellow warning CTRL 1
redking.blog.51cto/attachment/200903/3/27212_12360910185k8q.png
redking.blog.51cto/attachment/200903/3/27212_1236091026JU7y.png
In this way, the Person class is finished.~~~
InputData class to write code
The code of all input data is actually very similar, so design an InputData class here, which is specially used to obtain various data types of input.
redking.blog.51cto/attachment/200903/3/27212_1236091033HtRK.png
This ends the function of entering numbers, and we verify it in Test
redking.blog.51cto/attachment/200903/3/27212_1236091035ulC2.png
redking.blog.51cto/attachment/200903/3/27212_1236091038nQSY.png
The complete code of the Person class:
packageorg.redking.demo.vo;
importjava.io.Serializable;
@SuppressWarnings(serial)
publicclassPersonimplementsSerializable{
privateStringname;
privateintage;
privatefloatscore;
publicPerson(){}
publicPerson(Stringname,intage,floatscore){
super();
this.name=name;
this.age=age;
this.score=score;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicfloatgetScore(){
returnscore;
}
publicvoidsetScore(floatscore){
this.score=score;
}
publicStringtoString(){
return name:  this.name , age:  this.age , achievement:  this.score;
}
}
The complete code of the PersonOperate class:
packageorg.redking.demo.op;
importorg.redking.demo.util.FileOperate;
importorg.redking.demo.util.InputData;
importorg.redking.demo.vo.Person;
publicclassPersonOperate{
privateInputDatainput=null;
publicPersonOperate(){
this.input=newInputData();
}
//End the detailed Person target operation
publicvoidadd(){
//class to use input data
Stringname=null;
intage=0;
floatscore=0.0f;
System.out.print(Enter name:);
name=this.input.getString();
System.out.print(Enter age:);
age=this.input.getInt();
System.out.print(input result:);
score=this.input.getFloat();
//Generate the Person target and keep the target in the file
Personp=newPerson(name,age,score);
try{
newFileOperate().save(p);
System.out.println(Data retention successful!);
}catch(Exceptione){
System.out.println(Data retention failed!);
}
}
publicvoidshow(){
// read the content from the file
Personp=null;
try{
p=(Person)newFileOperate().read();
}catch(Exceptione){
System.out.println(content display failed, please determine whether the data exists!);
}
if(p!=null){
System.out.println(p);
}
}
publicvoidupdate(){
abbr_266ac41fdcf35af9f41c6793b6acffb8.doc)

[下载]10481223310.rar




上一篇:UltraEdit 14.20.1.1000 汉化版[附注册机]
下一篇:从零开始学Java编程