当我使用 javac 编译多个.java文件时,出现一些“重复类”错误,但我在代码中找不到错误。
有四个.java文件,所有这些文件都位于 Windows 中的同一文件夹中。
MyApp.java文件中的代码:
import dx.*;
import dx.shapes.*;
class MyApp {
public static void main(String[] args) {
System.out.println("This is a test application.");
Rectangle rect = new Rectangle(10, 20);
rect.Speak();
Circle circle = new Circle(15);
circle.Speak();
Worker worker = new Worker();
worker.Speak();
}
}
Rectangle.java文件中的代码:
package dx.shapes;
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void Speak(){
System.out.println("I'm a rectangle, width:" + this.width + ", height:" + this.height);
}
}
Circle.java文件中的代码:
package dx.shapes;
public class Circle {
private int x, y;
private int radius;
public Circle() {
this(0, 0, 10);
}
public Circle(int radius) {
this(0, 0, radius);
}
public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public void Speak(){
System.out.println("I'm a circle, radius:" + this.radius);
}
}
Worker.java文件中的代码:
package dx;
public class Worker {
public void Speak(){
System.out.println("I'm a worker.");
}
}
在Windows命令行中,我使用javac来编译这些源代码:
javac MyApp.java Rectangle.java Circle.java Worker.java
狐的传说
郎朗坤
开满天机
相关分类