抽象類與接口

抽象類與多態(tài)練習(xí)

【問(wèn)題描述1】

1)定義一個(gè)抽象基類Shape,包含一個(gè)受保護(hù)的字符串類型變量name,保存形狀的名稱;包含兩個(gè)抽象方法getArea()和getLength(),前一個(gè)方法用以返回形狀的面積,后一個(gè)方法用以返回形狀的周長(zhǎng)。包含一個(gè)單參構(gòu)造函數(shù),用形參為name字段賦值,并顯示“名稱:”+name字段的值。

2)從Shape類派生出Rectangle和Circle類,這兩個(gè)類都按照對(duì)應(yīng)形狀的規(guī)則,各自實(shí)現(xiàn)了從抽象基類Shape中繼承得到的抽象方法getArea()和getLength()。 Circle類中還定義了PI常量(圓周率,用3.14為值)以及radius成員變量(保存半徑),Circle類中有一個(gè)兩參構(gòu)造函數(shù),第一個(gè)參數(shù)為字符串類型(說(shuō)明當(dāng)前的形狀),第二個(gè)參數(shù)為圓形的半徑值參數(shù),構(gòu)造函數(shù)中用第一個(gè)參數(shù)來(lái)調(diào)用其父類的構(gòu)造函數(shù),并為radius字段賦值。Rectangle類中定義了height和width分別保存長(zhǎng)和寬的數(shù)據(jù),Rectangle類中有一個(gè)三參構(gòu)造函數(shù),第一個(gè)參數(shù)為字符串類型(說(shuō)明當(dāng)前的形狀),第二個(gè)參數(shù)為矩形的寬的參數(shù),第三個(gè)參數(shù)為矩形的高的參數(shù),構(gòu)造函數(shù)中用第一個(gè)參數(shù)來(lái)調(diào)用其父類的構(gòu)造函數(shù),并為width字段、height字段賦值。

3)主類中聲明抽象基類Shape類型的應(yīng)用變量,分別讓其引用實(shí)例化一個(gè)半徑為10.2的圓形和一個(gè)長(zhǎng)寬分別為6.5、10.3的長(zhǎng)方形,并調(diào)用相應(yīng)函數(shù),返回這兩個(gè)形狀的面積和周長(zhǎng)數(shù)據(jù)顯示在屏幕上。
【輸入形式】

無(wú)
【輸出形式】
【樣例輸入】

無(wú)
【樣例輸出】

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();

    }

}

接口的聲明與實(shí)現(xiàn)

【問(wèn)題描述】

定義一個(gè)接口Shape,接口中定義抽象方法面積area()和體積volume(),定義一個(gè)長(zhǎng)方體類Cuboid實(shí)現(xiàn)接口Shape, 定義另一個(gè)圓柱體類Cylinder實(shí)現(xiàn)接口Shape.

在Cuboid類中新定義長(zhǎng)、寬、高, 通過(guò)創(chuàng)建對(duì)象時(shí)傳遞進(jìn)來(lái)的參數(shù)在構(gòu)造方法中對(duì)變量賦值,并用area和volume求底面積和體積

在Cylinder類中新定義半徑、高, 通過(guò)創(chuàng)建對(duì)象時(shí)傳遞進(jìn)來(lái)的參數(shù)在構(gòu)造方法中對(duì)半徑和高賦值,并用area和volume求底面積和體積

在主類中,定義接口類型的引用變量,將創(chuàng)建的子類對(duì)象賦給接口類型的引用變量實(shí)現(xiàn)多態(tài),創(chuàng)建Cuboid對(duì)象(含三個(gè)參數(shù)長(zhǎng)10,寬6,高5),調(diào)用方法area和volume輸出長(zhǎng)方體的底面積和體積;創(chuàng)建Cylinder對(duì)象(含兩個(gè)參數(shù)半徑5、高10),調(diào)用方法area和volume輸出圓柱體的底面積和體積

【輸入形式】

無(wú)
【輸出形式】

控制臺(tái)輸出

【樣例輸入】

無(wú)
【樣例輸出】

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();
        

    }

}

定義一個(gè)接口實(shí)現(xiàn)兩個(gè)對(duì)象的比較

【問(wèn)題描述】

定義如下接口CompareObject用來(lái)實(shí)現(xiàn)兩個(gè)對(duì)象的比較

interface CompareObject

{

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

** //若相等,返回值是 0 ;當(dāng)前對(duì)象大則返回1;當(dāng)前對(duì)象小,返回-1 。**

** }**

  • 定義一個(gè)Circle類,包含屬性有半徑,方法有面積計(jì)算方法;

  • **定義一個(gè)ComparableCircle類,繼承Circle類并且實(shí)現(xiàn)CompareObject接口。在ComparableCircle類中給出接口中方法compareTo的實(shí)現(xiàn)體,用來(lái)比較兩個(gè)圓的半徑大小 **//若相等,返回值是 0 ;當(dāng)前對(duì)象大則返回1;當(dāng)前對(duì)象小,返回-1 。****

  • 定義矩形類Rectangle,包含的屬性有長(zhǎng)、寬,方法有面積計(jì)算;

  • 定義ComparableRectangle類,在ComparableRectangle類中給出compareTo方法的實(shí)現(xiàn),規(guī)則是根據(jù)兩個(gè)矩形的面積大小判斷矩形的大小。 **//若相等,返回值是 0 ;當(dāng)前對(duì)象大則返回1;當(dāng)前對(duì)象小,返回-1 。******

  • 定義一個(gè)測(cè)試類TestInterface,創(chuàng)建兩個(gè)半徑都為3.0的ComaparableCircle對(duì)象,調(diào)用compareTo方法比較兩個(gè)圓的大小。創(chuàng)建兩個(gè)廠和款分別為6.0和5.0的ComparableRectangle對(duì)象,調(diào)用compareTo方法比較兩個(gè)矩形的大小。
    【樣例輸入】
    沒(méi)有輸入
    【樣例輸出】

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));

    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容