qiancat
qiancat
采纳率100%
2019-04-15 02:04 阅读 736
已采纳

java 接口实现类编译显示不是抽象类,未覆盖抽象方法问题?

接口代码:
interface Js{
public double mjjs();
public double zcjs();
}
两个实现类代码:
正方形
class Zfx implements Js{
double a;
public double mjjs(double a){
double zfxmj = a * a;
return zfxmj;
}
public double zcjs(double a){
double zfxzc = a * 4;
return a * 4;
}
}

class Circle implements Js{
double A = 3.14;
public double mjjs(double r){
double circlemj= A*r*r;
return circlemj;
}
public double zcjs(double r){
double circlezc = A*2*r;
return circlezc;
}
}

测试类
class TestJs{
public static void main(String[] args){
Zfx zfx = new Zfx();
double zfxmj=zfx.mjjs(4.0);
double zfxzc=zfx.zcjs(4.0);
Circle circle =new Circle();
double circlemj=circle.mjjs(2.0);
double circlezc=circle.zcjs(2.0);
System.out.println(zfxmj);
System.out.println(zfxzc);
System.out.println(circlemj);
System.out.println(circlezc);
}
}

请大神帮我看下,哪里出问题了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    ZhangShuiShou ZhangShuiShou 2019-04-15 09:47

    你应该是重载了,你接口中的方法没有元素但继承后类中方法里有元素了

    interface Js{
    public double mjjs(double a);
    public double zcjs(double a);
    }

    class Zfx implements Js{
    double a;
    public double mjjs(double a)
    {
    double zfxmj=a*a;
    return zfxmj;
    }

    public double zcjs(double a)
    {
        double zfxzc=a*4;
        return zfxzc;
    }
    

    }

    class Circle implements Js{
    double A = 3.14;
    public double mjjs(double r){
    double circlemj= A*r*r;
    return circlemj;
    }
    public double zcjs(double r){
    double circlezc = A*2*r;
    return circlezc;
    }
    }
    public class test {
    public static void main(String[] args)
    {
    Zfx zfx = new Zfx();
    double zfxmj=zfx.mjjs(4.0);
    double zfxzc=zfx.zcjs(4.0);
    Circle circle =new Circle();
    double circlemj=circle.mjjs(2.0);
    double circlezc=circle.zcjs(2.0);
    System.out.println(zfxmj);
    System.out.println(zfxzc);
    System.out.println(circlemj);
    System.out.println(circlezc);
    }
    }

    点赞 评论 复制链接分享
  • qq_39249094 用善良的眼光看世界 2019-04-15 09:55

    你这样写报错了吧,报错不就知道是什么问题了吗,你实现了接口,接口里的方法都是抽象方法,你重载了这个方法而没有重写这个方法

    点赞 评论 复制链接分享

相关推荐