oop原则:组合大于继承
//继承
class A extends B{
}
//组合,减少耦合性
class A{
public B b:
}
改动前
面向过程写法
//简易计算器
public class TextCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
windowClose1(calculator);
}
//关闭窗口
private static void windowClose1 (Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//计算器类
class Calculator extends Frame{
public Calculator() {
//3个文本框
TextField tF1 = new TextField(10);
TextField tF2 = new TextField(10);
TextField tF3 = new TextField(10);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(tF1,tF2,tF3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
//排序
add(tF1);
add(label);
add(tF2);
add(button);
add(tF3);
pack();
setVisible(true);
}
}
//监听类
class MyCalculatorListener implements ActionListener{
private TextField tF1,tF2,tF3;
public MyCalculatorListener(TextField tF1,TextField tF2,TextField tF3) {
this.tF1 = tF1;
this.tF2 = tF2;
this.tF3 = tF3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(tF1.getText());
int n2 = Integer.parseInt(tF2.getText());
//将这两个值相加后,输入第三个框
tF3.setText(""+(n1+n2));
// 清空
tF1.setText("");
tF2.setText("");
}
}
完全改造为面向对象写法
//简易计算器
public class TextCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
windowClose1(calculator);
}
//关闭窗口
private static void windowClose1 (Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField tF1;
TextField tF2;
TextField tF3;
public void loadFrame() {
//3个文本框
tF1 = new TextField(10);
tF2 = new TextField(10);
tF3 = new TextField(10);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
//排序
add(tF1);
add(label);
add(tF2);
add(button);
add(tF3);
pack();
setVisible(true);
}
}
//监听类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另一个类。
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(calculator.tF1.getText());
int n2 = Integer.parseInt(calculator.tF2.getText());
//将这两个值相加后,输入第三个框
calculator.tF3.setText(""+(n1+n2));
// 清空
calculator.tF1.setText("");
calculator.tF2.setText("");
}
}
内部类
- 更好的包装
//简易计算器
public class TextCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
windowClose1(calculator);
}
//关闭窗口
private static void windowClose1 (Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField tF1;
TextField tF2;
TextField tF3;
public void loadFrame() {
//3个文本框
tF1 = new TextField(10);
tF2 = new TextField(10);
tF3 = new TextField(10);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
//排序
add(tF1);
add(label);
add(tF2);
add(button);
add(tF3);
pack();
setVisible(true);
}
//监听类
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
int n1 = Integer.parseInt(tF1.getText());
int n2 = Integer.parseInt(tF2.getText());
//将这两个值相加后,输入第三个框
tF3.setText(""+(n1+n2));
// 清空
tF1.setText("");
tF2.setText("");
}
}
}