接口类

  1. /**
  2. * 接口:形状
  3. */
  4. public interface Shape {
  5. /**
  6. * 获取体积
  7. *
  8. * @return 形状的体积
  9. */
  10. int getArea();
  11. /**
  12. * 获取名称
  13. */
  14. String getName();
  15. }

实现类 Rectangle

  1. /**
  2. * 矩形
  3. */
  4. public class Rectangle implements Shape {
  5. /** TAG */
  6. private static final String TAG = "矩形";
  7. /** 宽 */
  8. private int width;
  9. /** 长 */
  10. private int length;
  11. public Rectangle(int width, int length) {
  12. this.width = width;
  13. this.length = length;
  14. }
  15. @Override
  16. public int getArea() {
  17. return width * length;
  18. }
  19. @Override
  20. public String getName() {
  21. return TAG;
  22. }
  23. }

实现类 Square

  1. /**
  2. * 正方形
  3. */
  4. public class Square extends Rectangle {
  5. /** TAG */
  6. private static final String TAG = "正方形";
  7. public Square(int length) {
  8. super(length, length);
  9. }
  10. @Override
  11. public String getName() {
  12. return TAG;
  13. }
  14. }