1 下列程序的输出结果为:

class Base {
    Base() {
        int i = 100;
        System.out.println(i);
    }
}

public class Pri extends Base {
    static int i = 200;

    public static void main(String argv[]) {
        Pri p = new Pri();          // 实例化一个对象,执行Base()方法
        System.out.println(i);      // i为static变量,值为200
    }
}

运行结果

100
200


Process finished with exit code 0


2 下列父类中的变量不能够被子类所访问的是:

class Parent {
    private int i = 20;
    protected int j = 30;
    public int k = 40;
    int h = 50;
}

class Child extends Parent {
    void f() {
        }
}

A.i        B.j        C.k        D.h

答案: A 理由:

class Parent {
    private int i = 20;
    protected int j = 30;
    public int k = 40;
    int h = 50;
}

class Child extends Parent {
    void f() {
        //System.out.println(i); // i has private access in "Present"
        System.out.println(j);
        System.out.println(k);
        System.out.println(h); 
    }
}

3 写一个Java应用程序,主要是体现父类子类间的继承关系。父类:鸟,子类:麻雀、鸵鸟、鹰。子类继承父类的一些特点,如都是鸟的话就都会有翅膀、两条腿等,但它们各自又有各自的特点,如麻雀的年龄、体重;鸵鸟的身高、奔跑速度;鹰的捕食、飞翔高度等。

BirdTest.java

class Birds {
    int leg = 2;
    int wings = 2;

    public void fly() {
        System.out.println("鸟儿能够飞翔");
    }

    public void grow() {
        System.out.println("鸟儿都有两只翅膀和两条腿");
    }
}

class Sparrow extends Birds {

    public void printAge(int age) {
        System.out.println("鸟儿的年龄是:" + age);
    }

    public void printHeight(int height) {
        System.out.println("鸟儿飞行的高度是:" + height);
    }
}

class Ostrich extends Birds {
    public void printWight(int weight) {
        System.out.println("鸵鸟的体重是:" + weight);
    }

    public void printSpeed(int speed) {
        System.out.println("鸵鸟的速度是:" + speed);
    }
}

class Eagle extends Birds {
    public static void hunt() {
        System.out.println("鹰会捕食");
    }

    public static void height(int height) {
        System.out.println("鹰飞翔的高度是:" + height);
    }
}

public class BirdTest {
    public static void main(String[] args) {
        Birds birds = new Birds();
        birds.fly();
        birds.grow();

        //父类实例化调用
     System.out.println(); //这是一个换行
     Birds birdsOne = new Sparrow();
        birdsOne.grow();
        birdsOne.fly();
        //子类调用父类的方式
     ((Sparrow) birdsOne).printAge(5);
        ((Sparrow) birdsOne).printHeight(200);

        //子类调用
     System.out.println(); //这是一个换行
     Ostrich ostrich = new Ostrich();
        ostrich.fly();
        ostrich.grow();
        ostrich.printSpeed(60);
        ostrich.printWight(100);

        //直接调用static方法
     System.out.println(); //这是一个换行
     Eagle.hunt();
        Eagle.height(800); 
    }
}

运行结果:

鸟儿能够飞翔
鸟儿都有两只翅膀和两条腿

鸟儿都有两只翅膀和两条腿
鸟儿能够飞翔
鸟儿的年龄是:5
鸟儿飞行的高度是:200

鸟儿能够飞翔
鸟儿都有两只翅膀和两条腿
鸵鸟的速度是:60
鸵鸟的体重是:100

鹰会捕食
鹰飞翔的高度是:800

Process finished with exit code 0        


4 按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

示例代码如下:

Rectangle.java

class Rectangle {
    double chang;
    double kuan;
    Rectangle(double chang,double kuan){
        this.chang = chang;
        this.kuan = kuan;
    }

    double getArea(){
        return chang * kuan;
    }
}

class Rectangular extends Rectangle{
    double gao;

    Rectangular(double chang, double kuan,double gao) {
        super(chang, kuan);
        this.gao = gao;
    }

    double getVolume(){
        return gao * getArea();
    }
}

class RectangleTest{
    public static void main(String []args){
        System.out.println("开始执行");
        Rectangular rectangular = new Rectangular(15,12,5);
        System.out.println("这是体积计算:" + rectangular.getVolume());
        System.out.println("这是面积计算:" + rectangular.getArea());

        System.out.println("另一种实例化方式:");
        System.out.println("这是面积计算:" + new Rectangle(15,12).getArea());
        System.out.println("这是体积计算:" + new Rectangular(15,12,5).getVolume());
    }
}

运行结果:

开始执行
这是体积计算:900.0
这是面积计算:180.0
另一种实例化方式:
这是面积计算:180.0
这是体积计算:900.0

Process finished with exit code 0