我们都知道java面向对象的三大特征是封装、多态、和继承。对于一个类来说封装多态就很简单了,继承无非就是多个类之间的父与子的关系。如果我们在做一个项目时把所有的程序都写到一个文件中是不是太麻烦了,那么我们怎么做呢,那就是分解成多个小程序放在不同的文件中,在一个文件中可以通过导入另一个文件来调用那个文件。这样做有几点好处:
1.编译出错时根据错误信息能很快定位到相应的程序中2.可扩展性非常好。这个程序虽然小,但是足可以说明类调用的作用。
这个程序有Main、Menu、Person、PersonOperate、FileOperate和InputData。他们之间的关系如下图
这个程序的运行步骤如下:
运行系统时:它的界面是:
输入1,就会弹出添加信息的提示,输入正确的信息:
选择2,就会将刚添加的信息输出出来:
选择3,就是修改信息提示,输入修改后的信息就会:
选择4,就退出系统。
程序代码如下:
//main类
package main;
import java.io.IOException;
import menu.Menu;
public class Main {
public static void main(String args[]) throws IOException, ClassNotFoundException
{
new Menu();
}
}
//menu类
package menu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import op.PersonOperate;
import util.InputData;
public class Menu {
BufferedReader input= null;
//创建菜单
public Menu() throws IOException, ClassNotFoundException
{
this.input= new BufferedReader( new InputStreamReader(System. in));
while( true)
{
show();
}
}
//显示菜单
private void show() throws IOException, ClassNotFoundException {
System. out.println( "\t\t1.增加人员信息");
System. out.println( "\t\t2.浏览人员信息");
System. out.println( "\t\t3.修改人员信息");
System. out.println( "\t\t4.退出系统");
System. out.println( "请选择:");
//根据用户输入的信息选择相应的功能
int temp= new InputData().getInt();
switch(temp)
{
case 1: PersonOperate.add(); break;
case 2: PersonOperate.show(); break;
case 3: PersonOperate.update(); break;
case 4: System. out.println( "您选择了退出系统");System.exit(1); break;
default:System. out.println( "输入内容必须是0-9,请重新输入:");
}
}
}
//InputData类
package util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class InputData {
BufferedReader input= null; //创建输入流
public InputData()
{
this.input= new BufferedReader( new InputStreamReader(System. in));
}
//得到键盘输入的信息
public String getString() throws IOException
{
String buf;
buf=input.readLine();
return buf;
}
//将从键盘读入的信息转换成int类型
public int getInt() throws IOException
{
String str= null;
int temp = 0;
str= this.getString();
while( true)
{
if(str.matches( "\\d+"))
{
temp=Integer.parseInt(str); break;
}
else
{
System. out.println( "输入的数必须是整数");
}
}
return temp;
}
//将从键盘读入的信息转换成float类型
public float getFloat() throws IOException {
boolean flag= true;
String str= null;
float score = 0;
while( true)
{
str= this.getString();
if(str.matches( "\\d+?"))
{
score=Float.parseFloat(str); break;
}
else
{
System. out.println( "输入格式不正确");
}
}
return score;
}
}
//PersonOperate类
package op;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import util.FileOperate;
import util.InputData;
import vo.Person;
public class PersonOperate {
private static String name;
private static int age;
private static float score;
private static Person per;
static BufferedReader input= new BufferedReader( new InputStreamReader(System. in));
//添加数据
public static void add() throws IOException {
System. out.println( "请输入姓名:");
name= new InputData().getString();
System. out.println( "请输入年龄:");
age= new InputData().getInt();
System. out.println( "请输入成绩:");
score= new InputData().getFloat();
per= new Person(name,age,score);
new FileOperate().save(per);
}
//浏览数据
public static void show() throws FileNotFoundException, IOException, ClassNotFoundException
{
Person p=(Person)( new FileOperate().read());
System. out.println(p);
}
//更新数据
public static void update() throws IOException, ClassNotFoundException {
Person p=(Person)( new FileOperate().read()); //读出原数据
System. out.println( "请输入新的姓名(原姓名:"+p.getName()+ "):");
name= new InputData().getString();
System. out.println( "请输入新的年龄(原年龄:"+p.getAge()+ "):");
age= new InputData().getInt();
System. out.println( "请输入新的成绩(原成绩:"+p.getScore()+ "):");
score= new InputData().getFloat();
per= new Person(name,age,score);
new FileOperate().update(per);
}
}
//FileOperate类
package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import vo.Person;
public class FileOperate {
File f= new File( "e:\\guanli.txt");
ObjectOutputStream out= null; //创建输出流
ObjectInputStream input= null; //创建输入流
//保存数据到文件中
public void save(Object per) throws IOException {
out= new ObjectOutputStream( new FileOutputStream(f));
out.writeObject(per);
System. out.println( "数据保存成功");
}
//从文件中读取数据
public Object read() throws FileNotFoundException, IOException, ClassNotFoundException {
input= new ObjectInputStream( new FileInputStream(f));
return input.readObject();
}
//同样是将修改后的数据保存到文件中
public void update(Object per) throws FileNotFoundException, IOException {
out= new ObjectOutputStream( new FileOutputStream(f));
out.writeObject(per);
System. out.println( "数据更新成功");
}
}
//Person类
package vo;
import java.io.Serializable;
public class Person implements Serializable{
private String name;
private int age;
private float score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore( float score) {
this.score = score;
}
public Person(String name, int age, float score)
{
this.setName(name);
this.setAge(age);
this.setScore(score);
}
public String toString()
{
return "姓名:"+ this.name+ "年龄"+ this.age+ "成绩"+ this.score;
}
}
转载于:https://blog.51cto.com/tscfengkui/416540