一、上机目的

        1.掌握类的声明,对象的创建以及方法的定义和调用。

        2.掌握包机制。

        3.掌握类的继承。

        4. 掌握多态机制。

        5.掌握抽象类与接口的使用。

二、上机内容

1.(1)设计一个矩形类Rectangle,该类包含2个私有实例变量:矩形的长(length)和宽(width),它还有一个方法,计算长方形的面积getArea()。在Rectangle类中创建构造方法,初始化类中的各个变量;

矩形类:

package OOP.work.Test4.T_1;

public class Rectangle {
    private static int length;
    private static int width;

    //构造无参及有参函数
    public Rectangle() {
    }

    public Rectangle(int lenght, int width) {
        this.length = lenght;
        this.width = width;
    }
    
    //set和get方法
    public int getLenght() {
        return length;
    }

    public void setLenght(int lenght) {
        this.length = lenght;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    //求面积 自定义函数
    public static int getArea(){
        return length * width;
    }
}

(2) 设计一个长方体类Cuboid,该类包含2个私有实例变量:长方体的底面矩形(rectangle)和高(height),它还有一个方法,计算长方体的体积getVolume()。在Cuboid类中创建构造方法,初始化类中的各个变量;

长方体类:

package OOP.work.Test4.T_1;

public class Cuboid extends Rectangle{//extends 表 继承 父类Rectangle
    private static int rectangle;
    private static int height;

    public Cuboid() {
        super();
    }

    public Cuboid(int rectangle, int height) {
        super();
        this.rectangle = rectangle;
        this.height = height;
    }

    public int getRectangle() {
        return rectangle;
    }

    public void setRectangle(int rectangle) {
        this.rectangle = rectangle;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
    public static int getVolume(){
        /*rectangle = Rectangle.getArea();*/
        return rectangle * height;
    }
}

(3)设计一个测试类Test,该类中分别创建类Rectangle和Cuboid的实例,求矩形的面积和长方体的体积。

测试类:

package OOP.work.Test4.T_1;

public class RectangleTest {
    public static void main(String[] args) {
        Rectangle area=new Rectangle(2,4);
        System.out.println("矩形面积是:"+Rectangle.getArea());

        Cuboid he=new Cuboid(Rectangle.getArea(),4);
        System.out.println("长方体的体积是:"+Cuboid.getVolume());
    }
}

2. 编写一个完整的Java Application程序,包含接口Shape,Circle类和Square类实现接口shape,Test类测试Circle和Square,具体要求如下:

   1)接口Shape:包含方法

       double getArea():求一个形状的面积

       double getPerimeter():求一个形状的周长

   2)Circle类:具有以下属性和方法:

属性radius:double类型,表示圆的半径。

方法:Circle(double r):构造函数

            toString(): 输出圆的相关信息(半径等)。

   3)Square类:具有以下属性和方法:

属性length,width:均是double类型,表示长方形的长和宽。

方法:Square(double x,double y):构造函数

toString(): 输出长方形的相关信息(长 宽等)

package OOP.work.Test4.T_2;

//Shape接口

public interface Shape {
    double getArea();//抽象方法 面积()
    double getPerimeter();//抽象方法 周长()
}


//Circle类
class Circle implements Shape{
    private double r;//半径

    public Circle() {
        /*double r;*/
    }

    public Circle(double r) {
        this.r = r;
    }

    @Override
    public String toString() {
        return "圆的半径是:" + r ;
    }

    //自定义函数 面积周长
    public double getArea(){
        System.out.print("圆的面积为:");
        return 3.14*r*r;
    }
    public double getPerimeter(){
        System.out.print("圆的周长为:");
        return 2*3.14*r;
    }
}


//Square类
class Square implements Shape{
    private  double length;
    private double width;

    public Square() {
    }

    public Square(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public String toString() {
        return  "长方形的长为:" + length +";"+
                "长方形的宽为:" + width ;
    }
    public double getArea(){
        System.out.print("Square面积为:");
        return length *width;
    }
    public double getPerimeter(){
        System.out.print("Square周长为:");
        return 2*(length+width);
    }
}

   4)Test类作为主类完成测试功能:

输出圆的周长和面积。

输出长方形的周长和面积

产生随机数决定生成形状的类型,偶数就生成一个Circle对象,奇数就生成一个Square对象,将生成的对象赋值给接口句柄,计算并输出该形状的周长和面积。

测试类Test:

package OOP.work.Test4.T_2;
import java.util.Random;
public class ShapeTest {

