更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
10. 定义学生类 Student和大学生类UndergraduateStudent,子类继承父类并添加新属性和方法。

1.Student类有 doHomework() 方法,输出 "Student is doing homework."。

2.UndergraduateStudent 类继承 Student,新增 joinClub(String clubName) 方法; 并重写 doHomework() 方法输出 "UndergraduateStudent is doing math and english homework."。

3.在 main 方法中创建 UndergraduateStudent  对象,调用 doHomework() 和 joinClub("AG")。

class Student {

    public void doHomework() {

        System.out.println("Student is doing homework.");

    }

}    

class UndergraduateStudent extends Student { 

    @Override

    public void doHomework() {

          (代码1)___________                          //调用父类的doHomework() 方法

        System.out.println("UndergraduateStudent is doing math and english homework.");

    }

 

   //新增 joinClub(String clubName) 方法,输出:UndergraduateStudent is joining a club:  ...

     (代码2程序段)                                        

__________________________                                                                              

}    

 

 

public class DesignStudents {

    public static void main(String[] args) {

        UndergraduateStudent undergrad = new UndergraduateStudent();

        undergrad.doHomework(); // 代码3输出结果     _______________                                     

        undergrad.joinClub("AGI");

    }

}

定义宠物接口  Cosset 和猫类 Cat,子类继承父类并添加新属性和方法。

1. Cosset 接口有 age属性和 play() 方法,输出 "Pet is playing"。

2.Cat 类实现  Cosset,新增 color 属性,实现 play() 方法输出 "Cat is chasing a mouse",新增 meow() 方法。

3.Dog 类实现  Cosset,新增 type 属性,实现 play() 方法输出 "Dog is Sleeping",新增 move() 方法。

4.在 main 方法中创建 Cat 对象,调用 play() 和 meow(),并输出属性值; 创建 Dog 对象,调用 play() 和 move()。

 

interface  Cosset {  

    int age=10;  

    public void play();

 }  

class Cat implements  Cosset {  

    private String color;  

    private String name;  

    public Cat(String name, String color) {  

        this.name = name;  

        this.color = color;  

    }  

    //实现 play() 方法输出:Cat is chasing a mouse

     (代码1程序段)                                                                                                                                                                 

                                                                                                          

   public void meow() {  

        System.out.println("Meow! My name is " + name + ", color is " + color);  

    }  

}  

class Dog implements  Cosset {  

    private String type;   //狗的类型

    public Dog(String type) {  

        this.type = type;  

    }  

    @Override  

    public void play() {  

        System.out.println("Dog is Dancing");  

    }  

    //新增move() 方法输出:move! My type is  ...

   (代码2程序段)                                                                                                                                                                 

                                                                                                          

                                                                                                

}  

public class Design {  

    public static void main(String[] args) {  

        Cosset pet = new Cat("Mimi", "White");  

        pet.play();           

        ((Cat)pet).meow();        //代码3输出                                                                                    

        pet = new Dog("bit");  

        pet.play();        //代码4输出                                                                                                     

        ((Dog)pet).move();        // 输出 move! My type is bit

    }  

定义了Demo类如下。在该类中,数据成员名是  (1)____   ,构造方法名是  (2) ___  ,当该类的对象被释放之前有可能被系统自动调用的方法名是  (3)___   。

public class Demo{

   private int data; 

    public Demo(){ 

      data=0;

   }

   public void show(){

       System.out.println("data="+data);

   }

   protected void finalize() throws Throwable{

      System.out.println("data is "+data);

      super.finalize();

   }

   public static void main(String[] arge){

   } 

}

 

代码1的输出结果                                                ___________________                                               

代码2的输出结果                                                _____________________                                               

interface ShowInfos { 

    void displayBrand(String brand); //显示品牌方法 

 

class Phone implements ShowInfos { 

    public void displayBrand(String brand) { 

       System.out.println("手机品牌: " + brand); 

    } 

 

class Car implements ShowInfos { 

    public void displayBrand(String brand) { 

      System.out.println("汽车品牌: " + brand); 

    } 

 

public class Example { 

    public static void main(String args[]) { 

         ShowInfos sm; //声明接口变量 

         sm = new Phone();  //接口变量中存放对象的引用 

         sm.displayBrand("华为"); //代码1的输出

         sm = new Car(); //接口变量中存放对象的引用 

         sm.displayBrand("宝马"); //代码2的输出

     } 

}

定义 ShapeCalc 接口,实现圆形类 Circle 和矩形类 Rectangle

1.ShapeCalc 接口包含 getArea() 和 getPerimeter() 抽象方法。

2.Circle 类实现接口,包含半径属性,计算圆的面积和周长(π取3.14)。

3.Rectangle 类实现接口,包含长和宽属性,计算矩形的面积和周长。

4.在 main 方法中创建对象,输出面积和周长。

 

 

interface ShapeCalc {  

    double getArea();  

    double getPerimeter();  

}  

class Circle implements ShapeCalc {  

    private double radius;  

    public Circle(double radius) {  

        this.radius = radius;  

    }  

    @Override  

    public double getArea() {  //计算圆的面积

                            代码1                                   _______________________________           

    }  

    @Override  

    public double getPerimeter() {  //计算圆的周长

                             代码2                                   _________________________________________           

    }  

}  

class Rectangle implements ShapeCalc {  

    private double length;  

    private double width;  

    public Rectangle(double length, double width) {  

        this.length = length;  

        this.width = width;  

    }  

    @Override  

    public double getArea() {  

        return length * width;  

    }  

    @Override  

    public double getPerimeter() {  

        return 2 * (length + width);  

    }  

}  

public class Design {  

    public static void main(String[] args) {  

        ShapeCalc calc= new Circle(5);          

        System.out.println("Area: " + calc.getArea());   // 代码3输出结果是  _____________                              

        calc = new Rectangle(4, 6);  

        System.out.println("Perimeter: " + calc.getPerimeter());  // 代码4输出结果是   __________________                 

    } 

 }  

1