class DirectionKey {//定义一个火星车的对象
private String up ;//向上 W (y+1)
private String down;//向下 S (y-1)
private String left;//向左 A(x-1)
private String right;//向右 D(x+1)
private int x;
private int y;
// public void setX(int x) {
// this.x = x;
// }
// public int getX() {
// return x;
// }
// public void setY(int y) {
// this.y = y;
// }
// public int getY() {
// return y;
// }
public void setUp(String up) {
if ("W".equals(up)){
this.y = ++y;
System.out.println(y+";"+x);
}
}
public String getUp() {
return up;
}
public void setDown(String down) {
if ("S".equals(down)){
this.y = --y;
System.out.println(y+";"+x);
}
}
public String getDown() {
return down;
}
public void setLeft(String left) {
if ("A".equals(left)){
this.x = --x;
System.out.println(y+";"+x);
}
}
public String getLeft() {
return left;
}
public void setRight(String right) {
if ("D".equals(right)){
this.x = ++x;
System.out.println(y+";"+x);
}
}
public String getRight() {
return right;
}
// public void up(String up) {
// System.out.println(this.x+";"+this.y);
// }
}
// public void up() {
// // int a =
// // System.out.println(x+";"+y);
// // }
// // class coordinate {//坐标
// // private int x;
// private int y;
// public void setX(int x) {
// this.x = x;
// }
// public int getX() {
// return get;
// }
// public void setY(int y) {
// // this.y = y;
// // }
// // public int getY() {
// // return get;
// }
// }
public class Demo {
public static void main(String args[]) {
DirectionKey directionkey = new DirectionKey();
directionkey.setUp("W");
directionkey.setDown("S");
directionkey.setLeft("A");
directionkey.setRight("D");
// directionkey.up();
// directionkey.setUp("W");
// // directionkey.setX(1);
// // directionkey.setY(0);
// directionkey.up("2");
}
}
//字符串.equals( ) 字符串的比较
结果:输入W y+1......
参数: String w String S String A String D int x int y
private私有
set方法 4个if方法