java 创建对对象的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14950448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:59:21  来源:igfitidea点击:

Creating References to Objects

javaobjectreference

提问by Sherifftwinkie

I have this Object here:

我在这里有这个对象:

public class ItemInfo {

    String productName;
    String rfidTagNumber;
    String originalLocal;
    String currentLocal;
    double productPrice;

    public ItemInfo(String name, String tag, String origLoc, String curLoc, double price) {
        productName = name;
        rfidTagNumber = tag;
        originalLocal = origLoc;
        currentLocal = curLoc;
        productPrice = price;
    }

I need to create and have a reference to an ItemInfoObject in here:

我需要ItemInfo在这里创建并引用一个对象:

public class ItemInfoNode {

    ItemInfoNode next;
    ItemInfoNode prev;

    public ItemInfoNode(){

    }

    public void setInfo(ItemInfo info) {

    }

    public ItemInfo getInfo(){
        return null;    
    }

    public void setNext(ItemInfoNode node) {

    }

    public void setPrev(ItemInfoNode node) {

    }

    public ItemInfoNode getNext() {
        return null;
    }

    public ItemInfoNode getPrev() {
        return null;
    }
}

I know how to make new objects but I am not quite sure that is correct so I omitted the line, how do I create just a reference?

我知道如何创建新对象,但我不太确定这是否正确,所以我省略了该行,如何仅创建引用?

EDIT: Just another small sidebar, I have two ItemInfoNode objects next and prev as seen above, do I just leave the constructor blank? Is that okay? I pasted the rest of the class to show functions, if that'll help an answer in anyway. Thanks again!

编辑:只是另一个小侧边栏,我有两个 ItemInfoNode 对象 next 和 prev 如上所示,我是否只是将构造函数留空?这样可以吗?我粘贴了课程的其余部分以显示函数,如果这有助于回答的话。再次感谢!

回答by Ryan Stewart

In Java, the newkeyword returns a reference to a newly-constructed object. Therefore, in

在 Java 中,new关键字返回对新构造对象的引用。因此,在

String s = new String("foo");

the value of variable sis a reference to a String object. And then, if you were to do something like

变量的值s是对 String 对象的引用。然后,如果你要做类似的事情

String t = s;

then you've set tto the same value as s, which is to say, it's now a reference to the same object.

那么您已设置t为与 相同的值s,也就是说,它现在是对同一对象的引用。

回答by Michelle

This looks like an implementation of a LinkedList. So what you want to do is to store the reference to the next node and previous ItemInfoNode using the setters - i.e. setNext() and setPrev() method by storing the arguments (which are the references) as your class variables next and prev which you declared already. Then in your getters - i.e. getPrev() and getNext() you can return your stored variables.

这看起来像是LinkedList的实现。所以你想要做的是使用 setter 来存储对下一个节点和上一个 ItemInfoNode 的引用 - 即 setNext() 和 setPrev() 方法通过将参数(它们是引用)存储为你的类变量 next 和 prev已经声明了。然后在你的 getter 中——即 getPrev() 和 getNext() 你可以返回你存储的变量。

public void setNext(ItemInfoNode node){
    next = node;
}
public void setPrev(ItemInfoNode node){
    prev = node;
}
public ItemInfoNode getNext(){
    return next;
}
public ItemInfoNode getPrev(){
    return prev;
}

回答by Ulises

It is unclear what you are trying to accomplish but the simplest way to create an object is to the use the class name ItemInfoand the newkeyword. For example, you could this line of code and declare your object reference globally:

不清楚您要完成什么,但创建对象的最简单方法是使用类名ItemInfonew关键字。例如,您可以使用这行代码并全局声明您的对象引用:

ItemInfo myObj= new ItemInfo();

回答by SudoRahul

You can either create a new instance like this

您可以像这样创建一个新实例

ItemInfo myObj = new ItemInfo("name", "tag", "location", "local", 0.0);

ItemInfo myObj = new ItemInfo("name", "tag", "location", "local", 0.0);

Or, do something like this:-

或者,做这样的事情:-

public class ItemInfoNode{

        ItemInfo myObj;

        public ItemInfoNode(ItemInfo myObj){

        this.myObj = myObj;

}