New users cannot understand set and get.

Source: Internet
Author: User

I want to write a ball. This class can let external instances out and implement two attributes using the get and set methods. One is the ball radius and the other is the ball color.
I wrote it like this, but when the class is called externally, the ball size cannot be changed when the ball is generated. Where is the error of my get and set methods?

Code

  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  

 

Code

 1. This part is divided into answers
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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.