12
返回列表 发新帖
楼主: Sky-Tiger

ActionScript for Java developers, Part 1

[复制链接]
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
11#
 楼主| 发表于 2009-2-25 23:32 | 只看该作者
There is also another way of casting in AS3, using the as operator:

var g:int = f as int;

Exceptional coding

Java typically declares exceptions that are thrown:

public void foo() throws MyException
  {
    try {
      // really awesome code
    } catch (Exception e) {
      throw new MyException("AAAUUUGGGHHHH!");
   }
  }

AS3 throws exceptions without declaration:

public function foo():void
  {
    try {
      // really awesome code
    } catch (var e:Error) {
      throw new Error("AAAUUUGGGHHHH!");
   }
  }

Generically speaking

Java has generics and typed collections:

List<FooType> list = new ArrayList<FooType>();
  list.add(new FooType());
  FooType foo = list.get(0);

AS3 ... does not.

But AS3 does have typed arrays through the Vector class:

var vec:Vector.<FooType> = new Vector.<FooType>();
  vec[0] = new FooType();
  var foo:FooType = vec[0];

Some may wonder at the odd angle-bracket syntax of the Vector declaration. I'm not sure of the history, but I have a feeling that AS3 was just trying to achieve readability and angle-bracket parity with Java's generics.
XML

Speaking of angle brackets in code, Java handles XML processing through various libraries (many of them), such as JAXP, JAXB, SAX, Xerces, JDOM, etc.

AS3 has E4X integrated into the language itself for queries, manipulation, and the like.

var myXML:XML = <Grob>gable</Grob>;
  trace(myXML.Grob);
  // outputs 'gable'

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
12#
 楼主| 发表于 2009-2-25 23:32 | 只看该作者
he next level: Class behavior

As you've seen by now, AS3 classes look pretty much like Java classes. You haven't seen them yet, but AS3 interfaces also look eerily similar to their Java counterparts. But looks aren't everything (except with supermodels and wax fruit) and there are a few important distinctions in behavior that are worth investigating.
Constructors and permissive behavior

Java allows the same access permission specifiers (public, protected, private, and package-private) on constructors that are allowed on classes, fields, and methods:


public class FooObject {
    private FooObject() {}
  }
  FooObject foo = new FooObject();
  // Compiler error

In AS3, constructors are always public:

public class FooObject {
    private function FooObject() {}
  }
  // Compiler error

Making a constructor private (in a language that supports it, like Java) is not a typical pattern, although it is helpful in some situations, like creating singletons. If you really want only one of something, then it's a good idea to prevent anyone but the class itself from creating it. A workaround used in AS3 involves throwing exceptions from the constructor when called from outside of your singleton accessor, but it is not quite the same thing.
Interfaces and properties

Java allows properties to be declared on interfaces:

public interface IFoo {
    public int blah = 5;
    public final int VAL = 7;
  }

*Both of these properties are implicitly static and final, even though they lack those keywords. Try it, you'll see.

AS3 does not allow properties on interfaces. Only functions can be declared on interfaces. Note, however, that you can declare properties with set/get functions; just not properties as fields. For example, this works:

public interface IProperties
  {
      function set blah(value:Number):void;   
      function get blah():Number;   
  }

If the get/set example here makes no sense, don't worry. You'll learn more about properties and these functions in the second half of this article.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
13#
 楼主| 发表于 2009-2-25 23:32 | 只看该作者
Abstract, what's that?

Java allows abstract classes:

public abstract class FooObject {
    public abstract void foo();
  }

AS 3 ... does not. There is no concept of "abstract" in AS3.
Until next time ...

That's probably enough comparison to get a busy mind spinning, so I'll stop for now. I hope the examples here have whet your appetite, because there's more to come. In the second half of this article we'll get into the advanced topics of properties, dynamic behavior, and functions, with a similar line-up of code-based comparison and usage commentary from, well, me.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
14#
 楼主| 发表于 2009-2-25 23:33 | 只看该作者
Acknowledgements

Thanks toJames Ward of Adobe for helping with the original presentation that these articles are based upon. Thanks also to Jeff Dyer, an actual language expert at Adobe, for providing a timely technical review and ensuring that I wasn't simply rattling off a bunch of half-baked opinions and outright stinking lies.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
15#
发表于 2009-2-25 23:46 | 只看该作者
不搞as

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表