标题翻译
Replace an Object by another type of Object in a Main[JAVA]
问题
以下是翻译好的代码部分:
Main.java:
public class Main {
public static void main(String[] args) {
Controller newcontroller = Controller.create();
newcontroller.change();
}
}
Controller.java:
public class Controller {
public static Controller create() {
return new North(0, 0);
}
public void change() {
}
}
North.java:
public class North extends Controller {
int x;
int y;
public North(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void change() {
super.change();
// 现在我需要将 newcontroller 从 North(x, y) 替换为 South(x, y)
super.newcontroller = new South(this.x, this.y);
}
}
South.java 与 North.java 类似,但它会再次变为 North。如何实现这一点?或者我完全错误地进行了操作?只允许使用线性编程,不能使用 if、switch、for、while 等结构。
英文翻译
i try to to change a Object to another type of Object in my main. Here is some code to see what i actually mean.
Main.java
public class Main {
public static void main(String[] args) {
Controller newcontroller = Controller.create();
newcontroller.change();
}
}
Controler.java:
public class Controller{
public static Controller create() {
return new North(0,0);
}
public void change() {
}
}
North.java:
public class North extends Controller{
int x;
int y;
public North(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void change() {
super.change();
//Now i have to replace the newcontroller North(x,y) by South(x,y)
super.newcontroller = new South(this.x, this.y)
}
}
South.java is just like the North one but it changes to North again.
How can i do that? Or am i doing it completly on a false way?
its not allowed to use if, switch, for, while, ... Only linear programming
答案1
得分: 0
一个简单的方法是在Controller类内部有一个布尔标志,检查该标志进行更改,但是这样做是不合适的,因为它不遵循面向对象编程原则。在您上面的代码中,看起来您正在创建一个返回控制器的工厂。
public class ControllerFactory {
public static CreateController(boolean isNorth) {
return isNorth ? new North() : new South();
}
}
无论在哪里实例化Controller,都有一个布尔变量来保存控制器的当前状态。
boolean isNorth = true;
isNorth = !isNorth;
controller = ControllerFactory.CreateController(isNorth);
英文翻译
An easy way to do this is to have a boolean flag inside the Controller class and check the flag to change, but this should not be done because it doesn't follow OOP principles. In your code above it looks like you are creating a Factory that returns a controller.
public class ControllerFactory {
public static CreateController(boolean isNorth) {
return isNorth ? new North() : new South();
}
}
And wherever you instantiate Controller have a boolean that holds the current state of the controller.
boolean isNorth = true;
isNorth = !isNorth;
controller = ControllerFactory.CreateController(isNorth);
答案2
得分: 0
需要进行一些小的修改。
public class Main {
public static void main(String[] args) {
Controller ctl = Controller.create();
ctl.change();
}
}
public class Controller{
public static Controller create() {
return new North(0,0);
}
public Controller change() {
return null;
}
}
public class North extends Controller{
int x;
int y;
public North(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public Controller change() {
return new South(this.x, this.y);
}
}
public class South extends Controller {
int x;
int y;
public South(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public Controller change() {
return new North(this.x, this.y);
}
}
英文翻译
You need a little modification.
public class Main {
public static void main(String[] args) {
Controller ctl = Controller.create();
ctl.change();
}
}
public class Controller{
public static Controller create() {
return new North(0,0);
}
public Controller change() {
return null;
}
}
public class North extends Controller{
int x;
int y;
public North(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public Controller change() {
return new South(this.x, this.y);
}
}
public class South extends Controller {
int x;
int y;
public South(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public Controller change() {
return new North(this.x, this.y);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论