1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class Ocp{ public status void main(String[] args){ GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); } } class GraphicEditor{ public void drawShape(Shape shape){ shape.draw(); } }
abstract class Shape{ int m_type; public abstract void draw(); } class Rectangle extends Shape{ Rectangle(){ super.m_type = 1; } @Override public void draw(){ System.out.println("绘制矩形") } } class Circle extends Shape{ Circle(){ super.m_type = 2; } @Override public void draw(){ System.out.println("绘制圆形") } }
|