抽象类与接口

抽象类与多态练习

【问题描述1】

1)定义一个抽象基类Shape,包含一个受保护的字符串类型变量name,保存形状的名称;包含两个抽象方法getArea()和getLength(),前一个方法用以返回形状的面积,后一个方法用以返回形状的周长。包含一个单参构造函数,用形参为name字段赋值,并显示“名称:”+name字段的值。

2)从Shape类派生出Rectangle和Circle类,这两个类都按照对应形状的规则,各自实现了从抽象基类Shape中继承得到的抽象方法getArea()和getLength()。 Circle类中还定义了PI常量(圆周率,用3.14为值)以及radius成员变量(保存半径),Circle类中有一个两参构造函数,第一个参数为字符串类型(说明当前的形状),第二个参数为圆形的半径值参数,构造函数中用第一个参数来调用其父类的构造函数,并为radius字段赋值。Rectangle类中定义了height和width分别保存长和宽的数据,Rectangle类中有一个三参构造函数,第一个参数为字符串类型(说明当前的形状),第二个参数为矩形的宽的参数,第三个参数为矩形的高的参数,构造函数中用第一个参数来调用其父类的构造函数,并为width字段、height字段赋值。

3)主类中声明抽象基类Shape类型的应用变量,分别让其引用实例化一个半径为10.2的圆形和一个长宽分别为6.5、10.3的长方形,并调用相应函数,返回这两个形状的面积和周长数据显示在屏幕上。
【输入形式】


【输出形式】
【样例输入】


【样例输出】

Name:Rectangle;Area=66.95;Length=33.6

Name:Circle;Area=326.68559999999997;Length=64.056

public abstract class Shape {
    protected String name;
    public abstract void getArea();
    public abstract void getLength();
    
    public Shape(String name){
        this.name=name;
        System.out.print("Name:"+name);
    }
    

}

public class Rectangle extends Shape {
    public double height;
    public double width;

    public Rectangle(String name,double width,double height) {
        super(name);
        this.width=width;
        this.height=height;
    }

    public void getArea() {
        System.out.print(";Area="+width*height);
    }

    public void getLength() {
        System.out.println(";Length="+(width+height)*2);
    }
    
}


public class Circle extends Shape{
    public double pi=3.14;
    public double radius;

    public Circle(String name,double radius) {
        super(name);
        this.radius=radius; 
    }

    public void getArea() {
        System.out.print(";Area="+pi*radius*radius);    
    }

    public void getLength() {
        System.out.print(";Length="+2*pi*radius);
    }

}


public class Test {

    public static void main(String[] args) {
        Shape shape;
        shape=new Rectangle("Rectangle",6.5,10.3);
        shape.getArea();
        shape.getLength();
        
        shape=new Circle("Circle",10.2);
        shape.getArea();
        shape.getLength();

    }

}

接口的声明与实现

【问题描述】

定义一个接口Shape,接口中定义抽象方法面积area()和体积volume(),定义一个长方体类Cuboid实现接口Shape, 定义另一个圆柱体类Cylinder实现接口Shape.

在Cuboid类中新定义长、宽、高, 通过创建对象时传递进来的参数在构造方法中对变量赋值,并用area和volume求底面积和体积

在Cylinder类中新定义半径、高, 通过创建对象时传递进来的参数在构造方法中对半径和高赋值,并用area和volume求底面积和体积

在主类中,定义接口类型的引用变量,将创建的子类对象赋给接口类型的引用变量实现多态,创建Cuboid对象(含三个参数长10,宽6,高5),调用方法area和volume输出长方体的底面积和体积;创建Cylinder对象(含两个参数半径5、高10),调用方法area和volume输出圆柱体的底面积和体积

【输入形式】


【输出形式】

控制台输出

【样例输入】


【样例输出】

The area of this cuboid is 60.0

The volume of this cuboid is 300.0

The area of this cylinder is 78.5

The volume of this cylinder is 785.0

public interface  Shape {
   public void area();
   public void volume();

}

public class Cuboid implements Shape {
    private double height;
    private double length;
    private double width;
    
    public Cuboid(double height,double length,double width){
        this.height=height;
        this.length=length;
        this.width=width;
    }
    @Override
    public void area() {
        System.out.println("The area of this cuboid is "+length*width);
    }

