【第二十一章】抽象类与接口的案例分析
一.获取类信息:
抽象类和接口是Java里面最为核心的概念,也是所有设计模式的综合体现,包括在日后学习的过程中也会接触到许多的系统提供的接口和抽象类。
案例一:定义一个ClassName接口,接口中只有一个抽象方法getClassName();设计一个类Company,该类实现接口ClassName中的方法getClassName(),功能是获取该类的类名称;编写应用程序使用Company。
interface IClassName{
public String getClassName();
}
class Company implements IClassName{
public String getClassName(){
return"Company";
}
}
public class tsy
{
public static void main(String args[]) {
IClassName cla = new Company();
System.out.println(cla.getClassName());
}
}
二.绘图处理
考虑一个表示绘图的标准,并且可以根据不同的图形来进行绘制。
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 = x;
}
public double getY(){
return this.y = 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 Circle implements IGraphical{
private double radius;
public Circle(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("Circle".equalsIgnoreCase(className)){
return new Circle(args[0]);
}else {
return null;
}
}
}
public class tsy
{
public static void main(String args[]) {
IGraphical iga = Factory.getInstance("triangle",1.2,1.5,5.1,5.6,20.1,20.6,20.4,31.0,1.5,1.8,2.3,5.4);
iga.paint();
IGraphical ise = Factory.getInstance("circle",2.0);
ise.paint();
}
}
三.案例三(图形):
定义类Shape,用来表示一般二维图形。Shape具有抽象方法area和perimeter,分别用来计算形状的面积和周长。试定义一些二维形状类(如矩形、三角形、圆形等),这些均为Shape的子类。
abstract class AbstractShape{
public abstract double area();
public abstract double perimeter();
}
class Circle extends AbstractShape
{
private double radius;
public Circle(double radius){
this.radius = radius;
}
public double area(){
return 3.14*this.radius*this.radius;
}
public double perimeter(){
return 2*3.14*this.radius;
}
}
class Rectangle extends AbstractShape
{
private double length,width;
public Rectangle(double length,double width){
this.length = length;
this.width = width;
}
public double area(){
return this.length*this.width;
}
public double perimeter(){
return 2*(this.length+this.width);
}
}
class Factory
{
public static AbstractShape getInstance(String className,double ... args){
if("circle".equalsIgnoreCase(className)){
return new Circle(args[0]);
}else if("rectangle".equalsIgnoreCase(className)){
return new Rectangle(args[0],args[1]);
}
return null;
}
}
public class tsy
{
public static void main(String args[]) {
AbstractShape cir = Factory.getInstance("circle",1.0);
System.out.println("AREA:"+cir.area()+"、PERMETER:"+cir.perimeter());
}
}
使用工厂设计模式完全隐藏了实现的子类。