zoukankan      html  css  js  c++  java
  • 关于set和get,新手想不明白的事情

    2009-07-02 13:30

    我想写一个球类,该类可让外部实例出来,并用get和set方法实现两个属性,一个是球的半径,一个是球的颜色。
    我是这样写的,可是在外部调用该类,生成小球时,并不能改变球的大小,请问,我的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
  • 相关阅读:
    CS224n, lec 10, NMT & Seq2Seq Attn
    CS231n笔记 Lecture 11, Detection and Segmentation
    CS231n笔记 Lecture 10, Recurrent Neural Networks
    CS231n笔记 Lecture 9, CNN Architectures
    CS231n笔记 Lecture 8, Deep Learning Software
    CS231n笔记 Lecture 7, Training Neural Networks, Part 2
    pytorch坑点排雷
    Sorry, Ubuntu 17.10 has experienced an internal error
    VSCode配置python插件
    tmux配置与使用
  • 原文地址:https://www.cnblogs.com/crkay/p/1747859.html
Copyright © 2011-2022 走看看