在 Java 中创建对象

发布于 2024-09-27 05:41:28 字数 1289 浏览 3 评论 0原文

我有以下类:

class Position {

        private double x,y;
        private int id;

        private static int count=0; //counts number of times a Position object has been created

        public Position (double initX, double initY) {
            x=initX;
            y=initY;
            id=count;
            count++;
        }

        public Position (Position a) {
            id=count;
            count++;
            x=a.x;
            y=a.y;
        }

我现在想在另一个 .java 文件中创建一个 Position 对象。我该怎么做呢?我不是只使用 Position x=new Position; 吗?那不行。我是否必须将职位类别导入到文件中?我也尝试过,没有成功。不让我进口。我的文件位于默认文件夹中。

这就是我想使用它的地方。我什至不确定我是否正确阅读了说明。这就是他们想从我这里得到的吗?将数组的每个元素初始化为新的位置对象?

/**
     * Returns an array of num positions. Each position is initialized to a random
     * (x,y) position.
     * if num is less than zero, just return an empty array of length 0.
     * 
     * @param num
     *            number of positions to create
     * @return array of newly minted Points
     */
 public int[] randomPos(int[] a) {
     int numPositions=Position.getNumPositionsCreated();
     int[] posArr=new int[numPositions];
     int x,y;
     for (int i=0; i<numPositions;i++)
         Position rand = new Position(x,y);
         //

I have the following class:

class Position {

        private double x,y;
        private int id;

        private static int count=0; //counts number of times a Position object has been created

        public Position (double initX, double initY) {
            x=initX;
            y=initY;
            id=count;
            count++;
        }

        public Position (Position a) {
            id=count;
            count++;
            x=a.x;
            y=a.y;
        }

I want to now create a Position object in another of my .java files. How would I do so? Wouldnt I just use Position x=new Position; ? Thats not working. Do I have to import the position class into the files? I tried that too, didnt work. Wouldnt let me import. My files are in the default folder.

Here's where I want to use it. Im not even sure Im reading the instructions correctly. Is this what they want from me? To initialize every element of the array to a new position object?

/**
     * Returns an array of num positions. Each position is initialized to a random
     * (x,y) position.
     * if num is less than zero, just return an empty array of length 0.
     * 
     * @param num
     *            number of positions to create
     * @return array of newly minted Points
     */
 public int[] randomPos(int[] a) {
     int numPositions=Position.getNumPositionsCreated();
     int[] posArr=new int[numPositions];
     int x,y;
     for (int i=0; i<numPositions;i++)
         Position rand = new Position(x,y);
         //

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

壹場煙雨 2024-10-04 05:41:28

您需要调用构造函数,

Position x = new Position(2.0,2.0);

由于您的构造函数之一采用两个 double 作为参数,因此我使用它们作为示例。

或者,您可以通过传入另一个 Position 对象来创建一个新的 Position 对象,

Position otherX = new Position(new Position(2.0,2.0)); 
// or combining our above example assuming that x is already instantiated
Position otherX = new Position(x);

此外,以防万一您不确定,实例化之间存在差异。 em> 和声明

实例化:

Position posX = new Position(1.0, 4.0);

现在,posXPosition 对象的实例,因为我们通过调用构造函数来构造对象。

声明:

Position posX;

请注意,posX 变量已声明Position 对象,但尚未实例化,因此posX 将有一个 null 引用。

更新:

没有真正为你做作业,因为你不会那样学习。我可以告诉你,到目前为止你所拥有的内容与上面 javadoc 中列出的内容不一致。另外,鉴于 javadoc 的编写方式,很难理解,因此让我尝试为您清理它并让您完成其余的工作,

/* Returns an array of n Positions. Each Position is initialized to a random
 * (x,y) position.
 * if n is less than zero, just return an empty array of length 0.
 * 
 * @param n
 *     number of Positions to create
 * @return array of newly created Positions
 */

现在我们可以分解该 javadoc,所以让我们查明我们所知道的内容。

  • 我们传递一个参数 n ,它指示 Positions 数组应该有多大。
  • 我们需要检查n是否等于0,如果是,我们返回一个空的Position数组。
  • 每个 Position 对象都将使用随机的 xy实例化
  • 我们知道我们需要返回一个 Position 数组。

这应该可以帮助您开始,我相信您可以弄清楚其余的事情。

You would need to invoke the constructor,

Position x = new Position(2.0,2.0);

Since one of your constructor takes two double's as arguments, I used those as an example.

Or, you could create a new Position object by passing in another Position object,

Position otherX = new Position(new Position(2.0,2.0)); 
// or combining our above example assuming that x is already instantiated
Position otherX = new Position(x);

Also, just in case you are unsure, there is a difference between instantiation and declaration!

Instantiation:

Position posX = new Position(1.0, 4.0);

Now, posX is an instance of a Position object, because we construct our object by invoking the constructor.

Declaration:

Position posX;

Note that the posX variable is declared to be a Position object, but has not yet been instantiated so posX would have a null reference.

Update:

Without actually do the homework for you, because you will not learn that way. I can tell you that what you have so far, and what is listed in the javadoc above do not agree. Also, given by the way the javadoc is written, it is tough to follow, therefore let me try to clean it up for you and leave you to do the rest,

/* Returns an array of n Positions. Each Position is initialized to a random
 * (x,y) position.
 * if n is less than zero, just return an empty array of length 0.
 * 
 * @param n
 *     number of Positions to create
 * @return array of newly created Positions
 */

Now we can break down that javadoc, so lets pinpoint what we know.

  • We are passing an argument, n which indicates how big the Positions array should be.
  • We need to check to see if n is equal to 0, if so we return an empty Position array.
  • Each Position object would be instantiated with random x and y values.
  • We know that we need to return an Position array.

This should get you started, I am sure you can figure out the rest.

风透绣罗衣 2024-10-04 05:41:28
Position x = new Position(1.0, 2.0);

例如。您现在的方式缺少方法参数。

Position x = new Position(1.0, 2.0);

For example. The way you have it now is missing method arguments.

乖乖公主 2024-10-04 05:41:28
Position x = new Position(1d,2d);

您需要传递构造函数参数。由于您在 Position 类上定义了 2 个构造函数,因此您必须使用其中之一。通常在你的类上放置一个不执行任何操作的构造函数并不是一个坏主意。就像

Postion () {}

或在你的情况下,

Position () {
   Position.static++;
}

因为你正在计数。请注意,您应该静态引用静态成员。

Position x = new Position(1d,2d);

You need to pass your constructor arguments. Since you defined 2 constructors on your Position class, you must use one of them. Usually not a bad idea to put a do nothing constructor on your class. like

Postion () {}

or in your case

Position () {
   Position.static++;
}

since you are counting. Note you should reference static members statically.

软糖 2024-10-04 05:41:28

您的构造函数需要参数,因此您必须提供一些参数。

Position p = new Position(1.0, -2.0);

如果您想创建一个不带参数的构造函数,则需要添加一个零参数构造函数。

public Position() {
}

然后通过执行 Position p = new Position() 来调用它(不传入参数时需要括号)

如果您没有定义任何构造函数,java 会自动创建一个隐含的零参数构造函数,但是一旦定义了不同的构造函数,就必须显式定义一个零参数构造函数才能拥有一个。

如果您的两个类都在默认包中,则不需要导入任何内容。

Your constructors take arguments, so you have to provide some.

Position p = new Position(1.0, -2.0);

If you want to create one without arguments, you need to a add a zero-argument constructor.

public Position() {
}

which would then be invoked by doing Position p = new Position() (the parenthesis are required when not passing in arguments)

If you don't define any constructors, java automatically creates an implied zero-arg constructor, but once you define a different constructor, you have to explicitly define a zero-arg constructor to have one.

If both of your classes are in the default package you shouldn't need to import anything.

帝王念 2024-10-04 05:41:28

Java已经有一个Point类。这对你来说可能有点过分了,但我还是要提一下。

http://download-llnw.oracle .com/javase/6/docs/api/java/awt/geom/Point2D.html

Java already has a Point class. It may be overkill for you but though I'd mention it.

http://download-llnw.oracle.com/javase/6/docs/api/java/awt/geom/Point2D.html

酒解孤独 2024-10-04 05:41:28

调用 new 时,不要忘记传递到构造函数中的括号和参数,例如:

Position p = new Position(x, y);

根据另一个类的位置,您可能还需要将 Position 设为公共类(看来您正在使用 package- 的默认可见性)私人的)。

另请注意,x 和 y 必须首先初始化,

Don't forget the parentheses and arguments to pass into the constructor when you call new, eg:

Position p = new Position(x, y);

Depending upon where the other class is, you may also need to make Position a public class (it appears you are using the default visibility of package-private).

Also note that x and y must be initialized first,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文