Tag: 这个

Javainheritance – 此关键字

我在网上搜索了类似的问题,但找不到它。 所以,发帖在这里。 在以下程序中,为什么“i”的值打印为100? AFAIK’this’指的是当前的对象; 在这种情况下,它是’TestChild’,类名也正确打印。 但为什么实例变量的值不是200? public class TestParentChild { public static void main(String[] args) { new TestChild().printName(); } } class TestChild extends TestParent{ public int i = 200; } class TestParent{ public int i = 100; public void printName(){ System.err.println(this.getClass().getName()); System.err.println(this.i); //Shouldn’t this print 200 } } 而且以下的输出正如我所期望的那样。 当我从Parent类调用“ this.test() ”时,将调用子类方法。 public class TestParentChild […]

如何在构造函数结束之前引用/处理“this”?

我想到这个问题的具体用法如下,但它更加普遍。 我有一个自定义的JFrame类,它也可以作为其组件的ActionListener 。 所以我的构造函数看起来像下面这样: private JButton myButton; public MyCustomFrame() { super(); myButton.addActionListener(this); // … more stuff } 我的问题是,这实际上是如何在幕后工作的? 如果构造函数是“创建”由此引用的对象,那么在构造函数返回之前如何使用this ? 代码编译并且工作完全正常(据我所知),因此对象必须已经在某种意义上“存在”,但我担心这可能会导致无法预料的问题。 传递对addActionListener()的“部分构造”引用是否存在任何危险(或者通常只使用它执行任何逻辑)? 或者是否有一些让我安全的幕后魔术? 例如,那些没有默认值但必须由构造函数提供的东西呢? 如果我有private final String SOME_VALUE; 声明,我明白这应该默认为null ,但是在构造函数中提供一个值之前,该对象不应该完全形成。 那么参考文献尽管是最终的,但可能会有不断变化的价值吗?

Java:将“this”作为构造函数参数传递以引用创建对象的替代方法

我已经花了一段时间思考不同的解决方案,因为我已经阅读过(我还没有真正熟悉Java),使用它作为构造函数参数通常不是一个好习惯。 我想要做的是实例化JobGroupMod类的几个对象,并且对于每个JobGroupMod,我必须创建一定数量的JobMod对象,这些对象必须能够引用它们已经从中生成的JobGroupMod对象。 为了实现这一目标,我将“this”传递给JobMod构造函数,但即使工作,也感觉不适合设计。 public class JobGroupMod implements JobGroup { public JobGroupMod(Node n,Set clusterJobs){ JobMod j=new JobMod(n,this); } } 现在是JobMod类: public class JobMod implements Job { public JobMod(Node n, JobGroup jg){ setJobGroup(jg); } } 我的问题是,有没有更好的解决方法,或者我的解决方案是建议的方式?

封闭对象的引用通过匿名类java转义

我在实践中阅读Java并发性,下面的例子来自于此。 我的问题是这个参考逃脱是什么意思? 会有什么问题? 。 该引用如何从doSomething(e)中逃脱。 public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } } ); } } 这是如何解决这个问题的 public class SafeListener { private final EventListener listener; private SafeListener() { listener = new EventListener() { public void onEvent(Event e) { doSomething(e); } }; } public […]

Javainheritance中的“this”关键字如何工作?

在下面的代码片段中,结果确实令人困惑。 public class TestInheritance { public static void main(String[] args) { new Son(); /* Father father = new Son(); System.out.println(father); //[1]I know the result is “I’m Son” here */ } } class Father { public String x = “Father”; @Override public String toString() { return “I’m Father”; } public Father() { System.out.println(this);//[2]It is called in […]

递归构造函数调用错误无法找到解决方案

我在四个公共金枪鱼部分得到了递归构造溢出调用错误(部分=可能是一个类或其他东西?)。 它在教程上工作但不适合我,似乎无法看到哪里 public class tuna { private int hour; private int minute; private int second; public tuna() { this(0,0,0); //default } public tuna(int h){ this(h,0,0); //with hours input } public tuna(int h, int m){ this(h,m,0); //with hours and minutes } public tuna(int h, int m, int s){ this(h,m,s); //with hours, minutes and seconds }

如何在此(…)或超级(…)之前“插入”代码?

有没有办法在调用super(…)或this(…)构造函数之前实现初步计算? 请考虑以下示例: public class Test { private final int n; private final int m; private final int[] store; public Test(int n, int m) { /* This is common (most generic) constructor of the class Test. It is desirable to invoke it via this(…) call from any other constructor of this class since it contains some […]

有人能详细解释一下“这个”的用法吗?

我真的不明白在Java中使用’this’。 如果有人能帮我澄清,我会非常感激。 在这个网站上它说: http : //docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html “在实例方法或构造函数中,这是对当前对象的引用 – 正在调用其方法或构造函数的对象。您可以使用此方法从实例方法或构造函数中引用当前对象的任何成员。 “ 它给出了以下示例: 例如,Point类就是这样写的 public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } } 但它可能是这样写的: public class Point { public int x = 0; public int y = […]

澄清Java中的“this”关键字

我从Android开发者网站复制了这段代码: public class ExampleActivity extends Activity implements OnClickListener { protected void onCreate(Bundle savedValues) { … Button button = (Button)findViewById(R.id.corky); button.setOnClickListener(this); } // Implement the OnClickListener callback public void onClick(View v) { // do something when the button is clicked } … } 我想知道“this”关键字到底是指什么? 它是指“ExampleActivity”类吗? 一般来说如何找到“这个”指的是什么?

在Java中访问内部类的包含类

这就是我现在正在做的事情。 有没有更好的方式来访问超类? public class SearchWidget { private void addWishlistButton() { final SearchWidget thisWidget = this; button.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { // A better way to access the super class? // something like “this.super” …? workWithWidget(thisWidget); } } } } 我正在使用Google Web Toolkit进行编程,但我认为这确实是一个通用的Java问题。