【问题标题】:the method is not applicable for the arguments该方法不适用于参数
【发布时间】:2012-08-30 22:22:03
【问题描述】:

我收到此错误:

t33.Psicodelia 类型中的方法 pintar() 不适用于参数 (int,int,int,int,int,int)

我该如何解决这个问题?

我有两个类和主选项卡:

第一类

public class Logica {
  Psicodelia miPsicodelia;


  public Logica() {
  }

  public void pintar() {
    miPsicodelia.pintar(width/2, height/2, height/4, 0, 0, 1);
    miPsicodelia.kaleidouno(posX, posY, rad, ang, depth, tam1);
  }  

  public void pressed() {
    miPsicodelia.pressed();
  }
}

2 级

public class Psicodelia {
  private float anguloGrande;
  private int numBolas, contador;


  public Psicodelia() {
    stroke(255);   
    this.anguloGrande = 0.0;
    this.numBolas = 7;
  }


  public void pintar() {
    //fill((int)random(0, 255), (int)random(0, 255), (int)random(0, 255));
    fill(255, 255, 0, (int)random(20, 100));
    pintar(width/2, height/2, height/4, 0, 0, 1);
    anguloGrande += .02; //velocidad de rotacion
  }

  public void kaleidouno(float posX, float posY, float rad, float ang, int depth, float tam) { //pinteme las bolas en la pos posX, posY, reciba un float de radianes y de angulos, y por ultimo un int de profundidad
    if (depth < contador) {
      tam=(int)random(0.5, 1.5);
      float anguloPeq = TWO_PI/numBolas;
      for (int i=0; i < numBolas; i++) {
        float nuevoRad = rad/2; //distancia y tamaño de las bolas entre ellas
        float nuevoAng = ang + i*anguloPeq - anguloGrande;
        float X = cos(nuevoAng)*rad + posX;
        float Y = sin(nuevoAng)*rad + posY;
        pintar(X, Y, nuevoRad, nuevoAng, depth + 1, 1);
      }
    }
    else  if (rad < 2) { 
      ellipse(posX, posY, 2.0*tam, 2.0*tam);
    }
    else { 

      ellipse(posX, posY, rad*2.0, rad*2.0);
    }
  }

  public void pressed() {
    contador++;
    if (contador >= 3) { 

      contador--;
    }
  }



  float getPosX () {
    return posX ;
  }


  float getPosY () {
    return posY ;
  }
}


// and the main tab


Logica miLogica;

//================================================================

void setup() {
  size(800,600);
  smooth();

miLogica= new Logica();

}

//================================================================

void draw() {
  background(0);
 miLogica.pintar();

}

//================================================================

void mousePressed() {
miLogica.pressed();
}
//================================================================

【问题讨论】:

  • 看起来在类Psicodelia 中的方法pintar() 中,您正在调用pintar(width/2, height/2, height/4, 0, 0, 1);,而实际上您应该调用kaleidouno(width/2, height/2, height/4, 0, 0, 1);
  • pintar(int,int,.,.,.,.,.,.) 的函数定义在哪里?您在类 2 中没有参数的 pintar() 方法;

标签: java class methods arguments


【解决方案1】:

你调用了方法

Psicodelia miPsicodelia;
miPsicodelia.pintar(width/2, height/2, height/4, 0, 0, 1);

但是您的Psicodelia 类只有以下pintar 方法:

public void pintar();

要以您的方式调用它,您必须为方法pintar() 提供希望的参数。

例如:

public void pintar(int a, int b, int c, int d, int e, int f){
    // do whatever you want here
}

P.S.:你会在任何地方实例化你的 miPsicodelia 对象吗?如果没有,这会给你一个 NullPointerException

【讨论】:

  • 谢谢,我试过了,但如果我这样做,我会得到意想不到的令牌:int
  • 你到底尝试了什么?您是否在 Psicodelia 类中实现了 public void pintar(int a, int b, int c, int d, int e, int f) 方法?错误究竟出现在哪里?行号会有所帮助。
  • 我写了这行。 miPsicodelia.pintar(int width/2, int height/2, int height/4, int 0, int 0, int 1);
  • 好吧,不,不是那样。当您编写miPsicodelia.pintar(width/2, height/2, height/4, 0, 0, 1); 时,您的意思是,您进入miPsicodelia 的类(即Psicodelia)并查找带有参数'int,int,int,int,int,int'的方法'pintar' ,这意味着,您需要在 Psicodelia 类中使用方法 public void pintar(int a, int b, int c, int d, int e, int f){...} 方法。因为他们现在只有public void pintar(){...} 你必须声明一个新方法。
【解决方案2】:

好吧,除非我错过了什么,否则您的 public void pintar() 方法声明时没有参数,因此您不能使用任何参数调用它。您必须使用所需数量和类型的参数声明它:

public void pintar(int para1, int para2, int para3, int para4, int para5, int para6)
{ 
   //Do something with the parameters.
}

【讨论】:

  • 我知道,但我在上面说过,如果给它参数,我会得到意想不到的令牌:int
  • 我想你误会了。 声明是错误的。您将方法创建为public void pintar() { //... },不带任何参数。因此,当您尝试调用 miPsicodelia.pintar(width/2, height/2, height/4, 0, 0, 1); 时,您的方法不知道如何处理它获得的数字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
相关资源
最近更新 更多