面向对象的高级特性

Ⅰ.选择题

1、  给定下列代码:(知识点:类的继承B

1. class Super{

2.     public float getNum(){return 3.0f; }

3.}

4.

5.public class Sub extends Super{

6.  

7.}

 当在第6行加入什么方法时会引起编译错误?

2、给定以下代码:(知识点:接口多态)

 

执行后的结果是?

        A. 打印字符串“Tested”         B. 编译失败

        C. 代码运行成功但无输出      D. 运行时抛出异常

 

3MAX_LENGTHintpublic成员变量, 变量值保持为常量100,用简短语句定义这个变量?  )(知识点: final关键字使用)
   A. public int MAX_LENGTH=100;  

B. final int MAX_LENGTH=100;

C. final public int MAX_LENGTH=100;

D. public final int MAX_LENGTH=100.

 

4、给出以下代码,请问该程序的运行结果是什么?(    (知识点:方法的覆盖)

  1class A { static void foo(int i ) {};}

   2.  class B extends A{ void foo( int i ){};}

     A 1行编译错误。

     B 2行编译错误。

     C 代码编译成功。

 

 

5、有关类的说法正确的是()(知识点:类的封装)

A. 类具有封装性,所以类的数据是不能被访问的

B. 类具有封装性,但可以通过类的公共接口访问类中的数据

C.  声明一个类时,必须使用public修饰符

D. 每个类中必须有main方法,否则程序无法运行

 

6、将类的访问权限设置为默认的,则该成员能被()(知识点:访问权限)

A. 同一包中的类访问   B.其他包中的类访问

C.所有的类访问       D.所有的类的子类访问

 

7、假设下面的程序代码都放在MyClass.java文件中,()程序代码能够编译通过。(知识点:Java包的概念)

A.      import java.awt.*;

package mypackage;

calss MyClass{  }

B.      package mypackage;

import java.awt.*;

public class myClass{  }

C.       int m;

package mypackage;

import java.awt.*;

class MyClass{  }

D.      /*This is a comment*/

package mypackage;

import java.awt.*;

public class MyClass{  }

 

8、假设在java源程序文件“MyClass.java”中只含有一个类,而且这个类必须能够被位于一个庞大的软件系统中的所有java类访问到,那么下面()声明有可能是符合要求的类声明。(知识点:访问权限)

A. private class MyClass extends Object

B. public class myclass extends Object

C.  public class MyClass

D. class MyClass extends Object

Ⅱ.程序分析题

1、分析并完善如下程序,并写出最后执行结果: (知识点:类的继承 super关键字使用)

   class Person{

    String name="zhangsan";

    intage=18;

    doubleheight=1.75;

   

    public Person() {

        System.out.println("Person()");

    }

    public Person(String n) {

        System.out.println("Person(String n)");

        name=n;

    }

    public Person(String n,int a,double h) {

        System.out.println("Person(String n,int a,double h)");

        name=n;

        age=a;

        height=h;

    }

   

    publicvoid sayHello() {

        System.out.println("Hello!");

    }

}

class Student4 extends Person{

    doublescore=0;

   

    public Student4() {

                 __[1]__("aa",10);

        System.out.println("Student()");

    }

    public Student4(String n,double s) {

         

        __[2]__(n);

        score=s;

        System.out.println("Student(String n,double s)");

    }

    public Student4(String n,int a,double h,double s) {

        __[3]__(n,s);

        age=a;

        height=h;

        score=s;

        System.out.println("Student(String n,int a,double h,double s)");

    }

    publicvoid study() {

        score++;

    }

    publicvoid sayHello() {

              __[4]__.sayHello();

        System.out.println("Hello, teacher!");

        int i = this.age;

        int j = super.age;

    }

    publicstaticvoid main(String args[]){

          Student4 s4 = new Student4();

          __[5]__.sayHello();

          }

  }

 

2、找出程序中的错误,并说明原因: (知识点:final关键字使用)

public class FinalVar{

    final int y=100;

    final int x1;

    public FinalVar(){

          x1=1;

    }

    public static void main(String[] args) {

        final int z=0;

    }

    public void aa(){

          FinalVar.add(1);

    }

    public static void add(final int x2) {

        x2++;  //final放在常量之前该常量不能被修改

    }

 }

3、找出程序中的错误,并说明原因:(知识点:final关键字使用)

finalclass FinalClass{

    publicvoid add(int x) {

        x++;

    }

}

publicclass SubFinalDemo extends FinalClass {

    publicvoid add(int x) {

        x+=2;

    }

}//final放在类之前该类不能被继承

 

4、找出程序中的错误,并说明原因:(知识点:final关键字使用)

 class FinalClass{

    public final void add(int x)

    {

        x++;

    }

}

public class SubFinalDemo extends FinalClass{

       public void add(int x)  {

        x+=2;

    }

}//final放在方法之前该方法不能被重写

Ⅲ.编程题

1、如下两个类之间存在一定的联系,请用面向对象的特性实现类及其关系:

 

2、(1)定义一个汽车类Vehicle,要求如下:(知识点:类的继承方法的覆盖)

(a)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。

(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。

(c)为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。

(d)定义一个一般方法run(),用打印语句描述汽车奔跑的功能

定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。

(2)定义一个Vehicle类的子类轿车类Car,要求如下:

(a)轿车有自己的属性载人数loader(int 类型)。

(b)提供该类初始化属性的构造方法。

(c)重新定义run(),用打印语句描述轿车奔跑的功能。

(d)定义测试类Test,在其main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。

 

3、设计四个类,分别是:(知识点:抽象类及抽象方法)

1Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor

22个子类:

1Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPergetAreashowAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。

2Circle表示圆类,增加1个属性,radius表示半径,重写getPergetAreashowAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

 3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

 

4、Cola公司的雇员分为以下若干类:(知识点:多态)

(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

Ÿ            方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

(2) SalariedEmployee :     ColaEmployee 的子类,拿固定工资的员工。

Ÿ            属性:月薪

(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。

Ÿ            属性:每小时的工资、每月工作的小时数

(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

Ÿ            属性:月销售额、提成率

(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

 

5、利用接口实现动态的创建对象:(知识点:接口 )

(1)创建4个类

1苹果

2香蕉

3葡萄

4园丁

(2)在三种水果的构造方法中打印一句话.

以苹果类为例

class apple

{

       public apple()

       {

              System.out.println(“创建了一个苹果类的对象”);

}

}

(3)类图如下:

(4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象。

运行结果如图:

 

6Person类,如下图:两个测试类,访问该Person类,要求

1TestMainPerson在相同包中test2

2TestMain在包test1中,Person在包test2

(知识点:Java中包的概念)

 

7、修改原来的Person类,将其进行良好的封装(知识点:封装)

 

 

8、编写三个系别的学生类:英语系,计算机系,文学系(要求通过继承学生类)各系有以下成绩:(知识点:面向对象综合应用、数组中元素为引用类型)

   英语系:  演讲,期末考试,期中考试;

   计算机系:操作能力,英语写作,期中考试,期末考试;

   文学系:  演讲,作品,期末考试,期中考试;

 各系总分评测标准:

    英语系:     演讲 50%

                 期末考试 25%

                 期中考试 25%

    计算机系:  操作能力  40%

                英语写作   20%

                期末考试   20%

                期中考试  20%

    文学系:   演讲   35%

               作品   35%

               期末考试  15%

               期中考试   15%

定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:

学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX

 

答案:

Ⅰ.选择题

1、  给定下列代码:B

 当在第6行加入什么方法时会引起编译错误?

2、给定以下代码:B

 

执行后的结果是?

        A. 打印字符串“Tested”         B. 编译失败

        C. 代码运行成功但无输出      D. 运行时抛出异常

 

3MAX_LENGTHintpublic成员变量, 变量值保持为常量100,用简短语句定义这个变量?CD  )
   A. public int MAX_LENGTH=100;  

B. final int MAX_LENGTH=100;

C. final public int MAX_LENGTH=100;

D. public final int MAX_LENGTH=100.

 

4、给出以下代码,请问该程序的运行结果是什么?(  B 

  1class A { static void foo(int i ) {};}

   2.  class B extends A{ void foo( int i ){};}

     A 1行编译错误。

     B 2行编译错误。

     C 代码编译成功。

//静态方法不能被重写

 

4、有关类的说法正确的是(B

A. 类具有封装性,所以类的数据是不能被访问的

B. 类具有封装性,但可以通过类的公共接口访问类中的数据

C.  声明一个类时,必须使用public修饰符

D. 每个类中必须有main方法,否则程序无法运行

 

5、将类的访问权限设置为默认的,则该成员能被(A

A. 同一包中的类访问   B.其他包中的类访问

C.所有的类访问       D.所有的类的子类访问

 

6、假设下面的程序代码都放在MyClass.java文件中,(D )程序代码能够编译通过。

A.      import java.awt.*;

package mypackage;

calss MyClass{  }

B.      package mypackage;

import java.awt.*;

public class myClass{  }

C.       int m;

package mypackage;

import java.awt.*;

class MyClass{  }

D.      /*This is a comment*/

package mypackage;

import java.awt.*;

public class MyClass{  }

 

7、假设在java源程序文件“MyClass.java”中只含有一个类,而且这个类必须能够被位于一个庞大的软件系统中的所有java类访问到,那么下面(C )声明有可能是符合要求的类声明。

A. private class MyClass extends Object

B. public class myclass extends Object

C.  public class MyClass

D. class MyClass extends Object

Ⅱ.程序分析题

1、分析并完善如下程序,并写出最后执行结果:

    class Person{

    String name="zhangsan";

    intage=18;

    doubleheight=1.75;

   

    public Person() {

        System.out.println("Person()");

    }

    public Person(String n) {

        System.out.println("Person(String n)");

        name=n;

    }

    public Person(String n,int a,double h) {

        System.out.println("Person(String n,int a,double h)");

        name=n;

        age=a;

        height=h;

    }

   

    publicvoid sayHello() {

        System.out.println("Hello!");

    }

}

class Student4 extends Person{

    doublescore=0;

   

    public Student4() {

                 __this__("aa",10);

        System.out.println("Student()");

    }

    public Student4(String n,double s) {

         

        __super__(n);

        score=s;

        System.out.println("Student(String n,double s)");

    }

    public Student4(String n,int a,double h,double s) {

        __this__(n,s);

        age=a;

        height=h;

        score=s;

        System.out.println("Student(String n,int a,double h,double s)");

    }

    publicvoid study() {

        score++;

    }

    publicvoid sayHello() {

              __this/super__.sayHello();

        System.out.println("Hello, teacher!");

        int i = this.age;

        int j = super.age;

    }

    publicstaticvoid main(String args[]){

          Student4 s4 = new Student4();

          __s4__.sayHello();

          }

  }

2、找出程序中的错误,并说明原因:

public class FinalVar{

    final int y=100;

    final int x1;

    public FinalVar(){

          x1=1;

    }

    public static void main(String[] args) {

        final int z=0;

    }

    public void aa(){

          FinalVar.add(1);

    }

    public static void add(final int x2) {

        x2++;    //错了 final不许修改

    }

 }

3、找出程序中的错误,并说明原因:

finalclass FinalClass{

    publicvoid add(int x) {

        x++;

    }

}

publicclass SubFinalDemo extends FinalClass {//不能被继承

    publicvoid add(int x) {

        x+=2;

    }

}

 

4、找出程序中的错误,并说明原因:

 class FinalClass{

    public final void add(int x)

    {

        x++;

    }

}

public class SubFinalDemo extends FinalClass{

       public void add(int x)  {//不能在子类中被覆盖,即不能修改。

        x+=2;

    }

}

Ⅲ.编程题

1、如下两个类之间存在一定的联系,请用面向对象的特性实现类及其关系:

publicclass Person {

    String name;

    intage;

    doubleheight;

    Person(){

      

    }

    publicvoid sayHello(){

      

    }

}

 

publicclass Student extends Person{

    doublescore;

   

    publicvoid study(){

      

    }

   

}

2、(1)定义一个汽车类Vehicle,要求如下:

(a)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。

(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。

(c)为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。

(d)定义一个一般方法run(),用打印语句描述汽车奔跑的功能

定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。

publicclass Vehicle {

    private String brand;

    private String color;

    privatedoublespeed;

    Vehicle(){

      

    }

    Vehicle(String brand,String color){

       this.brand = brand;

       this.color = color;

       speed = 0;

    }

    public String getColor() {

       returncolor;

    }

    publicvoid setColor(String color) {

       this.color = color;

    }

    publicdouble getSpeed() {

       returnspeed;

    }

    publicvoid setSpeed(double speed) {

       this.speed = speed;

    }

   

    publicvoid run(){

       System.out.println(getColor()+""+getBrand()+"的速度是"+getSpeed());

    }

    public String getBrand() {

       returnbrand;

    }

}

(2)定义一个Vehicle类的子类轿车类Car,要求如下:

(a)轿车有自己的属性载人数loader(int 类型)。

(b)提供该类初始化属性的构造方法。

(c)重新定义run(),用打印语句描述轿车奔跑的功能。

(d)定义测试类Test,在其main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。

publicclass Car extends Vehicle {

    intloader;

    Car(){

      

    }

    Car(String brand,String color,int loader){

       super(brand, color);

       this.loader = loader;

    }

   

    publicvoid run(){

       System.out.println(getColor()+"的载人数"+loader+getBrand()+"的速度是"+getSpeed());

    }

}

publicstaticvoid main(String[] args) {

       // TODO Auto-generated method stub

       Car car =new Car("Honda","red",2);

       car.run();

    }

 

3、设计四个类,分别是:

1Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor

publicabstractclass Shape {

    doublearea;

    doubleper;

    charcolor;

    Shape(){

      

    }

    Shape(char color){

       this.color = color;

    }

   

    publicabstractdouble getArea();

    publicabstractdouble getPer();

    publicabstractvoid showAll();

   

    publicchar getColor(){

       returncolor;

    }

}

22个子类:

1Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPergetAreashowAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。

publicclass Rectangle extends Shape {

    doublewidth;

    doubleheight;

   

    Rectangle(){

      

    }

   

    Rectangle(double width,  double height,char color){

       super(color);

       this.width = width;

       this.height = height;

    }

    @Override

    publicdouble getArea() {

       area = width*height;

       returnarea;

    }

 

    @Override

    publicdouble getPer() {

       per = 2*(width+height);

       returnper;

    }

 

    @Override

    publicvoid showAll() {

       System.out.println("长:"+width);

       System.out.println("宽:"+height);

       System.out.println("面积:"+getArea());

       System.out.println("周长:"+getPer());

       System.out.println("颜色:"+getColor());

    }

 

}

 

2Circle表示圆类,增加1个属性,radius表示半径,重写getPergetAreashowAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

publicclass Circle extends Shape {

    finaldoublepi = 3.14;

    doubleradius;

    Circle(){

      

    }

    Circle(double radius,char color){

       super(color);

       this.radius = radius;

    }

    @Override

    publicdouble getArea() {

       area = pi*radius*radius;

       returnarea;

    }

 

    @Override

    publicdouble getPer() {

       per = 2*pi*radius;

       returnper;

    }

 

    @Override

    publicvoid showAll() {

       System.out.println("半径:"+radius);

       System.out.println("面积:"+getArea());

       System.out.println("周长:"+getPer());

       System.out.println("颜色:"+getColor());

 

    }

 

}

 

 3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

publicclass PolyDemo {

    publicstaticvoid main(String[] args) {

       Rectangle r = new Rectangle(1,2,'');

       Circle c = new Circle(1.2,'');      

       r.showAll();

       System.out.println("-----------------");

       c.showAll();

       System.out.println("-----------------");

    }

}

 

4、Cola公司的雇员分为以下若干类:

(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

Ÿ            方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

(2) SalariedEmployee :     ColaEmployee 的子类,拿固定工资的员工。

Ÿ            属性:月薪

(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。

Ÿ            属性:每小时的工资、每月工作的小时数

(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

Ÿ            属性:月销售额、提成率

(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

publicabstractclass ColaEmployee {

    String name;

    intyear;

    intmonth;

    intday;

   

    ColaEmployee(){

      

    }

    ColaEmployee(String name,int year,int month,int day){

       this.name = name;

       this.day = day;

       this.month = month;

       this.year = year;

    }

    abstractdouble getSalary(int month);

}

 

publicclass SalariedEmployee extends ColaEmployee {

    doublesalary;

   

    SalariedEmployee(){

      

    }

   

    SalariedEmployee(String name,int year,int month,int day,double salary){

       super(name, year, month, day);

       this.salary =salary;

    }

    @Override

    double getSalary(int month) {

      

       if(month == this.month){

           salary +=100;

       }

       returnsalary;

    }

 

}

publicclass SalesEmployee extends ColaEmployee{

    doublemonthSalary;

    doublerate;

   

    SalesEmployee(){

      

    }

   

publicclass HourlyEmployee extends ColaEmployee{

    doublehourSalary;

    inthour;

   

    HourlyEmployee(){

      

    }

   

    HourlyEmployee(String name,int year,int month,int day,double hourSalary,int hour){

       super(name, year, month, day);

       this.hourSalary =hourSalary;

       this.hour = hour;

    }

    @Override

    double getSalary(int month) {

       double salary = 0;

       if(hour<=160){

           salary = hour * hourSalary;

       }else{

           salary = 160 * hourSalary + (hour - 160 )*hourSalary*1.5;

       }

      

       if(month == this.month){

           salary +=100;

       }

       return salary;

    }

 

}

 

    SalesEmployee(String name,int year,int month,int day,double monthSalary,double rate){

       super(name, year, month, day);

       this.monthSalary =monthSalary;

       this.rate = rate;

    }

    @Override

    double getSalary(int month) {

       double salary=monthSalary * rate;

       if(month == this.month){

           salary +=100;

       }

       return salary;

    }

}

 

public  class Company {

 

    staticvoid getSalary(int month,ColaEmployee c){

       System.out.println(month+""+c.name+"的工资:"+c.getSalary(month));

    }

 

}

 

publicclass TestCompany {

 

    /**

     *@paramargs

     */

    publicstaticvoid main(String[] args) {

       // TODO Auto-generated method stub

       ColaEmployee[] a = new ColaEmployee[3];

       a[0]=new SalariedEmployee("大川",1988,2,29,3500);

       a[1]=new HourlyEmployee("海鸥",1988,2,29,20,200);

       a[2]=new SalesEmployee("abc",1988,5,10,30000,0.1);

       for(ColaEmployee c:a){

           Company.getSalary(2, c);

       }

    }

 

}

5、利用接口实现动态的创建对象:

(1)创建4个类

1苹果

2香蕉

3葡萄

4园丁

(2)在三种水果的构造方法中打印一句话.

以苹果类为例

class apple

{

       public apple()

       {

              System.out.println(“创建了一个苹果类的对象”);

}

}

(3)类图如下:

(4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象。

运行结果如图:

 

 

publicabstractinterface Fruit {

   

}

publicclass Apple implements Fruit{

    Apple(){

       System.out.println("创建了一个苹果类的对象");

    }

}

publicclass Banana implements Fruit{

    Banana(){

       System.out.println("创建了一个香蕉类的对象");

    }

}

publicclass Putao implements Fruit{

    Putao(){

       System.out.println("创建了一个葡萄类的对象");

    }

}

publicclass  Gardener {

    public Fruit creat(){

       Fruit f =null;

       Scanner input =new Scanner(System.in);

       String name = input.next();

       if(name.equals("苹果")){

            f = new Apple();

       }elseif(name.equals("香蕉")){

            f = new Banana();

       }elseif(name.equals("葡萄")){

            f =new Putao();

       }else{

           System.out.println("不会种");

       }

       return f;

    }

}

 

6Person类,如下图:两个测试类,访问该Person类,要求

1TestMainPerson在相同包中test2

2TestMain在包test1中,Person在包test2

(知识点:Java中包的概念)

 

7、修改原来的Person类,将其进行良好的封装

package com.neusot.test2;

 

publicclass Person {

    private String name;

    privateintage;

    privatedoubleheight;

   

    publicvoid person(){

      

    }

    publicvoid sayHello(){

      

    }

    public String getName() {

       returnname;

    }

    publicvoid setName(String name) {

       this.name = name;

    }

    publicint getAge() {

       returnage;

    }

    publicvoid setAge(int age) {

       this.age = age;

    }

    publicdouble getHeight() {

       returnheight;

    }

    publicvoid setHeight(double height) {

       this.height = height;

    }

}

 

 

 

 

8、编写三个系别的学生类:英语系,计算机系,文学系(要求通过继承学生类)各系有以下成绩:

   英语系:  演讲,期末考试,期中考试;

   计算机系:操作能力,英语写作,期中考试,期末考试;

   文学系:  演讲,作品,期末考试,期中考试;

 各系总分评测标准:

    英语系:     演讲 50%

                 期末考试 25%

                 期中考试 25%

    计算机系:  操作能力  40%

                英语写作   20%

                期末考试   20%

                期中考试  20%

    文学系:   演讲   35%

               作品   35%

               期末考试  15%

               期中考试   15%

定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:

学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX

publicabstractclass Student {

    String name;

    String id;

    String sex;

    intage;

    doublelastScore;    // 期末成绩

    doubleminScore//期中成绩

   

    Student(String name,String id,String sex,int age,double lastScore,double minScore){

       this.name = name;

       this.age =age;

       this.id = id;

       this.sex = sex;

       this.lastScore = lastScore;

       this.minScore = minScore;

    }

   

    Student(){

      

    }

    publicabstractdouble getScore();     //获取最终成绩

    publicvoid show(){

       System.out.println("学号:"+id+"姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 综合成绩:"+getScore());

    }

   

}

publicclass English extends Student{

   

    doublespeekScore;

   

    English(){

      

    }

   

    English(String name,String id,String sex,int age,double lastScore,double minScore,double speekScore){

       super(name, id, sex, age, lastScore, minScore);

       this.speekScore = speekScore;

    }

    publicdouble getScore(){

       returnlastScore*0.25+minScore*0.25+speekScore*0.5;

    }

   

   

}

publicclass Computer extends Student{

   

    double makeScore; //操作成绩

    double engScore;  //英语写作成绩

   

    Computer(){

      

    }

   

    Computer(String name,String id,String sex,int age,double lastScore,double minScore,double makeScore,double engScore){

       super(name, id, sex, age, lastScore, minScore);

       this.makeScore = makeScore;

       this.engScore = engScore;

    }

    @Override

    publicdouble getScore() {

       // TODO Auto-generated method stub

       return lastScore*0.2+minScore*0.2+engScore*0.2+makeScore*0.4;

    }

 

}

publicclass Literature extends Student{

    doublespeekScore;

    doublecomposition;

 

    Literature(){

      

    }

   

    Literature(String name,String id,String sex,int age,double lastScore,double minScore,double speekScore,double composition){

       super(name, id, sex, age, lastScore, minScore);

       this.composition =composition;

       this.speekScore = speekScore;

    }

    @Override

    publicdouble getScore() {

       // TODO Auto-generated method stub

       returnlastScore*0.15+minScore*0.15+speekScore*0.35+composition*0.35;

    }

   

}

publicclass Test {

 

    /**

     *@paramargs

     */

    publicstaticvoid main(String[] args) {

       // TODO Auto-generated method stub

      

       String sex;

       Student[] a = new Student[5];

       for(int i=0;i<a.length;i++){

           sex ="";

           if(Math.random()<0.5){

              sex = "";

           }

           if(Math.random()*3 >2){

             

              a[i] = new English((int)(Math.random()*1000)+"",(int)(Math.random()*1000000)+"",sex,(int)((Math.random()+0.1)*100),(Math.random()+1)*100,(Math.random()+1)*100,(Math.random()+1)*100);

          

           }elseif(Math.random()*3 >1){

              a[i] = new Computer((int)(Math.random()*1000)+"",(int)(Math.random()*1000000)+"",sex,(int)((Math.random()+0.1)*100),(Math.random()+1)*100,(Math.random()+1)*100,(Math.random()+1)*100,(Math.random()+1)*100);

                 

           }else{

             

              a[i] = new Literature((int)(Math.random()*1000)+"",(int)(Math.random()*1000000)+"",sex,(int)((Math.random()+0.1)*100),(Math.random()+1)*100,(Math.random()+1)*100,(Math.random()+1)*100,(Math.random()+1)*100);

           }

       }

       for(Student s:a){

           s.show();

       }

    }

}

 

猜你喜欢

转载自blog.csdn.net/jiangjiewudi/article/details/9632071