【发布时间】:2013-08-02 20:46:18
【问题描述】:
谁能给我这个代码的详细解释,并告诉我为什么它打印出它打印的输出。这是我的测试的示例练习题。我真的不擅长这些方法和类部分。所以假设我对这个主题知之甚少。请逐行解释并告诉我值的变化。因此,如果测试中出现类似的问题,我将能够做到。 输出为apples,grapes,pears,kiwi,oranges,pears,pears,grapes,oranges,20
public Purchase questionSampleInClass(Purchase p1, int newX) {
newX=30
Purchase p2= new Purchase("banana", 5, 4.50, 12);
Purchase p3= new Purchase("plum", 6, 5.50, 10);
p1=p2;
p3=this;
p3.setName("pears");
p1.setName("oranges");
p1 = new Purchase("kiwi", 6, 3.00, 3);
System.out.println(this.getName());
System.out.println(p2.getName());
System.out.println(p3..getName());
return p2;
}
public class PurhaseDemoClassProblem {
public static void main(String{} args) {
Purchase oneSale= new Purchase("apples", 4, 3.50, 6);
Purchase twoSale= new Purchase("grapes", 8, 1.99, 4);
Purchase threeSale;
int x=20;
System.out.println(oneSale.getName());
Syste,.out.println(twoSale.getName());
threeSale= oneSale.questionSampleInClass(twoSale, x);
System.out.println(oneSale.getName());
System.out.println(twoSale.getName());
System.out.println(threeSale.getName());
System.out.println(x);
}
}
【问题讨论】:
-
如果您的代码格式正确,我会这样做。您首先要阅读 this Java 教程,解释对象引用。
-
啊第一次使用这个网站,这个更好吗?
-
“请逐行解释,并在值变化时告诉我。” ...你知道谁真正擅长这个吗?你的调试器。
-
@Basco 另外,你能改正你的错别字吗?我至少数了三个。
-
使用调试器。它会帮助你。