提示公用类型
我定义了一个public类,提示公用类型必须在自己的文件中定义,请问是什么意思?
----------------解决方案--------------------------------------------------------
类名是否和文件名相同。
----------------解决方案--------------------------------------------------------
看看代码是怎样的?
----------------解决方案--------------------------------------------------------
public,要类名和文件名一样
----------------解决方案--------------------------------------------------------
我用eclipse新建一个类,名为OblongTester,下面是代码,我是自学菜鸟,大家帮看一下,谢谢
public class Oblong{
private double length;
private double height;
public Oblong(double lengthIn, double heightIn)
{
length = lengthIn;
height = heightIn;
}
public double getLength()
{
return length;
}
public double getHeight()
{
return height;
}
public void setLength(double lengthIn)
{
length = lengthIn;
}
public void setHeight(double heightIn)
{
height = heightIn;
}
public double calculateArea()
{
return length * height;
}
public double calculatePerimeter()
{
return 2 * (length + height);
}
}
import java.util.*;
public class OblongTester{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
double oblongLength, oblongHeight;
Oblong myOblong;
System.out.print("Please enter the length of your oblong:");
oblongLength = sc.nextDouble();
System.out.print("Please enter the height of your oblong:");
oblongHeight = sc.nextDouble();
myOblong = new Oblong(oblongLength, oblongHeight);
System.out.println("Oblong length is " + myOblong.getLength());
System.out.println("Oblong height is " + myOblong.getHeight());
System.out.println("Oblong area is " + myOblong.calculateArea());
System.out.println("Oblong perimeter is " + myOblong.calculatePerimeter());
}
}
----------------解决方案--------------------------------------------------------
我把public class Oblong{放到用eclipse另一个新建的类文件里面就可以了,是不是说一个类文件只能放一个类呢,但我看到的课本有些代码是有几个类放在同一个类文件里面的,都可以执行.
----------------解决方案--------------------------------------------------------
可一个文件下只能有一个public的类啊
----------------解决方案--------------------------------------------------------
我试过只一个类是可以,而我想知道的是我上边的那代码里边有两个类,执行时提示Oblong类必须在他自己的文件中定义,请问是什么意思
----------------解决方案--------------------------------------------------------
我的意思就是让你看下这两个类是不是都是public的
----------------解决方案--------------------------------------------------------
恩,是的,都是public
----------------解决方案--------------------------------------------------------