    @Override
    public void volume() {
        System.out.println("The volume of this cuboid is "+height*length*width);
    }

}


public class Cylinder implements  Shape{
    private double height;
    private double radius;
    
    
    public Cylinder(double height,double radius) {
        // TODO Auto-generated constructor stub
        this.height=height;
        this.radius=radius;
    }
    
    @Override
    public void area() {
        System.out.println("The area of this cylinder is "+3.14*radius*radius);
    }

    @Override
    public void volume() {  
        System.out.println("The volume of this cylinder is "+3.14*radius*radius*height);
    }

}


public class Test {

    public static void main(String[] args) {
        
        Shape shape;
        shape=new Cuboid(5, 10, 6);
        shape.area();
        shape.volume();
        
        shape=new Cylinder(10, 5);
        shape.area();
        shape.volume();
        

    }

}

定义一个接口实现两个对象的比较

【问题描述】

定义如下接口CompareObject用来实现两个对象的比较

interface CompareObject

{

** public int compareTo(Object o); **

** //若相等,返回值是 0 ;当前对象大则返回1;当前对象小,返回-1 。**

** }**

  • 定义一个Circle类,包含属性有半径,方法有面积计算方法;

  • **定义一个ComparableCircle类,继承Circle类并且实现CompareObject接口。在ComparableCircle类中给出接口中方法compareTo的实现体,用来比较两个圆的半径大小 **//若相等,返回值是 0 ;当前对象大则返回1;当前对象小,返回-1 。****

  • 定义矩形类Rectangle,包含的属性有长、宽,方法有面积计算;

  • 定义ComparableRectangle类,在ComparableRectangle类中给出compareTo方法的实现,规则是根据两个矩形的面积大小判断矩形的大小。 **//若相等,返回值是 0 ;当前对象大则返回1;当前对象小,返回-1 。******

  • 定义一个测试类TestInterface,创建两个半径都为3.0的ComaparableCircle对象,调用compareTo方法比较两个圆的大小。创建两个厂和款分别为6.0和5.0的ComparableRectangle对象,调用compareTo方法比较两个矩形的大小。
    【样例输入】
    没有输入
    【样例输出】

0

0

interface CompareObject {
    public int compareTo(Object o);

}


public class Circle {
    public double radius;
    
    public Circle(double radius){
        this.radius=radius;
    }

    public double getRadius() {
        return radius;
    }


    public void setRadius(double radius) {
        this.radius = radius;
    }


    public double getArea(){
        return 3.14*radius*radius;
    }

}


class ComparableCircle extends Circle implements CompareObject{
    
    public ComparableCircle(double radius) {
        super(radius);
    }
    @Override
    public int compareTo(Object o) {
        if(this.getRadius()>((Circle) o).getRadius())
            return 1;
        else if(getRadius()<((Circle) o).getRadius())
            return -1;
        else
            return 0;
    }

}

public class Rectangle {
    public double length;
    public double  weigth;
    
    public Rectangle(double length,double  weigth){
        this.length=length;
        this.length=length;
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public double getWeigth() {
        return weigth;
    }
    public void setWeigth(double weigth) {
        this.weigth = weigth;
    }
    
    public double getArea(){
        return length*weigth;
    }
    

}


class ComparableRectangle extends Rectangle implements CompareObject{
 
    public ComparableRectangle(double length, double weigth) {
        super(length,weigth);
        
    }

    @Override
    public int compareTo(Object o) {
//      if(this==o) return 0;
//      else if(o instanceof ComparableCircle){
            if(getArea()>((Rectangle) o).getArea())
                return 1;
            else if(getArea()<((Rectangle) o).getArea())
                return -1;
            else
                return 0;
//      }
        //else return -1;
        
    }
}


public abstract class TestInterface {

    public static void main(String[] args) {
        ComparableCircle c1 = new ComparableCircle(3.0);
        ComparableCircle c2 = new ComparableCircle(3.0);
        System.out.println(c1.compareTo(c2));
        ComparableRectangle r1 = new ComparableRectangle(6.0, 5.0);
        ComparableRectangle r2 = new ComparableRectangle(6.0, 5.0);
        System.out.println(r1.compareTo(r2));

    }

}

你可能感兴趣的:(抽象类与接口)