java算法:抽象数据类型ADT

java算法:抽象数据类型ADT

开发有关系数据和处理这些数据的方法的抽象数据模型是用计算机解决问题的过程中必不可少的步骤。

使用抽象数据类型,可以很好的把任何具体的数据结构表示与算法分开,利于研究算法。

抽象数据类型是一种智能通过接口访问的数据类型(值与值上的操作所构成的集合),我们把使用ADT的程序称为客户程序,把指定数据类型的程序称为实现。

抽象数据类型与其他数据类型的主要区别是:对于抽象数据类型,客户程序只能通过接口中提供的操作来访问数据值。接口把所有的数据表示和操作方法的实现完全与客户程序隔离。在Java中,一般不能直接访问数据,而是通过方法访问的。

例1:点的类实现

Java代码 复制代码
  1. public class Point{   
  2.     private double x,y;   
  3.     Point(){   
  4.         x = Math.random();   
  5.         y = Math.random();   
  6.     }   
  7.     Point(double x, double y){   
  8.         this.x = x;   
  9.         this.y = y;   
  10.     }   
  11.     double x(){   
  12.         return x;   
  13.     }   
  14.     double y(){   
  15.         return y;   
  16.     }   
  17.     double r(){   
  18.         return Math.sqrt(x * x + y * y);   
  19.     }   
  20.     double theta(){   
  21.         return Math.atan2(y , x);   
  22.     }   
  23.     double distance(Point p){   
  24.         double dx = x - p.x;   
  25.         double dy = y - p.y;   
  26.         return Math.sqrt(dx * dx + dy * dy);   
  27.     }   
  28.     public String toString(){   
  29.         return "(" + x + " , " + y + ")";   
  30.     }   
  31. }  
public class Point{
	private double x,y;
	Point(){
		x = Math.random();
		y = Math.random();
	}
	Point(double x, double y){
		this.x = x;
		this.y = y;
	}
	double x(){
		return x;
	}
	double y(){
		return y;
	}
	double r(){
		return Math.sqrt(x * x + y * y);
	}
	double theta(){
		return Math.atan2(y , x);
	}
	double distance(Point p){
		double dx = x - p.x;
		double dy = y - p.y;
		return Math.sqrt(dx * dx + dy * dy);
	}
	public String toString(){
		return "(" + x + " , " + y + ")";
	}
}

定义ADT的根本原因:通过使客户不能直接访问数据表示,可以随意地对数据表示进行修改!在这种情况下,使用极坐标来表示点,但客户程序可以不管点是如何实现的而执行相同的运算。

例2:点类(替换实现)

Java代码 复制代码
  1. public class Point2{   
  2.     private double r,theta;   
  3.        
  4.     private static Point2 p2;   
  5.        
  6.     public static Point2 getInstance(){   
  7.         double x = Math.random() * 100;   
  8.         double y = Math.random() * 100;   
  9.         p2 = new Point2(x,y);   
  10.         return p2;   
  11.     }   
  12.        
  13.     public Point2(double x, double y){   
  14.         this.r = Math.sqrt(x * x + y * y);   
  15.         this.theta = Math.atan2(y , x);   
  16.     }   
  17.        
  18.     public double x(){   
  19.         return r * Math.cos(theta);   
  20.     }   
  21.     public double y(){   
  22.         return r * Math.sin(theta);   
  23.     }   
  24.     public double r(){   
  25.         return r;   
  26.     }   
  27.     public double theta(){   
  28.         return theta;   
  29.     }   
  30.     public double distance(Point2 p){   
  31.         double dx = x() - p.x();   
  32.         double dy = y() - p.y();   
  33.         return Math.sqrt(dx * dx + dy * dy);   
  34.     }   
  35.     public String toString(){   
  36.         return "(" + x() + " , " + y() + ")";   
  37.     }   
  38.        
  39. }  
public class Point2{
	private double r,theta;
	
	private static Point2 p2;
	
	public static Point2 getInstance(){
		double x = Math.random() * 100;
		double y = Math.random() * 100;
		p2 = new Point2(x,y);
		return p2;
	}
	
	public Point2(double x, double y){
		this.r = Math.sqrt(x * x + y * y);
		this.theta = Math.atan2(y , x);
	}
	
	public double x(){
		return r * Math.cos(theta);
	}
	public double y(){
		return r * Math.sin(theta);
	}
	public double r(){
		return r;
	}
	public double theta(){
		return theta;
	}
	public double distance(Point2 p){
		double dx = x() - p.x();
		double dy = y() - p.y();
		return Math.sqrt(dx * dx + dy * dy);
	}
	public String toString(){
		return "(" + x() + " , " + y() + ")";
	}
	
}

使用ADT,精确提供返回客户感兴趣的数据方法。而且在实现方法更灵活。

例3:点的ADT接口

Java代码 复制代码
  1. public interface IPoint {   
  2.     double x();   
  3.     double y();   
  4.     double r();   
  5.     double theta();   
  6.     double distance(Point p);   
  7.     public String toString();   
  8. }  
public interface IPoint {
	double x();
	double y();
	double r();
	double theta();
	double distance(Point p);
	public String toString();
}

ADT是作为支持模块化编程的一种有效机制。模块化编程是现代大型软件系统的一种组织方法。ADT提供了灵活的修改,ADT接口明确定义了程序访问的方法。

转载于:https://www.cnblogs.com/wuyida/archive/2012/10/26/6301152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值