[英]The use of a reference variable in this java code
Why is the reference variable ‚Input inside' necessary?为什么需要引用变量“Input inside”? How does the ‚Input inside' work here?
“输入内部”在这里是如何工作的?
What does the code: The code adds 5 to the x-value (in this case 4) and uses the square of the new value.代码的作用:代码将 x 值加 5(在本例中为 4)并使用新值的平方。 So (4+5)^2.
所以 (4+5)^2。
Thank you.谢谢你。
Code:代码:
abstract class Input {
protected Input inside;
public Input() {
}
public Input(Input inside) {
this.inside = inside;
}
public int calculate(int value) {
if(inside == null) {
return value;
}
return inside.calculate(value);
}
public static void main(String[] args) {
Input chaincalculate = new Square(new AddFive());
int x;
x = 4;
System.out.println("The value is: " + (int) chaincalculate.calculate(x));
}
}
class AddFive extends Input {
public AddFive() {
}
public AddFive(Input inside) {
super(inside);
}
public int calculate(int value) {
value = super.calculate(value);
return value+5;
}
}
class Square extends Input {
public Square() {
}
public Square(Input inside) {
super(inside);
}
public int calculate(int value) {
value = super.calculate(value);
return value * value;
}
}
}
All the classes extending the abstract class Input , have their own calculate methods , and since, these classes, would want to use the abstract class Calculate method , the reference variable input inside is necessary to invoke/reference the calculate method residing in Input abstract class , to support the method chaining implemented in the code所有扩展抽象 class Input的类,都有自己的计算方法,并且由于这些类想要使用抽象 class 计算方法,因此内部的引用变量输入对于调用/引用位于输入抽象 ZA2F2ED4F8EBC2AB61DZ4C2中的计算方法是必要的, 以支持代码中实现的方法链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.