实验7:类的多态

一、实验代码如下:

  1 package 实验7;
  2 
  3 import java.util.Scanner;
  4 
  5 public class Student {
  6 public int i=0,id=1,nj,x;
  7 public static int count=1,count1=1;
  8 public static double wl,wlw,yw,sx,yy,yw1,sx1,yy1;
  9 public String xing,name,zy,address;
 10 public double date,avg,chinese,math,english;
 11 static Scanner shuru=new Scanner(System.in);
 12 public Student(String xing,String name,String zy,int nj,int id,String address,double date,double chinese,double math,double english)
 13 {
 14 this.xing=xing;
 15 this.name=name;
 16 this.zy=zy;
 17 this.nj=nj;
 18 this.x=this.nj*100;
 19 this.id=id;
 20 if(this.zy.equals("网络工程")){
 21 this.id=this.x+count;
 22 }
 23 
 24 if(this.zy.equals("生物技术"))
 25 this.id=this.x+count1;
 26 
 27 
 28 this.address=address;
 29 this.date=date;
 30 this.chinese=chinese;
 31 this.math=math;
 32 this.english=english;
 33 
 34 }
 35 
 36 public void name_chazhao(String temp) {
 37 if(this.xing.equals(temp)) {
 38 System.out.println(name+" "+zy+" "+nj+" "+id+" "+address+" "+date+" "+" "+chinese+" "+math+" "+english);
 39 }
 40 }
 41 public void date_chazhao(double temp) {
 42 for(i=0;i<3;i++)
 43 if(this.date==temp) {
 44 System.out.println(name+" "+zy+" "+nj+" "+id+" "+address+" "+date+" "+chinese+" "+math+" "+english);
 45 break;
 46 }
 47 }
 48 public void address_chazhao(String temp) {
 49 if(address.equals(temp)) {
 50 System.out.println(name+" "+zy+" "+nj+" "+" "+id+" "+address+" "+date+" "+chinese+" "+math+" "+english);
 51 }
 52 }
 53 public void tongjichengji() {
 54 if(this.zy.equals("网络工程")) {
 55 this.yw=this.yw+chinese;
 56 this.sx=this.sx+math;
 57 this.yy=this.yy+english;
 58 }
 59 if(this.zy.equals("生物技术")) {
 60 this.yw1=this.yw1+chinese;
 61 this.sx1=this.sx1+math;
 62 this.yy1=this.yy1+english;
 63 }
 64 }
 65 public static void main(String[] args) {
 66 Student[] stu=new Student[3];
 67 stu[0]=new Student("张","张灿","网络工程",2017,1111,"山东枣庄",1998,100,90,80);
 68 stu[1]=new Student("李","李会通","网络工程",2017,2222,"海南万宁",1999,100,95,90);
 69 stu[2]=new Student("马","马万虎","网络工程",2017,3333,"青海格尔木",1999,100,98,96);
 70 String temp;
 71 double year;
 72 Scanner shuru=new Scanner(System.in);
 73 int j=1;
 74 while(j!=0) {
 75 System.out.println("1 姓查找 2 出生年查找 3 地址查找 0 退出");
 76 j=shuru.nextInt();
 77 switch(j) {
 78 case 1:    
 79 System.out.println("请输入查找的姓:");
 80 temp=shuru.next();
 81 for(int i=0;i<3;i++) {
 82 stu[i].name_chazhao(temp);
 83 };break;
 84 case 2:System.out.println("请输入查找的年份:");
 85 year=shuru.nextDouble();
 86 for(int i=0;i<3;i++) {
 87 stu[i].date_chazhao(year);
 88 };break;
 89 case 3:System.out.println("请输入查找的地址:");
 90 temp=shuru.next();
 91 for(int i=0;i<3;i++) {
 92 stu[i].address_chazhao(temp);
 93 };break;
 94 }
 95 }
 96 System.out.println("查询专业的成绩:");
 97 temp=shuru.next();
 98 for(int i=0;i<3;i++) {
 99 if(stu[i].zy.equals("网络工程"))
100 stu[i].tongjichengji();
101 if(stu[i].zy.equals("生物技术"))
102 stu[i].tongjichengji();
103 }
104 if(temp.equals("网络工程"))
105 System.out.println("语文分数为:"+yw+"数学分数为:"+sx+"英语分数为"+yy);
106 if(temp.equals("生物技术"))
107 System.out.println("语文分数为:"+yw1+"数学分数为:"+sx1+"英语分数为"+yy1);    
108 }    
109 }

二、实验结果:

1 姓查找 2 出生年查找 3 地址查找 0 退出
2
请输入查找的年份:
1999
李会通 网络工程 2017 201701 海南万宁 1999.0 100.0 95.0 90.0
马万虎 网络工程 2017 201701 青海格尔木 1999.0 100.0 98.0 96.0
1 姓查找 2 出生年查找 3 地址查找 0 退出
1
请输入查找的姓:

张灿 网络工程 2017 201701 山东枣庄 1998.0 100.0 90.0 80.0
1 姓查找 2 出生年查找 3 地址查找 0 退出
3
请输入查找的地址:
海南万宁
李会通 网络工程 2017 201701 海南万宁 1999.0 100.0 95.0 90.0
1 姓查找 2 出生年查找 3 地址查找 0 退出
0
查询专业的成绩:
生物技术
语文分数为:0.0数学分数为:0.0英语分数为0.0

三、实验心得:

在定义数据类型时一定不要加上static,不然后面的对象数据会覆盖之前的数据

 

转载于:https://www.cnblogs.com/itsRes/p/10822346.html

你可能感兴趣的:(实验7:类的多态)