2009-07-02 13:30
我想写一个球类,该类可让外部实例出来,并用get和set方法实现两个属性,一个是球的半径,一个是球的颜色。 |
代码
1 package
2
3 {
4
5 import flash.display.Sprite;
6
7 public class Ball extends Sprite
8
9 {
10
11 private var _radius:Number;
12
13 private var _color:uint;
14
15
16 public function Ball(){
17
18 Init();
19
20 }
21
22 private function Init():void{
23
24 graphics.beginFill(color);
25
26 graphics.drawCircle(0,0,radius);
27
28 graphics.endFill();
29
30 }
31
32 public function set radius(rad:Number):void{
33
34 _radius = rad;
35
36 }
37
38 public function get radius():Number{
39
40 return _radius;
41
42 }
43
44 public function set color(col:uint):void{
45
46 _color = col;
47
48 }
49
50 public function get color():uint{
51
52 return _color;
53
54 }
55
56 }
57
58 }
59
代码
1 本部分为答案
2
3
4 package
5
6 {
7
8 import flash.display.Sprite;
9
10 public class Ball extends Sprite
11
12 {
13
14 private var _radius:Number;
15
16 private var _color:uint;
17
18 public function Ball(radius:Number = 100,color:uint = 0xff0000){
19
20 _radius = radius;
21
22 _color = color;
23
24 init();
25
26 }
27 private function init():void{
28 graphics.clear();
29
30 graphics.beginFill(_color);
31
32 graphics.drawCircle(0,0,_radius);
33
34 graphics.endFill();
35 }
36
37 public function set radius(rad:Number):void{
38
39 _radius = rad;
40
41 init();
42
43 }
44
45 public function get radius():Number{
46
47 return _radius;
48
49 }
50
51 public function set color(col:uint):void{
52
53 _color = col;
54
55 init();
56
57 }
58
59 public function get color():uint{
60
61 return _color;
62
63 }
64
65 }
66
67 }
68