函数与界面中的函数冲突

时间:2018-03-11 08:04:25

标签: java interface

我正在使用接口实现此程序以实现层次继承。它显示了这个错误:

"Error:(26, 11) java: compute(float,float) in com.xx.Rectangle cannot 
implement compute(float,float) in com.xx.Area attempting to assign weaker 
access privileges; was public"

请帮忙。 该计划:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Rectangle rect=new Rectangle();
        Circle cir=new Circle();
        Area area;
        area=rect;
        Scanner s = new Scanner(System.in);
        int a=s.nextInt();
        int b = s.nextInt();
        System.out.println("Area of Rectangle = "+area.compute(a,b));
        area=cir;
        System.out.println("Area of Circle = "+area.compute(a,b));    

    }
}


interface Area{
    final static float pi=3.14f;
    float compute(float x,float y);
}

class Rectangle implements Area{
    float compute(float x,float y){
        return(x*y);
    } 
}

class Circle implements Area{
    float compute(float x,float y){
        return(pi*x*y);
    }
}

0 个答案:

没有答案