处理/ Java接口构造不起作用

时间:2015-12-14 16:47:04

标签: java interface processing

我尝试在处理中使用接口。 接口方法setMapContent(int r, int c, int breite, int hoehe)不起作用。以下是我如何使用它:

界面:

interface MapContent{
    public void setMapContent(int r, int c, int breite, int hoehe);
}

我尝试使用方法的类方法:

void ubdate(MapContent function)
{
    if(usesMapArray == false)
    {
        for(int r = 0; r < rows; r++)
        {
            forR = r;

            for(int c = 0; c < column; c++)
            {
                forC = c;

                if(usesMapSelectedWH == true)
                {
                    //content(c*contentBreite,r*contentHoehe,contentBreite,contentHoehe);
                    //function.setMapContent(r,c,contentBreite,contentHoehe);
                    function.setMapContent(forR*contentBreite,forC*contentHoehe,contentBreite,contentHoehe);
                }
                else
                {
                    println("Please select width and heigth Manually ");
                    //content(c*contentBreite,r*contentHoehe,contentBreite,contentHoehe);
                    //function.setMapContent(r,c,(int)random(0,25),(int)random(0,25));
                    function.setMapContent(forR*(int)random(0,25),forC*(int)random(0,25),(int)random(0,25),(int)random(0,25));
                }
            }
        }
    }
    else if(usesMapArray == true)
    {
        for(int r = 0; r < mapArray.length; r++)
        {
            for(int c = 0; c < mapArray[0].length; c++)
            {
                if(usesMapSelectedWH == true)
                {
                    //content(c*contentBreite,r*contentHoehe,contentBreite,contentHoehe);
                    //function.setMapContent(r,c,contentBreite,contentHoehe);
                    function.setMapContent(forR*contentBreite,forC*contentHoehe,contentBreite,contentHoehe);
                }
                else
                {

                    println("Please select width and heigth Manually ");
                    // content(c*contentBreite,r*contentHoehe,contentBreite,contentHoehe);
                    //function.setMapContent(r,c,(int)random(0,25),(int)random(0,25));
                    function.setMapContent(forR*(int)random(0,25),forC*(int)random(0,25),(int)random(0,25),(int)random(0,25));
                }
            }
        }
    }
}

这是我尝试调用方法的地方:

testKarte.ubdate(new MapContent(){
       public void setMapContent(){
           rect(25,25,25,25);
       }});

每次收到错误时:

  

MapContent(){}必须实现继承的抽象方法MapContent.setMapContent(int,int,int,int);

这是什么意思?

2 个答案:

答案 0 :(得分:2)

错误似乎很清楚。您需要实现方法

 setMapContent(int, int, int, int)

你不是。现在你实现方法

setMapContent()

没有参数,而不是四个int。将您的声明更改为:

testKarte.ubdate(new MapContent(){
        public void setMapContent(int r, int c, int breite, int hoehe){
            rect(25,25,25,25);
        }});

即使您在这种情况下不使用参数,您仍然需要它们作为方法签名的一部分。

答案 1 :(得分:2)

您创建的匿名类没有实现MapContent接口,因为此接口指定一个方法,其中int,int,int,int作为参数,并且您编写的public void setMapContent()不是同一个签名... 您应该更改界面中的签名(删除参数)或更改类定义中的签名:

testKarte.ubdate(new MapContent(){
         public void setMapContent(int a, int b, int c, int d){

           rect(25,25,25,25);

         }});