我能使用设置方法将一个链表节点设置到另一个节点吗?

huangapple 未分类评论46阅读模式
英文:

Can I use a setter method to set a linked list node to another node?

问题

以下是翻译好的部分:

我刚开始学习Java,想尝试一下使用链表。一切都运行得很完美,但现在我想要将对节点属性的直接调用都替换为使用get和set方法。然而,有一个方法让我感到困扰。我想实现一个setNode方法,将当前节点设置为另一个节点。

以下是相关代码的部分内容:

public class ListNode {
	
	int value;
	ListNode next;

	// 更多的代码在中间,但理论上不应该与此相关 :P
	
    public void setNode(ListNode node) {
		this = node;
	}

}

当我尝试编译程序时,会得到以下错误信息:

ListNode.java:32: error: cannot assign a value to final variable this
                this = node;
                ^

所以我想知道是否有某种解决方法,以及为什么我们可以毫无问题地执行这样的操作:

node = node.next;
// 或者
node = node2;

例如,但不能这样做:

this = this.next;
// 或者
this = node2;
英文:

I'm new at learning Java and I wanted to try my luck with linked lists.
It all worked perfectly, but now I want to replace all direct calls to attributes of the nodes with get and set methods. There is however one method that troubles me. I wanted to implement a setNode method that would set the current node as another node.

Here is some of the relevant code:

public class ListNode {
	
	int value;
	ListNode next;

	// more code exists in-between but it shouldn't be related (theoretically speaking :P)
	
    public void setNode(ListNode node) {
		this = node;
	}

}

When I try to compile the program, I get the following error message:

ListNode.java:32: error: cannot assign a value to final variable this
                this = node;
                ^

So I was wondering if there is some sort of a workaround, and also why we can do this without issues:

node = node.next;
// or
node = node2;

for example, but not this:

this = this.next;
// or
this = node2;

答案1

得分: 0

'this'关键字代表了你正在引用的当前对象。因此,你不能简单地给 'this' 赋予一个对象值。

我建议对你的代码进行以下更改:

public void setValue(int value) {
    this.value = value;
}

public void setNextNode(ListNode node) {
    this.next = node;
}
英文:

The 'this' keyword represents the current object you're referencing. So you simply can't assign an object value to 'this'.

Am suggesting the following changes to your code,

public void setValue(int value) {
    this.value = value;
}

public void setNextNode(ListNode node) {
    this.next = node;
}

答案2

得分: 0

"this" 关键字用于在 Java 中引用当前对象。更重要的是,您的错误提示信息表明这是一个 final 变量(因此不能被更改)。将其设为 final 变量的原因是它可以随时用于获取当前对象的上下文。想象一下,如果有人修改它,那么在需要当前对象的代码中如何后续使用它。

在您的示例中,您需要使用以下代码:

public void setNode(ListNode node) {
     this.next = node;
}
英文:

this keyword is used to refer current object in java. More importantly, your error gives the information that this is a final variable (hence it cannot be changed). The reason it is made the final variable is that it can be used anytime to get the context of current object. Imagine if someone modifies it, how can it be used later in the code where the current object is required.

In your example, you need to use the following:

public void setNode(ListNode node) {
     this.next = node;
}

huangapple
  • 本文由 发表于 2020年5月4日 15:00:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/61586742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定