ArcGIS Flex API 中的 Flex 技术(二)--面向对象

简介:

面向对象的三大特性:继承、封装、多态,都可以在Flex开发中找到它们的身影,ArcGIS Flex API充分利用了Flex面向对象的语法机制进行了设计和扩展,前一篇谈到的事件就是继承了flash.events.Event,这篇文章将通过一个典型GeoRSS Demo分析Flex的面向对象。

    最基本的一个类封装:

复制代码
None.gif package com.esri.ags.samples
ExpandedBlockStart.gif
{
InBlock.gif   
InBlock.gif
public class Namespaces
ExpandedSubBlockStart.gif
{
InBlock.gif    
public static const RDF_NS:Namespace = new Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
InBlock.gif    
public static const DC_NS:Namespace = new Namespace("http://purl.org/dc/elements/1.1/");
InBlock.gif    
public static const SY_NS:Namespace = new Namespace("http://purl.org/rss/1.0/modules/syndication/");
InBlock.gif    
public static const CO_NS:Namespace = new Namespace("http://purl.org/rss/1.0/modules/company/");
InBlock.gif    
public static const TI_NS:Namespace = new Namespace("http://purl.org/rss/1.0/modules/textinput/");
InBlock.gif    
public static const RSS_NS:Namespace = new Namespace("http://purl.org/rss/1.0/< /span>");
InBlock.gif    
public static const ATOM_NS:Namespace = new Namespace("http://www.w3.org/2005/Atom");
InBlock.gif    
public static const ATOM_03_NS:Namespace = new Namespace("http://purl.org/atom/ns#");
InBlock.gif    
public static const XHTML_NS:Namespace = new Namespace("http://www.w3.org/1999/xhtml");
InBlock.gif    
public static const CONTENT_NS:Namespace = new Namespace("http://purl.org/rss/1.0/modules/content/");
InBlock.gif    
public static const GEORSS_NS:Namespace = new Namespace("http://www.georss.org/georss");
InBlock.gif    
public static const GEOWGS_NS:Namespace = new Namespace("http://www.w3.org/2003/01/geo/wgs84_pos#");
InBlock.gif    
public static const GEO_NS:Namespace    = new Namespace("http://www.w3.org/2003/01/geo/");
InBlock.gif    
public static const GML_NS:Namespace    = new Namespace("http://www.opengis.net/gml");
InBlock.gif
InBlock.gif    
public function Namespaces(singletonEnforcer:SingletonEnforcer)
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
class SingletonEnforcer
ExpandedBlockStart.gif
{
ExpandedBlockEnd.gif}
复制代码


    首先是Flex有package的概念,易于将类分类存放,并且可以避免命名相同的情况,其次public、class、static、const关键字都是我们非常熟悉的,它们的含义和常见OO语言的相同,代码申明了多个静态常量,存储RSS来源。

    Namespaces 类中有一个构造函数,和Java、C#相同,需要注意的是ActionScript不支持函数重载,最初这一点让大家不是很理解,但要知道,ActionScript和Javascript一样,都遵从ECMAScript 262规范,后面的同名函数会覆盖前面的,从另外一个角度去考虑,在as和js中,函数就是一个对象,同名的对象不可能同时存在于一个命名空间下,这点和 Java、C#有很大区别,所以要解决函数重载问题,要么使用不同名函数,要么通过一个参数去判断调用哪个函数。

    因此我们可以这样来设计多态:

复制代码
None.gif function doWithString(arg: string )
ExpandedBlockStart.gif
{
InBlock.gif    
return arg;
ExpandedBlockEnd.gif}

None.giffunction doWithInteger(arg:
int )
ExpandedBlockStart.gif
{
InBlock.gif    
return arg;
ExpandedBlockEnd.gif}
复制代码


    或者是:

复制代码
None.gif  style="color: #0000ff;">if  (arguments.length  ==   1 )
ExpandedBlockStart.gif
{
InBlock.gif    
if (arguments[0typeof 'String')
ExpandedSubBlockStart.gif    
{
InBlock.gif        doWithString(arg);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
if (arguments[0typeof 'Int')
ExpandedSubBlockStart.gif    
{
InBlock.gif        doWithInteger(arg);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
else   if  (arguments.length  ==   2 )
ExpandedBlockStart.gif
{
ExpandedBlockEnd.gif}
复制代码


    Flex继承采用了prototype原型,经过封装之后我们可以直接使用extends关键字实现常用继承功能:

None.gif public   class  GeoRSSProvider extends EventDispatcher
ExpandedBlockStart.gif
{
ExpandedBlockEnd.gif}


    可以发现,Flex在ECMAScript 262基础上还是做出了不少工作以支持常规面向对象的设计理念,开发者可以很方便的将OO思想应用到Flex之中,Flex里可以发现 Javascript、Java的身影,并加入了自己的一些语法习惯,例如变量类型或实例类型都放置在后面,和常规使用习惯稍有出入。

    再看GeoRSSUtil功能类,提供了一系列静态方法来解析资源中所提供的地理信息:

复制代码
None.gif package com.esri.ags.samples
ExpandedBlockStart.gif
{
InBlock.gif
InBlock.gifimport com.esri.ags.geometry.Geometry;
InBlock.gifimport com.esri.ags.geometry.MapPoint;
InBlock.gifimport com.esri.ags.geometry.Polygon;
InBlock.gif
InBlock.gifimport mx.utils.StringUtil;
InBlock.gif   
InBlock.gif
public class GeoRSSUtil
ExpandedSubBlockStart.gif
{
InBlock.gif    
private static const GEORSS : Namespace = Namespaces.GEORSS_NS;
InBlock.gif    
private static const GEOWGS : Namespace = Namespaces.GEOWGS_NS;
InBlock.gif    
private static const GEO : Namespace = Namespaces.GEO_NS;
InBlock.gif    
private static const GML : Namespace = Namespaces.GML_NS;
InBlock.gif    
InBlock.gif    
public function GeoRSSUtil(singletonEnforcer:SingletonEnforcer)
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static function toGeometry( x : XML ) : Geometry
ExpandedSubBlockStart.gif    
{
InBlock.gif        
const geoLat : String = String(x.GEOWGS::lat );
InBlock.gif        
const geoLon : String = String(x.GEOWGS::long );
InBlock.gif        
if( geoLat && geoLon )
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return new MapPoint( Number( geoLon ), Number( geoLat ));
ExpandedSubBlockEnd.gif        }

InBlock.gif       
InBlock.gif        
const georssPoint : String = String(x.GEORSS::point);
InBlock.gif        
if( georssPoint )
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return parseGeoRSSPoint(georssPoint);
ExpandedSubBlockEnd.gif        }

InBlock.gif       
InBlock.gif        
const pointList : XMLList = x.GEO::point;
InBlock.gif        
if( pointList && pointList.length() > 0 )
ExpandedSubBlockStart.gif        
{
InBlock.gif            
const geoPoint : XML = pointList[0];
InBlock.gif            
const geoPLat : Number = Number(geoPoint.GEO::lat);
InBlock.gif            
const geoPLon : Number = Number(geoPoint.GEO::long);
InBlock.gif            
return new MapPoint( geoPLon, geoPLat );
ExpandedSubBlockEnd.gif        }

InBlock.gif       
InBlock.gif        
const georssPolygon : String = String(x.GEORSS::polygon);
InBlock.gif        
if( georssPolygon )
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return parseGeoRSSPolygon( georssPolygon);
ExpandedSubBlockEnd.gif        }
       
InBlock.gif               
InBlock.gif        
const whereList : XMLList = x.GEORSS::where;
InBlock.gif        
if( whereList && whereList.length() > 0 )
ExpandedSubBlockStart.gif        
{
InBlock.gif            
const pos : String = whereList[0].GML::Point[0].GML::pos[0];
InBlock.gif            
const arr : Array = pos.split(" ");
InBlock.gif            
const gmlLat : Number = Number(arr[0]);
InBlock.gif            
const gmlLon : Number = Number(arr[1]);
InBlock.gif            
return new MapPoint( gmlLon, gmlLat );
ExpandedSubBlockEnd.gif        }
                       
InBlock.gif        
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif   
InBlock.gif    
private static function parseGeoRSSWhere( x : XML ) : Geometry
ExpandedSubBlockStart.gif    
{
InBlock.gif        
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif   
InBlock.gif    
private static function parseGeoRSSPoint( text : String ) : Geometry
ExpandedSubBlockStart.gif    
{
InBlock.gif        
const tokens : Array = StringUtil.trim(text).split(" ");
InBlock.gif        
const lat : Number = Number(tokens[0]);
InBlock.gif        
const lon : Number = Number(tokens[1]);
InBlock.gif        
return new MapPoint( lon, lat);        
ExpandedSubBlockEnd.gif    }

InBlock.gif   
InBlock.gif    
private static function parseGeoRSSPolygon( text : String ) : Geometry
ExpandedSubBlockStart.gif    
{
InBlock.gif        
const path : Array = [];
InBlock.gif        
const tokens : Array = StringUtil.trim(text).split(" ");
InBlock.gif        
for( var i:int=0, j:int=1; j < tokens.length; i+=2, j+=2 )
ExpandedSubBlockStart.gif        
{
InBlock.gif            var lat : Number 
= Number(tokens[i]);
InBlock.gif            var lon : Number 
= Number(tokens[j]);
InBlock.gif            path.push( 
new MapPoint( lon, lat) );  
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return new Polygon([path]);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
class  SingletonEnforcer
ExpandedBlockStart.gif
{   
ExpandedBlockEnd.gif}
复制代码


    package、import、class、public、private、static、const,就像设计一个Java Util类,相比js库如ext、dojo,flex对ECMAScript 262规范脚本封装的更为完美,丝毫感觉不到是在用脚本语言进行编程。parseGeoRSSPoint从GeoRSS资源中获取点状要素,parseGeoRSSPolygon从GeoRSS资源中获取面状要素,从这些代码中我们可以学习到GEORSS、GEOWGS、GEO、GML的解析方法。


本文转自Flyingis博客园博客,原文链接:http://www.cnblogs.com/flyingis/archive/2008/12/09/1350778.html,如需转载请自行联系原作者

相关文章
|
2月前
|
IDE Java API
使用Java Web技术构建RESTful API的实践指南
使用Java Web技术构建RESTful API的实践指南
|
3月前
|
数据采集 传感器 人工智能
大数据关键技术之电商API接口接入数据采集发展趋势
本文从数据采集场景、数据采集系统、数据采集技术方面阐述数据采集的发展趋势。 01 数据采集场景的发展趋势 作为大数据和人工智能工程的源头,数据采集的场景伴随着应用场景的发展而变化,以下是数据采集场景的发展趋势。
|
4月前
|
Java Shell 分布式数据库
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
84 0
|
4月前
|
分布式计算 Java 大数据
【大数据技术Hadoop+Spark】HDFS Shell常用命令及HDFS Java API详解及实战(超详细 附源码)
【大数据技术Hadoop+Spark】HDFS Shell常用命令及HDFS Java API详解及实战(超详细 附源码)
193 0
|
5月前
|
Java API Maven
微服务技术系列教程(27) - SpringCloud- Zuul整合Swagger管理微服务所有API
微服务技术系列教程(27) - SpringCloud- Zuul整合Swagger管理微服务所有API
56 0
|
3月前
|
存储 JSON API
淘宝/天猫商品详情实时数据API技术实现
随着电子商务的蓬勃发展,对于电商平台的商家而言,实时获取商品数据变得至关重要。通过API接口,可以轻松地从电商平台获取这些数据。本文将详细介绍如何使用淘宝/天猫提供的API接口实现商品详情数据的实时获取,并给出具体的代码示例。
|
4月前
|
监控 数据挖掘 API
阿里巴巴API接口技术揭秘:轻松驾驭电商数据,提升业务效率
阿里巴巴API接口技术揭秘:轻松驾驭电商数据,提升业务效率
|
9月前
|
文字识别 API 语音技术
百度语音技术:文字识别转化为语音在线API和PHP-SDK开发文档的学习
百度语音技术:文字识别转化为语音在线API和PHP-SDK开发文档的学习
65 0
|
6月前
|
人工智能 数据可视化 API
ArcGIS API for Python
ArcGIS API for Python
33 0
|
6月前
|
Oracle 关系型数据库 API
C# LIS检验系统源码,接口技术:RESTful API + Http+WCF
LIS检验系统一种专门用于医院化验室的计算机系统,它致力于提高医院化验室的工作效率和检测准确率。LIS系统由多个子系统组成,包括样本管理系统、质控系统、检验结果管理系统、报告管理系统等。体系结构:Client/Server架构 SaaS模式 客户端:WPF+Windows Forms 服务端:C# +.Net 数据库:Oracle 接口技术:RESTful API + Http+WCF