    public static void main(String[] args) {
        Circle ra=new Circle(3);
        System.out.println(ra.toString());
        System.out.println(ra.getArea());
        System.out.println(ra.getPerimeter());

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

        Square sq=new Square(2,4);
        System.out.println(sq.toString());
        System.out.println(sq.getArea());
        System.out.println(sq.getPerimeter());

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

        double radius = 4;
        double length = 6;
        double width = 7;
        Random random = new Random();
        if (random.nextInt(100) %2 == 0){
            //多态
            Shape circle= new Circle(radius);
            System.out.println(circle);
        }else {
            Shape square = new Square(length,width);
            System.out.println(square);
        }

    }
}

3. 设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:

(1)编写抽象类Animal()

该类有两个抽象方法cry()和getAnimalName(),即要求各种具体的动物给出自己的叫声和种类名称。

(2)编写模拟器类Simulator

该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用Animal的子类重写cry()方法播放具体动物的声音,调用子类重写getAnimalName()方法显示动物种类的名称。

(3)编写Animal类的子类:Dog和Cat类

(4)编写主类Application(用户程序)

在主类Application的main方法中至少包含如下代码:

       Simulator simulator=new Simulator();

       simulator.playSound(new Dog());

simulator.playSound(new Cat ());

参考代码如下:

(1) Animal.java                 

public abstract class Animal {

    public abstract void cry();

    public abstract String getAnimalName();

}

(2) Simulator.java               

public class Simulator {

   public void playSound(Animal animal) {

       System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");

       animal.cry();

   }

}

(3) Dog.java                  

public class Dog extends Animal {

  //重写方法cry()和getAnimalName(),输出狗的叫声“汪汪…汪汪”和狗的name“狗”。

   ……

}

Cat.java

public class Cat extends Animal {

  //重写方法cry()和getAnimalName(),输出猫的叫声“喵喵...喵喵”和猫的name“猫”。

  ……

}

(4) Application.java               

public class Application{

   public static void main(String args[]) {

      Simulator simulator = new Simulator();

      simulator.playSound(new Dog());

      simulator.playSound(new Cat());

   }

}

package OOP.work.Test4.T_3;

//抽象类Animal()
public  abstract class Animal {
    public  abstract  void cry();
    public abstract String getAnimalName();

    public Animal() {
    }
}


//模拟器类Simulator

class Simulator{
    public void playSound(Animal animal){
        System.out.println("现在播放的是:"+animal.getAnimalName()+"类 的声音");
        animal.cry();
    }
}
//Animal类的子类:Dog
class Dog extends Animal {

    @Override
    public void cry() {
        System.out.println("狗的叫声“汪汪……汪汪”");
    }

    @Override
    public String getAnimalName() {
        return "狗"; //直接返回
    }

}

//子类Cat类
class Cat extends Animal {

    @Override
    public void cry() {
        System.out.println("猫的叫声“喵喵……喵喵”");
    }

    @Override
    public String getAnimalName() {
        return "猫";
    }

}

调用:

package OOP.work.Test4.T_3;

public class Application {
    public static void main(String[] args) {
        Simulator simulator =new Simulator();


       //调用方法一
        Animal animal;
        animal =new Dog();
        simulator.playSound(animal);


        //调用方法二
        /*simulator.playSound(new Cat());*/


        //调用方法三
        Cat C1=new Cat();
        simulator.playSound(C1);


    }
}

4. 将第3题中的抽象类改为接口实现。

package OOP.work.Test4.T_4;

public interface Animal_1 {
    void cry();
    String getAnimalName();
    
}
class Simulator1{

    public static void playSound(Animal_1 animal1){
        System.out.println("现在播放的是:"+animal1.getAnimalName()+"类 的声音");
        animal1.cry();
    }
}

class Dog1 implements Animal_1 {

    @Override
    public void cry() {
        System.out.println("狗的叫声“汪汪……汪汪”");
    }

    @Override
    public String getAnimalName() {
        return "狗"; //直接返回
    }

}


class Cat1 implements Animal_1 {

    @Override
    public void cry() {
        System.out.println("猫的叫声“喵喵……喵喵”");
    }

    @Override
    public String getAnimalName() {
        return "猫";
    }

}

测试类:

package OOP.work.Test4.T_4;

import OOP.work.Test4.T_4.Animal_1;
import OOP.work.Test4.T_4.Dog1;
import OOP.work.Test4.T_4.Simulator1;

public class Animali_Test1 {
    public static void main(String[] args) {
        Simulator1 simulator1 =new Simulator1();

        Animal_1 animal1;
        animal1=new Dog1();
        Simulator1.playSound(animal1);
    }
}