简体   繁体   English

我在导入最近完成的课程时遇到问题

[英]I am having trouble importing a class I recently made

The class I just made is as follows: 我刚刚制作的课程如下:

package rectangle;

public class Rectangle {
    private double length,width;

    public void setLength(double length) {
        this.length=length;
    }

    public void setWidth(double width)   {
        this.width=width;
    } 

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public double area() {
        return length*width;
    }
}

It is in the package rectangle. 它在包装矩形中。 I understand now that I am supposed to import the class when I am going to use it outside a package that it is in. So: 我现在知道,当我要在其所在的包中使用该类时,应该导入该类。因此:

/*Testing out the rectangle class*/

package rectangleclasstest;

import java.util.Scanner;
import rectangle.Rectangle;                     //Here I try to import the class

public class RectangleClassTest {

    static void main(String[] args) 
    {
        Scanner keyboard= new Scanner(System.in);
        Rectangle rec=new Rectangle();

          //get length
        System.out.println("Please enter the length");
        rec.setLength(keyboard.nextInt());

    }

}

I am now having trouble because the program is telling me that the package rectangle does not exist. 我现在遇到麻烦了,因为程序告诉我包矩形不存在。 Why would it be saying this? 为什么会这样说呢? I am using Netbeans. 我正在使用Netbeans。

Your code is correct as far as I can tell, your issue is with class paths. 据我所知,您的代码是正确的,您的问题出在类路径上。

Class paths are basically where the file is on your computer. 类路径基本上是文件在计算机上的位置。 For example the program is may be looking for documents/folder_name/rectangle/Rectangle.class, but really it's in desktop/foo/rectangle/Rectangle.class (these paths are arbitrary and have no meaning). 例如,程序可能正在寻找document / folder_name / rectangle / Rectangle.class,但实际上它在desktop / foo / rectangle / Rectangle.class中(这些路径是任意的,没有任何意义)。 What you should do is check that the classes are in the similar locations and NetBeans can access them. 您应该做的是检查类是否在相似的位置,并且NetBeans可以访问它们。

Here is some reading: 这里是一些阅读:

http://en.wikipedia.org/wiki/Classpath_(Java) http://en.wikipedia.org/wiki/Classpath_(Java)

How to setup classpath in Netbeans? 如何在Netbeans中设置类路径?

You may be able to import the package when you create the class like you can in eclipse, but I'm not positive with netbeans 您可以像在eclipse中一样在创建类时导入包,但是我对netbeans并不满意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM