阿里Java学习路线:阶段 1:Java语言基础-Java面向对象编程:第21章:抽象类与接口应用:课时94:案例分析二(绘图处理)

案例分析二
考虑一个表示绘图的标准,并且可以根据不同的图形来进行绘制;
阿里Java学习路线:阶段 1:Java语言基础-Java面向对象编程:第21章:抽象类与接口应用:课时94:案例分析二(绘图处理)_第1张图片

interface IGraphical { // 定义绘图标准
	public void paint() ; // 绘图
}
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 paint() {
		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 paint() {
		System.out.println("以半径为“" + this.radius + "”绘制圆形。");
	}
}
class Factory {
	public static IGraphical getInstance(String className,double ... args) {
		if ("triangle".equalsIgnoreCase(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".equalsIgnoreCase(className)) {
			return new Circular(args[0]) ;
		} else {
			return null ;
		}
	}
}
public class JavaDemo {
	public static void main(String args[]) {
		IGraphical iga = Factory.getInstance("triangle",1.1,2.2,3.3,4.4,11.11,22.22,33.33,44.44,111.111,222.222,333.333,444.444);
		iga.paint() ;
		IGraphical igb = Factory.getInstance("circular",88.11) ;
		igb.paint() ;
	}
}

你可能感兴趣的:(java,学习,阿里Java学习路线)