简体   繁体   English

如何在Java中创建指向参考变量的指针变量?

[英]How do I create a pointer variable to a reference variable in Java?

I would like to make a variable that points to whatever a different reference variable is pointing to, so that if I change where the reference variable points, the pointer variable automatically points to that new place, too. 我想制作一个变量,使其指向其他参考变量所指向的内容,这样,如果我更改了参考变量所指向的位置,则指针变量也将自动指向该新位置。

For example, suppose I consider my favourite book to be whatever my local library's favourite book is. 例如,假设我认为我最喜欢的书是我当地图书馆最喜欢的书。 If my library changes what their favourite book is, I automatically consider that to be my favourite book. 如果我的图书馆更改了他们最喜欢的书,我会自动认为这是我最喜欢的书。

Here is some code to demonstrate what I mean: 这是一些代码来演示我的意思:

Book littleWomen = new Book("Little Women");
Book dracula = new Book("Dracula");

Book libraryFavourite = litteWomen;
Book myFavourite = libraryFavourite;  //myFavoutie is now littleWomen

libraryFavourite = dracula;   //i want myFavourite to update to point to dracula, now.

In the above code, changing what libraryFavourite points to doesn't automatically change what myFavourite points to. 在上面的代码中,更改libraryFavourite指向的内容并不会自动更改myFavourite指向的内容。 It stays pointing at littleWomen . 它一直指向着littleWomen

I understand why the above code doesn't work as I'm saying I "want" it to. 我理解上面的代码为什么不起作用,因为我是说我想“这样做”。 I understand that reference variables hold memory addresses, and therefore myFavourite = libraryFavourite just assigns the memory address that libraryFavourite points to into myFavourite , and thus future changes to libraryFavourite doesn't change myFavourite . 我知道引用变量保存内存地址,因此myFavourite = libraryFavourite只是将libraryFavourite指向的内存地址分配到myFavourite ,因此将来对libraryFavourite所做的更改不会更改myFavourite I only include the above code to help clarify the behaviour that I want, but understand I'll need a different approach to achieve. 我只包含上面的代码来帮助阐明我想要的行为,但是了解我将需要一种不同的方法来实现。

This link talks about aliasing a class (and the answers received was basically that it can't be done). 链接讨论了对类的别名(并且收到的回答基本上是无法完成的)。 Aliasing isn't exactly what I want done, though, because I want to be free to change myFavourite to stop pointing to the library's favourite book, and instead to something else (such as, for example, some new book that I newly discovered and fell in love with). 不过,别名并不是我想要做的,因为我想自由地更改myFavourite以停止指向图书馆最喜欢的书,而不是指向其他内容(例如,我刚发现的一些新书,爱上了)。

To achieve such behaviour you have to store references in objects instead of local values. 要实现这种行为,您必须将引用存储在对象中,而不是本地值中。 It should never work as you're expecting. 它永远不会像您期望的那样工作。 Variable just points to object, and if you change it you just make it points to different object - you cannot change also root object! 变量仅指向对象,如果更改它,则仅使其指向不同的对象-您也不能更改根对象!

Favorite libraryFavorite = new Favorite(littleWomen);
Favorite myFavorite = libraryFavorite;

libraryFavorite.set(dracula);

//now myFavorite.get also points to dracula

and Favorite is just a reference holder: 而收藏夹只是参考持有者:

public class Favorite {
  private Book book;

  public Favorite(Book book) {
    this.book = book;
  }

  public void set(Book newBook) {
    this.book = newBook;
  }

  public Book get() {
    return book;
  }
}

There is not way to do that. 没有办法做到这一点。 When you assign a reference (referenceA) to another reference (referenceB). 当您将一个参考(referenceA)分配给另一个参考(referenceB)时。 The referenceA copy the memory address of referenceB and that's it. referenceA复制了referenceB的内存地址,仅此而已。 referenceA forget about referenceB. referenceA忘了referenceB。 If referenceB point to another memory address, that change doesn't apply to referenceA. 如果referenceB指向另一个内存地址,则该更改不适用于referenceA。

A workaround would be to let your book class have a constructor that takes another book as an argument, use that the first time you initialize it, then mutate it. 一种解决方法是让您的书类具有一个构造函数,该构造函数将另一本书作为参数,在首次初始化它时使用它,然后对其进行突变。 Perhaps you'd want a subclass for this, so you could treat the favourite as a Book . 也许您想要一个子类,以便将喜欢的Book视为Book I'd actually recommend not doing that, instead have a getter for the book and just do favourite.get().whateverYouWantToDoWithMe(); 实际上,我建议您不要这样做,而应该为书准备一个吸气剂,然后执行favourite.get().whateverYouWantToDoWithMe();

Then you could do this: 然后,您可以这样做:

Book littleWomen = new Book("Little Women");
Book dracula = new Book("Dracula");

Book libraryFavourite = new Book(littleWomen);
Book myFavourite = libraryFavourite;  //myFavourite is now littleWomen

libraryFavourite.setBook(dracula); //myFavourite is now dracula

One possible way to implement this while still allowing Book to be immutable is as follows: 在仍然允许Book不可变的同时实现此功能的一种可能方法如下:

public class ReferenceBook extends Book {
    private Book book;

    public ReferenceBook(Book book) {
        this.book = book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public boolean equals(Object o){
        if (o instanceof Book) {
            return o.equals(book);
        }
        return false;
    }
    // and so on for other methods
}

Then you'd do this: 然后,您可以这样做:

Book littleWomen = new Book("Little Women");
Book dracula = new Book("Dracula");

ReferenceBook libraryFavourite = new ReferenceBook(littleWomen);
Book myFavourite = libraryFavourite;  //myFavourite is now littleWomen

libraryFavourite.setBook(dracula); //myFavourite is now dracula

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

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