定义接口,声明若干个abstract方法;创建类,在类中定义若干个参数为接口类型的方法,通过实现接口的类的对象回调类重写的接口方法,实现相应功能,最后完成实验报告。

实验三

定义接口,声明若干个abstract方法;创建类,在类中定义若干个参数为接口类型的方法,通过实现接口的类的对象回调类重写的接口方法,实现相应功能,最后完成实验报告。

 输出成功

package com.experimental;
interface IShape    
{
    static final double PI=3.14;
    abstract double getArea();      //声明抽象方法
    abstract double getLength();    //声明抽象方法
}
class Circle implements IShape  //以IShape接口来实现Circle类
{
    double radius;
    public Circle(double r)
    {
        radius=r;
    }
    public double getArea()     //实现接口中getArea()方法
    {
        return PI*radius*radius;
    }
    public double getLength()   //实现接口中getLength()方法
    {
        return 2*PI*radius;
    }
}
class Rectangle implements IShape   //以IShape接口来实现Rectangle类
{
    private double width;
    private double height;
    public Rectangle(double width,double height)
    {
        this.width=width;
        this.height=height;
    }
    public double getArea() //实现接口中getArea()方法
    {
        return width*height;
    }
    public double getLength()//实现接口中getLength()方法
    {
        return 2*(width+height);
    }
}
public class experimental {      //主类
    public static void main(String[] args) {
        IShape circle=new Circle(4.0);      //声明父接口变量circle,指向子类对象
        System.out.print("圆面积="+circle.getArea());      //接口回调
        System.out.println(";周长="+circle.getLength());  //接口回调
        Rectangle rect=new Rectangle(6.5,10.8); //声明rectangle类的变量rect
        System.out.print("矩形面积="+rect.getArea());       //接口回调
        System.out.println(";周长="+rect.getLength());     //接口回调
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦与陌生人

打赏每人一元起步·小气鬼

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值