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