简体   繁体   English

从另一个类的构造函数获取价值

[英]Getting value from constructor in another class

I am sure this is a very easy question for many, but I am struggling with it. 我确信这对许多人来说是一个非常简单的问题,但我正在努力解决。 I am trying to get a value from the following constructor and place it in a vector. 我试图从以下构造函数中获取一个值并将其放置在向量中。

Each time I add the object to the vector though, the value that is placed inside the vector is null. 但是,每次我将对象添加到向量中时,向量中放置的值都为null。 How can I get the number to be the value that is placed into the vector? 如何获取数字作为放置在向量中的值?

The CInteger class: CInteger类:

public class CInteger  
{
    private int i;
    CInteger(int ii)
    {
        i = ii;
    }
}

And in my A1 class, the constructor and my attempt at getting the value: 在我的A1类中,构造函数和我尝试获取值的尝试:

    Object enqueue(Object o) 
    {
        CInteger ci = new CInteger(88);
        Object d = ??
        add(tailIndex, d);// add the item at the tail
    }

Thank you all for any insight and help, I am still learning. 谢谢大家的见解和帮助,我仍在学习。

EDIT: SOLVED 编辑:已解决

CInteger class: CInteger类:

public class CInteger implements Cloneable // Cloneable Integer 
{
    int i;
     CInteger(int ii)
    {
        this.i = ii;
    }

public int getValue()
    {
        return i;
    }

}

Both enqueue methods: 两种入队方法:

public void enqueue(CInteger i)  // enqueue() for the CInteger
{
    add(tailIndex, new Integer(i.getValue())); get int value and cast to Int object
}
public void enqueue(Date d)  // enqueue() for the Date object
{
    add(tailIndex, d);
}

Thank you very much everyone. 非常感谢大家。 :D :D

You can simply overload the enqueue class to take both Dates and Integers. 您可以简单地使入队类重载以同时使用Date和Integer。 In either case, it sounds like you need a method getValue() in CInteger that lets you access the int value. 无论哪种情况,听起来您都需要在CInteger中使用方法getValue()来访问int值。

public class CInteger
{
    //constructors, data

    public void getValue()
    {
        return i;
    }
}

and then you can have two enqueue() methods in your other class: 然后在另一个类中可以有两个enqueue()方法:

public void enqueue(Date d)
{
    add(tailIndex, d);
}

public void enqueue(CInteger i)
{
    add(tailIndex, new Integer(i.getValue()); //access the int value and cast to Integer object
}

And Java will know which one you are calling automatically based on the parameters. Java会根据参数自动知道您正在调用哪一个。

It is not entirely clear what you are actually trying to do, but I think that this will suffice: 目前尚不清楚您实际上要做什么,但是我认为这足以满足需要:

Object enqueue() {
    CInteger ci = new CInteger(88);
    add(tailIndex, ci);// add the item at the tail
    return ci;  // this will automatically upcast ci as an Object
}

Try this. 尝试这个。

public class CInteger {
    private int i;

    CInteger(int ii) {
       this.i = ii;
    }
}

Using the this Keyword 使用此关键字

Wouldn't it just be: 不会只是:

void main(string[] args)
{
    CInteger ci = new CInteger(88);

    encqueue(ci.i);
}

Object enqueue(Object o) 
{
    add(tailIndex, o);
}

Or am I missing something? 还是我错过了什么?

First of all, Constructors never return any value. 首先, 构造函数永远不会返回任何值。 You have to access the value through its objects or you have to use getter methods. 您必须通过其对象访问该值,或者必须使用getter方法。

In your case, " private int i; " can not be accessed directly. 在您的情况下,不能直接访问“ private int i; ”。 So try make either it as public or have some getter method. 因此,请尝试将其公开或使用某种吸气方法。

So try it: 所以尝试一下:

    CInteger ci = new CInteger(88);
    Object d = ci.i; // if i is public member
    add(tailIndex, d);

or 要么

    ...
    private int i;
    ...
    public int getI() {
        return  this.i;
    }
    ...
    CInteger ci = new CInteger(88);
    Object d = ci.getI(); 
    add(tailIndex, d);

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

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