Tag: 这个

java“this”关键字正确使用

我的构造函数中有一个使用关键字this的Fraction类: public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; adjustSigns(); if(this.denominator == 0 ) { throw new FractionException(” Undefined Fraction “); } } 我也有一个方法: public FractionInterface multiply(FractionInterface secondFraction) { Fraction second = (Fraction) secondFraction; Fraction answer = new Fraction ((numerator * second.numerator), (denominator * second.denominator)); answer.reduceToLowestTerms(); return answer; } 我编译和运行时上面的方法工作正常但是这个版本: public […]

C ++相当于Java

在Java中,您可以通过执行以下操作来引用当前对象: this.x = x 。 你是如何用C ++做的? 假设这些代码示例中的每一个都是名为Shape的类的一部分。 Java的: public void setX(int x) { this.x = x; } C ++: public: void setX(int x) { //? }

请向我解释“这个”

我已经在java中阅读了关于“this”的数百个解释,我真的很难理解它。 我正在学习android和java并排,我知道这样更难,但我很享受。 我被杀的一件事就是“这个”……我正在粘贴下面的教程中的代码,该教程一次使用“this”。 我只想放一段代码,但希望尽可能地提供帮助。 我正在寻找一个很好的解释“这个”,我可以添加到我的笔记。 任何和所有的帮助表示赞赏。 提前致谢。 示例代码从下面开始: import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import android.view.View; import android.content.DialogInterface; import android.app.Dialog; import android.app.AlertDialog; public class DialogActivity extends Activity { CharSequence[] items = { “Google”, “Apple”, “Microsoft” }; boolean[] itemsChecked = new boolean [items.length]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle […]

为什么我不能在静态上下文中使用“super”变量,即使“super”指的是父类而不是类实例,不像“this”?

我说的是java语言。 变量“this”在类中使用时,引用该类的当前实例,这意味着您不能在静态方法中使用“this”。 但是,“super”,当在类中使用时,指的是该类的超类,而不是超类的实例,这应该意味着你可以在静态方法中使用“super”。 但事实certificate你不能。 一个可能的解释是说“超级”也指超类的一个实例,但我不明白为什么它应该……

“this”关键字:Java中的工作机制

在学习Java一段时间之后,第一次使用this关键字让我非常困惑。 这是我如何困惑。 我写了以下代码: class BasicInheritanceTest3Base{ private int x = 0; public int y; public void a() { x++; this.x++; System.out.println(“BasicInheritanceTest3Base.a()”); b(); this.b(); System.out.println(x); System.out.println(y); } public void b(){ System.out.println(“BasicInheritanceTest3Base.b()”); } } public class BasicInheritanceTest3 extends BasicInheritanceTest3Base { private int x = 3; public int y = 2; public void b() { System.out.println(“BasicInheritanceTest3.b()”); } public static […]

使用Java这个关键字

在类构造函数中,我试图使用: if(theObject != null) this = theObject; 我搜索数据库,如果记录存在,我使用Hibernate Query生成的对象。 为什么我不能用this ?

Java中有“this”这个名字吗?

Eclipse会给出一个错误,“赋值的左侧必须是一个变量”,当我尝试类似的东西时: public class Thing{ String a1; int a2; public void meth(){ Thing A = new Thing(); this = A; } } 我必须分配每个变量( this.a1 = A.a1; this.a2 = A.a2; )作为解决方法。 是否有其他方法可以在不通过每个变量字段的情况下执行此操作 如果this不是一个变量,它叫什么?

在Java中使用“this”

如果我写下面的课程: public class Example { int j; int k; public Example(int j, int k) { j = j; k = k; } public static void main(String[] args) { Example exm = new Example(1,2); System.out.println(exm.j); System.out.println(exm.k); } } 程序编译,但是当我运行程序时,main方法将打印出两个0。 我知道为了说我想在构造函数中初始化实例变量,我必须写: this.j = j; this.k = k; 但是如果我不写它,那么在构造函数中(在表达式的左侧和写入侧)评估(或考虑)哪个变量? 是参数还是实例变量? 这有什么不同吗? 是否有其他情况需要使用thisfunction?

在方法中使用“this”(在Java中)

如何在Java中使用“this”和方法呢? 它是可选的还是有需要使用它的情况? 我遇到的唯一情况是在类中调用方法中的方法。 但它是可选的。 这是一个愚蠢的例子,只是为了表明我的意思: public class Test { String s; private String hey() { return s; } public String getS(){ String sm = this.hey(); // here I could just write hey(); without this return sm; } }

为什么我们可以使用’this’作为实例方法参数?

什么是Java中的receiver参数? Java 8语言规范谈到了this 。