WPF应用
代码
private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.Parse(textBox1.Text),int.Parse(textBox3.Text),textBox2.Text); if (sa.answer == int.Parse(textBox4.Text)) { MessageBox.Show("回答正确"); } else { MessageBox.Show("回答错误"); }
interface chapter { int answer { get; } } class calculate:chapter { private int x1; private int x2; private string Operator; public int result; public calculate(int x1, int x2, string Operator) { this.x1 = x1; this.x2 = x2; this.Operator = Operator; } public int answer { get { if (Operator == "+") { result = x1 + x2; } if (Operator == "-") { result = x1 - x2; } if (Operator == "*") { result = x1 * x2; } if (Operator == "/") { result = x1 / x2; } return result; } } } }
总结:
在不同情况下运用封装的语句很容易,困难的是对设计模式的理解和运用现在正在思考如何运用。。