欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Shape接口的应用

程序员文章站 2022-03-26 16:45:11
/*实现智能识别器,可针对圆形、矩形、三角形、梯形不同形状,提供如下服务:a. 识别形状的面积;b. 输出形状属性信息:类型和各种参数,如:梯形,上底:20,下底:30,高:10)— 考虑toString()*/继承实现abstract class Shape {private String type;public String getType() { return type; }public Shape(String x){ type = x; }//int getArea()...

/*实现智能识别器,可针对圆形、矩形、三角形、梯形不同形状,提供如下服务:
a. 识别形状的面积;
b. 输出形状属性信息:类型和各种参数,如:梯形,上底:20,下底:30,高:10)— 考虑toString()
*/

继承实现

abstract class Shape {
	private String type;
	public String getType() { return type; }
	public Shape(String x){ type = x; }
//	int getArea() {return 0;} //此处仅表明Shape具有获取面积的方法,且该方法无参
	public abstract int getArea(); //abstract的使用
}
class Circle extends Shape{
	private int r;
	public Circle(String x,int y){
		super(x);
		r=y;
	}
	public int getArea() { return 3*r*r; } //无参,迫使对象只能使用自己的属性并进行计算
	public String toString() {
		return "半径:"+r;
	}
}
class Rectangle extends Shape{
	private int w,h;
	public Rectangle(String x,int y,int z){
		super(x);
		w=y; h=z;
	}
	public int getArea() { return w*h; }
	public String toString() {
		return "宽:"+w+" 高:"+h;
	}
}
class Triangle extends Shape{
	private int d,h;
	public Triangle(String x,int y,int z){
		super(x);
		d=y; h=z;
	}
	public int getArea() { return d*h/2; }
	public String toString() {
		return "底:"+d+" 高:"+h;
	}
}
class Trapezoid extends Shape{
	private int a,b,h;
	public Trapezoid(String x,int y,int z,int w){
		super(x);
		a=y; b=z; h=w;
	}
	public int getArea() { return (a+b)*h/2; }
	public String toString() {
		return "上底:"+a+" 下底:"+b+" 高:"+h;
	}
}
class Identifier{
	static void identify(Shape s) {
		System.out.println("形状:"+s.getType()
		+" 面积:"+s.getArea()+" 属性:"+s);
	}
	
}
//框架程序,必须基于超类编程,特色:运行时可插装不同子类对象
class App_shape{ //识别器
	public static void main(String [] args) {
		Shape [] a = {new Circle("圆形",10),				      
				      new Rectangle("矩形",9,9),
				      new Triangle("三角形",4,5),
				      new Trapezoid("梯形",2,4,10)};
		for(int i=0;i<a.length;i++)
			Identifier.identify(a[i]); //实施运行时插装不同子类对象
	}
}

Java Shape接口的应用

//本例掌握:

  1. 设计策略:
    按照需求:实现形状类型、面积,即要有属性type、计算面积的方法getArea()
  2. abstract的使用:
    有抽象方法的类必须是抽象类
    抽象类没有方法体
    抽象方法必须被子类重写,否则,该子类必须是抽象类(即无法创建对象)
  3. 框架程序
    基于超类编程,以便运行时可以插装不同子类对象

接口实现

interface Shape{
	String getType();
	int getArea(); 
}
class Circle implements Shape{
	private int r;
	private String type;
	public String getType() { return type;} //必须有public
	public Circle(int x) { type="圆形"; r=x;}
	public int getArea() { return 3*r*r;} //必须有public
	public String toString() { return " 半径:"+r;}
}
class Rectangle implements Shape{
	private int w,h;
	private String type;
	public String getType() { return type;} //必须有public
	public Rectangle(int x,int y) { type="矩形"; w=x; h=y;}
	public int getArea() { return w*h;} //必须有public
	public String toString() { return " 宽:"+w+"高:"+h;}
}
class Triangle implements Shape{
	private int d,h;
	private String type;
	public String getType() { return type;} //必须有public
	public Triangle(int x,int y) { type="三角形"; d=x; h=y;}
	public int getArea() { return d*h/2;} //必须有public
	public String toString() { return " 底:"+d+"高:"+h;}
}
class Trapezoid implements Shape{
	private int a,b,h;
	private String type;
	public String getType() { return type;} //必须有public
	public Trapezoid(int x,int y,int z) { type="梯形"; a=x; b=y; h=z;}
	public int getArea() { return (a+b)*h/2;} //必须有public
	public String toString() { return " 上底:"+a+"下底:"+b+"高:"+h;}
}
class Identifier{
	public static void Identify(Shape s) {
		System.out.println("形状:"+s.getType()+" 面积:"+s.getArea()+s);
	}
}
class App{
	public static void main(String [] args) {
		Shape [] s = {new Circle(3),
				      new Rectangle(5,6),
				      new Triangle(5,8),
				      new Trapezoid(9,9,2)};
		for(int i=0;i<s.length;i++)
			Identifier.Identify(s[i]);
	}
}

Java Shape接口的应用

本文地址:https://blog.csdn.net/m0_46527503/article/details/109564227