开发者

Getting value from constructor in another class

开发者 https://www.devze.com 2023-02-17 21:01 出处:网络
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.

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. How can I get the number to be the value that is placed into the vector?

The CInteger class:

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

And in my开发者_Go百科 A1 class, the constructor and my attempt at getting the value:

    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:

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


You can simply overload the enqueue class to take both Dates and Integers. In either case, it sounds like you need a method getValue() in CInteger that lets you access the int value.

public class CInteger
{
    //constructors, data

    public void getValue()
    {
        return i;
    }
}

and then you can have two enqueue() methods in your other class:

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.


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


First of all, Constructors never return any value. You have to access the value through its objects or you have to use getter methods.

In your case, "private int i;" can not be accessed directly. 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);


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消