我无法使我的方法正常工作,因为当我从同一个包导入类时,它会给我一个错误。当我把它们放在同一个包中时,它给出了一个“无法找到符号”的错误,指的是我试图在第二个中使用的类/方法。比如这里,例如,我可以使用其他类中的变量,但每次使用方法时都会抛出错误。我见过类似的问题,但到目前为止,他们都没有帮助我。
第一堂课:
package main;
/**
*
* @author Darias
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ball p;
p = new ball();
System.out.println("the ball weighs" +p.getlength);
}
}
第二课:
package main;
public class ball {
float length;
float weight;
public ball()
{
length = 100;
weight = 250;
}
public ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getlength()
{
return length;
}
public float getweight()
{
return weight;
}
public void kickball()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}
}
注意:它在程序中正确缩进,这里我只是将它传递给文本
答案 0 :(得分:0)
您在p.getlength
Main
之后错过了括号。
System.out.println("the ball weighs" +p.getlength());
答案 1 :(得分:0)
getlength
是一个函数,所以请使用这种方式:"the ball weighs" +p.getlength()
请使用可见性语法并面向对象:private, protected etc.
public class Ball {
private float length;
private float weight;
public Ball()
{
this.length = 100;
this.weight = 250;
}
public Ball(float length, float peso)
{
this.length = length;
this.weight = weight;
}
public float getLength()
{
return length;
}
public float getWeight()
{
return weight;
}
public void kickBall()
{
System.out.println("you kicked the ball");
}
public void atraparPelota()
{
System.out.println("you caught the ball");
}