实验七:类的多态性
package homeWork;
import java.util.Scanner;
import static java.lang.System.exit;
public class HomeWork {
static Object[] objects=new Object[100];//存放数据的地方
static int count=0;
private static Scanner input=new Scanner(System.in);
static String zhuangye1="软件工程";
static String zhuangye2="网络";
public static void main(String[] args) {
int flag=6;
while(true) {
System.out.println("你具有如下的功能选择:");
System.out.println("1:添加学生信息");
System.out.println("2:删除学生基本信息");
System.out.println("3:查找学生基本信息");
System.out.println("4:录入学生的基本成绩");
System.out.println("5:退出 EXIT--->");
flag=input.nextInt();
boolean flag1=false;
switch (flag){
case 1: flag1=Add() ;if(flag1){
System.out.println("添加成功");
}break;
case 2: Delect();break;
case 3: FindStudents();break;
case 5: System.out.println("——————————————————————————退出学生管理系统——————————————————————————————————");exit(0);
case 4: ;AddScore();break;
//case 6: ;break;
default:exit(0);
}
}
}
private static boolean Add(){
boolean flag=false;
System.out.println("请选择对应的专业A 软件工程,B 网络工程");
String s;
s=input.next();
char p=s.charAt(0);
String name;String gender;String province;String city;String zhuangye;
if(p=='A'){
System.out.println("请输入你的名字");
name=input.next();
System.out.println("请输入你的性别");
gender=input.next();
System.out.println("请输入你的省份");
province=input.next();
System.out.println("请输入你的城市");
city=input.next();
zhuangye=zhuangye1;
Students students=new Students(name,gender,province,city,zhuangye);
objects[count]=students;//多态的体现
count++;
flag=true;
}
else if(p=='B') {
System.out.println("请输入你的名字");
name=input.next();
System.out.println("请输入你的性别");
gender=input.next();
System.out.println("请输入你的省份");
province=input.next();
System.out.println("请输入你的城市");
city=input.next();
zhuangye=zhuangye2;
Students students=new Students(name,gender,province,city,zhuangye);
objects[count]=students;
count++;
flag=true;
}
else {
flag=false;
}
return flag;
}
private static boolean Delect(){
boolean flag=false;
return flag;
}
private static boolean AddScore(){
boolean flag=false;
System.out.println("请选择你的的专业A 软件工程,B 网络工程");
String s;
s=input.next();
char p=s.charAt(0);
String name;
int socre;
String zhuangye;
if(p=='A'){
System.out.println("请输入你的名字");
name=input.next();
System.out.println("请输入你的分数");
socre=input.nextInt();
zhuangye=zhuangye1;
Students students=new Students(name,socre);
objects[count]=students;//多态的体现
count++;
flag=true;
}
else if(p=='B') {
System.out.println("请输入你的名字");
name=input.next();
System.out.println("请输入你的分数");
socre=input.nextInt();
zhuangye=zhuangye2;
Students students=new Students(name,socre);
objects[count]=students;
count++;
int flag1 = 0;
if(flag1==1){
System.out.println("成绩录入成功");
}
else {
System.out.println("成绩录入失败");
}
}
return flag;
}
private static Students FindStudents(){
Students students=null;
return students;
}
}
class Students{
String name,zhuangye;
int number;
double Score=0;
String gender,province,city;
private static int number1=00001;//专业1 学号统计
private static int number2=00001;//专业 2
public Students(String name,String gender,String province,String city,String zhuangye){
this.city=city;
this.gender=gender;
this.name=name;
this.province=province;
this.zhuangye=zhuangye;
number=setNumber(zhuangye);
}
public Students(String name2, int socre) {
// TODO Auto-generated constructor stub
}
private static int setNumber(String zhuangye){
int number=0;
if(zhuangye.equals("软件工程")){
number=number1;
number1++;
}
else {
number=number2;
number2++;
}
return number;
}
public void setScore(double score){
this.Score=score;
}
}
实验结果:
实验心得:
1.程序还未完全完成,后面改善