String str ="a=1;b=2;c=3"(面向对象)

版权声明:No Rain No Rainbow https://blog.csdn.net/xiangyuenacha/article/details/82759905

考题:
String str =“a=1;b=2;c=3”;
输入a 出 1,输入 b 出 2(备注:使用面向对象)

package com.test.mianshi;

public class TestMain {
	public static void main(String[] args) {
		Test test = new Test("a=1", "b=2", "c=3");
		System.out.println(test.getA());
		System.out.println(test.getB());
	}
}

package com.test.mianshi;

/**
 * String str ="a=1;b=2;c=3";
 * 输入a 出 1,输入 b 出 2
 * @author PC
 *
 */
public class Test {

	private String a;
	private String b;
	private String c;
	
	
	public Test(String str) {
		String[]split =str.split(";");
		this.a=split[0].split("=")[1];
		this.a=split[1].split("=")[1];
		this.a=split[2].split("=")[1];
	}
	
	public Test(String a, String b, String c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}

	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	public String getC() {
		return c;
	}
	public void setC(String c) {
		this.c = c;
	}
	public String get(String symbol){
		String result = null;
		switch(symbol){
		case "a":
			result=getA();
			break;
		case "b":
			result=getB();
			break;
		case "c":
			result=getC();
			break;
		default:
			break;
		}
		return result;
	}
}

猜你喜欢

转载自blog.csdn.net/xiangyuenacha/article/details/82759905