美文网首页
抽象类与接口应用--综合案例

抽象类与接口应用--综合案例

作者: 曾梦想仗剑天涯 | 来源:发表于2020-11-02 15:20 被阅读0次

获取类信息

定义一个ClassName接口,接口中只有一个抽象方法getClassName();设计一个类Company,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名称;编写应用程序使用Company类

interface IClassName {
  public abstract String getClassName();
}
class Company implements IClassName {
  public String getClassName() {
    return "Company";
  }
}
public class JavaDemo {
  public static void main(String [] args) {
    IClassName ica = new Company();
    System.out.println(ica.getClassName());
  }
}

绘图处理

考虑一个表示绘图的标准,并且可以根据不同的图形来进行绘制


此图来源于李兴华老师
interface IGraphical {
  public abstract void print();
}
class Point {
  private double x;
  private double y;
  public Point(double x, double y) {
    this.x = x;
    this.y = y;
  }
  public double getX() {
    return this.x;
  }
  public double getY() {
    return this.y;
  }
}
class Triangle implements IGraphical {
  private Point [] x;
  private Point [] y;
  private Point [] z;
  public Triangle(Point [] x, Point [] y, Point [] z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  public void print() {
    System.out.println("第一条边的起止坐标" + "[" + this.x[0].getX() + "," + this.x[0].getY() + "]" + "至" + "[" + this.x[1].getX() + "," + this.x[1].getY() + "]");
    System.out.println("第二条边的起止坐标" + "[" + this.y[0].getX() + "," + this.y[0].getY() + "]" + "至" + "[" + this.y[1].getX() + "," + this.y[1].getY() + "]");
    System.out.println("第三条边的起止坐标" + "[" + this.z[0].getX() + "," + this.z[0].getY() + "]" + "至" + "[" + this.z[1].getX() + "," + this.z[1].getY() + "]");
  }  
}
class Circular implements IGraphical {
  private double radius;
  public Circular(double radius) {
    this.radius = radius;
  }
  public void print() {
    System.out.println("以半径"+ this.radius +"绘制圆形");
  }
}
class Factory {
  public static IGraphical getInstance(String className, double ...args) {
    if("Triangle".equals(className)) {
      return new Triangle(
        new Point [] {
        new Point(args[0], args[1]), new Point(args[2], args[3])
        },
        new Point [] {
        new Point(args[4], args[5]), new Point(args[6], args[7])
        },
        new Point [] {
        new Point(args[8], args[9]), new Point(args[10], args[11])
        }
      );
    } else if("Circular".equals(className)) {
        return new Circular(args[0]);
    } else {
        return null;
    }
  } 
}
public class JavaDemo {
  public static void main(String [] args) {
    IGraphical iga = Factory.getInstance("Triangle", 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0);
    iga.print();
    IGraphical igb = Factory.getInstance("Circular", 5.0);
    igb.print();
  }
}

图形

定义类Shape,用来表示一般的二维图形,Shape具有抽象方法area和pserimeter,分别用来计算形状的面积和周长,试定义一些二维形状(如矩形、三角形、圆形、椭圆形等),这些类均为Shape的子类。

abstract class AbstractShape {
  public abstract double area();
  public abstract double pserimeter();
}
class Circular extends AbstractShape {
  private double radius;
  public Circular(double radius) {
    this.radius = radius;
  }
  public double area() {
    return 3.1415926 * this.radius * this.radius;
  }
  public double pserimeter() {
    return 2 * 3.1415926 * this.radius;
  }
}
class Rectangle extends AbstractShape {
  private double length;
  private double width;
  public Rectangle(double length, double width) {
    this.length = length;
    this.width = width;
  }
  public double area() {
    return this.length * this.width;
  }
  public double pserimeter() {
    return 2 * (this.length + this.width);
  }
}
class Factory {
  public static AbstractShape getInstance(String className, double ...args) {
    if("Circular".equals(className)) {
      return new Circular(args[0]);
    } else if("Rectangle".equals(className)) {
      return new Rectangle(args[0], args[1]);
    } else {
      return null;
    }
  }
}
public class JavaDemo {
  public static void main(String [] args) {
    AbstractShape shapeA = Factory.getInstance("Circular", 5);
    System.out.println("圆的面积:" + shapeA.area());
    System.out.println("圆的周长:" + shapeA.pserimeter());
    AbstractShape shapeB = Factory.getInstance("Rectangle", 5, 7);
    System.out.println("矩形的面积:" + shapeB.area());
    System.out.println("矩形的周长" + shapeB.pserimeter());
  }
}

相关文章

网友评论

      本文标题:抽象类与接口应用--综合案例

      本文链接:https://www.haomeiwen.com/subject/pyjmvktx.html