/as3/Astro/AstroVector/src/AstroVector.as (DL, SVN)


  1. package
  2. {
  3. import __AS3__.vec.Vector;
  4.  
  5. import flash.display.Sprite;
  6. import flash.text.TextField;
  7.  
  8. [SWF(width=800, height=600, backgroundColor=0xffffff)]
  9. public class AstroVector extends Sprite
  10. {
  11. public function AstroVector()
  12. {
  13. _info = new TextField();
  14. _info.width = 800;
  15. _info.height = 600;
  16. addChild(_info);
  17.  
  18. // Primitive Vector
  19. {
  20. var pv:Vector.<int> = new Vector.<int>();
  21.  
  22. pv.push(1);
  23. pv.push(2);
  24. pv.push(true); // is correct.
  25. pv.push(false); // Boolean will cast to int.
  26. pv.push('hoge'); // String too.
  27.  
  28. trace(String(pv)); // 1,2,1,0,0
  29. }
  30.  
  31. // Custom Class Vector
  32. {
  33. var a:* = new A();
  34. var b:* = new B();
  35. var c:* = new C();
  36.  
  37. var v:Vector.<A> = new Vector.<A>();
  38.  
  39. v.push(a);
  40. v.push(b);
  41.  
  42. try {
  43. v.push(c); // is error. C can't cast to A.
  44. // but null is pushed instead.
  45. }
  46. catch (e:*) {
  47. trace(e.toString()); // TypeError
  48. }
  49.  
  50. trace(String(v.length)); // 3. It isn't 2.
  51. trace(String(v)); // A, B, null
  52. }
  53.  
  54. // Cast
  55. {
  56. // Array to Vector
  57. var atv:Vector.<A> = Vector.<A>([new A(), new B()]);
  58.  
  59. trace(String(atv.length)) // 2
  60. trace(String(atv)); // A, B
  61.  
  62. // Vector to Array
  63. // is impossible...?
  64. }
  65.  
  66. // Length
  67. {
  68. var vl:Vector.<int> = new Vector.<int>(2);
  69.  
  70. vl[0] = 1; // OK
  71. vl[1] = 2; // OK
  72.  
  73. try {
  74. vl[3] = 4; // Bad. Out of range.
  75. // Index must be (0 <= index <= length + 1).
  76. }
  77. catch (e2:*) {
  78. trace(e2.toString()); // RangeError
  79. }
  80.  
  81. vl[2] = 3; // OK
  82. vl[3] = 4; // OK. Because now length+1 is 3.
  83. }
  84.  
  85. // Fixed
  86. {
  87. var vf:Vector.<int> = new Vector.<int>(2);
  88.  
  89. vf.fixed = true;
  90.  
  91. vf[0] = 1; // OK
  92. vf[1] = 2; // OK
  93.  
  94. try {
  95. vf[2] = 3; // Bad. Length is fixed.
  96. }
  97. catch (e3:*) {
  98. trace(e3.toString()); // RangeError
  99. }
  100.  
  101. try {
  102. vf.push(3); // Bad. Length is fixed.
  103. }
  104. catch (e4:*) {
  105. trace(e4.toString()); // RangeError
  106. }
  107. }
  108. }
  109.  
  110. private var _info:TextField;
  111.  
  112. private function trace(s:String):void
  113. {
  114. _info.appendText(s + "\n");
  115. }
  116. }
  117. }
  118.  
  119. class A
  120. {
  121. public function toString():String
  122. {
  123. return 'A';
  124. }
  125. }
  126.  
  127. class B extends A
  128. {
  129. public override function toString():String
  130. {
  131. return 'B';
  132. }
  133. }
  134.  
  135. class C
  136. {
  137. public function toString():String
  138. {
  139. return 'C';
  140. }
  141. }
  142.