OOP原则:组合大于继承!
//继承
class A expends B{
}
//组合
class A{
public B b;
}
简易计算器的实现:
目录
实现结果:
1. 程序代码(简单粗暴版):面向过程的写法
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField field1 = new TextField(10);
TextField field2= new TextField(10);
TextField field3 = new TextField(20); //最大容纳字符数
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(field