使用下面的代码,我得到一个编译错误"不兼容的类型 - 找到Tree<T>但预期Tree<T>".知道我对仿制药做错了什么吗?有没有其他方法来访问ParentObjectClass.this以外的对象的父对象?我知道"变通办法",比如向Node类添加一个指向其父对象的变量.此外,我可以传递myInt作为构造函数参数,但我想找到一个更好的解决方案.谢谢你的任何建议.
我在这个网站上遇到了一个类似的问题,但是我尝试了我的代码而无法让它工作:从Java匿名类访问"this".
public class Tree<T> implements TreeIF<T> {
private int myInt;
Node<T> root;
...
// a constructor
public Tree(Node<T> root) {
this(root.getTree().getMyInt()); // call to different constructor
this.root = root;
}
...
// a method
public int getMyInt() {
return myInt;
}
...
// inner class
class Node<T> {
T element;
...
Tree<T> getTree() {
// return Tree.this; // I tried this too, but it didn't work
**return Tree<T>.this;** // HERE'S THE …Run Code Online (Sandbox Code Playgroud)