Apache flex 我写AS3的方式对吗?

Apache flex 我写AS3的方式对吗?,apache-flex,flash,actionscript-3,oop,Apache Flex,Flash,Actionscript 3,Oop,我对flash和ActionScript3非常陌生。我已经读了很多关于它的书,这也是我第一次接触面向对象编程 到目前为止,我创建了一个带有登录按钮的应用程序,仅此而已。然而,我想知道我做错了什么,或者应该做不同的事情(或者更好)。我正在使用AdobeFlexBuilder3 主actionscript文件是Client2.as: package { //import required libraries import flash.display.Sprite; //se

我对flash和ActionScript3非常陌生。我已经读了很多关于它的书,这也是我第一次接触面向对象编程

到目前为止,我创建了一个带有登录按钮的应用程序,仅此而已。然而,我想知道我做错了什么,或者应该做不同的事情(或者更好)。我正在使用AdobeFlexBuilder3

主actionscript文件是Client2.as:

package
{
    //import required libraries
    import flash.display.Sprite;

    //set project properties
    [SWF(width="800", height="600", frameRate="31", backgroundColor="#C0C0C0")]

    //launch main class
    public class Client2 extends Sprite
    {   
        public function Client2() { //the constructor
            trace("Client launched.");
            var loginGui:LoginInterface = new LoginInterface(); //load the login interface object
            loginGui.init(); //initialize the login interface (load it)
            addChild(loginGui); //add login gui to the display tree
        }
    }
}
它正在加载登录界面对象。这是件好事吗?我这样做对吗

然后是LoginInterface.as类文件:

package
{
    //import required libraries
    import flash.display.Sprite;

    //the LoginInterface class
    public class LoginInterface extends Sprite
    {
        public function LoginInterface() //the constructor
        {
            trace("LoginInterface object loaded.");
        }

        public function init():void //initialize the login interface (load it)
        {
            trace("LoginInterface init method was called.");
            var loginButton:CustomButton = new CustomButton(300, 300, 100, 30, 3, 18, "Login!"); //create a new custom button
            addChild(loginButton); //add the custom button to the display tree
        }
    }
}
那怎么办?有什么评论吗?为了简化简单按钮的创建,我创建了另一个名为CustomButton.as的类文件-->

对此有何评论?我确信我做了很多错事,也许我没有真正理解整个面向对象的东西?另外,我对我在类声明之后使用“extends…”的方式有一种不好的感觉,主要是因为我只是一直在使用Sprite,并不真正理解它的作用(在互联网上也很难找到)。我不确定的另一件事是AS3中变量的命名。我真的应该使用诸如xLoc或ILabloffset之类的名称吗?我想我的变量命名至少不是很一致

我希望有人能给我一个更好的轨道比我现在的,因为我相信,我应该改进我的AS3编码,然后我继续在这个野兽的工作

非常感谢。

我的意见:

名为Client2的类可能是一个错误的命名选择。客户2没有告诉我太多。一年后它能告诉你多少

在CustomButton中,初始化在构造函数中进行。在LoginInterface中,使用类的实例需要显式调用init()。容易忘记和不必要。除非有充分的理由不这样做,否则从构造函数调用init

iLabelOffset是什么意思?最好在参数列表中使用不太容易混淆的名称

CustomButton构造函数的参数列表相当长。没有必要在x和y方向通过。Sprite已经有了一个x和y属性,所以将所有内容都放回零偏移,并在构建CustomButton后操纵其x和y属性

对CuutButt构造函数的其余参数,考虑重新排序它们,以便可以提供默认参数(只能在参数列表的末尾)。标签偏移和字体大小似乎是很好的候选

通过删除重复的代码来保持函数大小较小。创建一个函数来创建在其参数中采用颜色的按钮状态精灵(或者更好的是,将此功能移动到一个新类型的精灵派生类中),并添加一个createLabel函数,以便可以将该代码移出构造函数。如果您试图保持函数的小尺寸,代码将更易于阅读和维护。这也意味着你必须写更少的评论;-)

我的意见:

名为Client2的类可能是一个错误的命名选择。客户2没有告诉我太多。一年后它能告诉你多少

在CustomButton中,初始化在构造函数中进行。在LoginInterface中,使用类的实例需要显式调用init()。容易忘记和不必要。除非有充分的理由不这样做,否则从构造函数调用init

iLabelOffset是什么意思?最好在参数列表中使用不太容易混淆的名称

CustomButton构造函数的参数列表相当长。没有必要在x和y方向通过。Sprite已经有了一个x和y属性,所以将所有内容都放回零偏移,并在构建CustomButton后操纵其x和y属性

对CuutButt构造函数的其余参数,考虑重新排序它们,以便可以提供默认参数(只能在参数列表的末尾)。标签偏移和字体大小似乎是很好的候选


通过删除重复的代码来保持函数大小较小。创建一个函数来创建在其参数中采用颜色的按钮状态精灵(或者更好的是,将此功能移动到一个新类型的精灵派生类中),并添加一个createLabel函数,以便可以将该代码移出构造函数。如果您试图保持函数的小尺寸,代码将更易于阅读和维护。这也意味着你必须写更少的评论;-)

挥金如土的人一针见血。这些无疑是我在查看您的代码时提出的问题。他提到的东西不一定是Actionscript问题(这个问题不是很合适的词,也许是“需要注意的领域”),这些问题是所有编程语言的通用问题。例如,描述性命名非常重要

很少有书关注编程的这一方面,甚至很少有书做得很好。如果你想在这一领域获得更大的发展,我强烈建议你选择以下两本书,(即使你不想,我也会推荐:)


这两本书都是每个程序员都应该读的,所以请看看。从Actionscript的角度来看,您编写的代码很好,但这只是语法。重要的是要注意,除非你真的编写代码,否则这些技能永远不会发展,所以一定要“继续研究这头野兽”,其他人也会照做。

斯彭德一针见血。这些无疑是我在查看您的代码时提出的问题。他提到的东西不一定是Actionscript问题(这个问题不是很合适的词,也许是“需要注意的领域”),这些问题是所有编程语言的通用问题。例如,描述性命名非常重要

很少有书关注编程的这一方面,甚至很少有书做得很好。如果你愿意的话,我强烈建议你选择以下两本书
package
{
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    public class CustomButton extends Sprite
    {
        public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String)
        {
            //create new simple button instance
            var myButton:SimpleButton = new SimpleButton();
            //create the look of the states
            var normal:Sprite = new Sprite();
            normal.graphics.lineStyle(1, 0x000000);
            normal.graphics.beginFill(0x6D7B8D);
            normal.graphics.drawRect(xLoc, yLoc, width, height);
            //the mouseover sprite
            var over:Sprite = new Sprite();
            over.graphics.lineStyle(1, 0x000000);
            over.graphics.beginFill(0x616D7E);
            over.graphics.drawRect(xLoc, yLoc, width, height);
            // assign the sprites
            myButton.upState = normal;
            myButton.downState = normal;
            myButton.hitTestState = normal;
            myButton.overState = over;
            //add the button to the display tree
            addChild(myButton);

            //create button label
            var tText:TextField = new TextField();
            tText.mouseEnabled = false,
            tText.x = xLoc;
            tText.y = yLoc + iLabelOffset;
            tText.width = width;
            tText.selectable = false
            var Format:TextFormat = new TextFormat();
            Format.font = "Arial";
            Format.color = 0x000000;
            Format.size = fontsize;
            Format.bold = false;
            Format.align = TextFormatAlign.CENTER;
            tText.defaultTextFormat = Format;
            tText.text = label;
            addChild(tText)
        }
    }
}
public class CustomButton extends Sprite
{
    private var myButton:SimpleButton;
    private var normal:Sprite;
    private var over:Sprite;
    // etc ... 

    public function CustomButton(xLoc:int, yLoc:int, width:int, height:int, iLabelOffset:int, fontsize:uint, label:String)
    {
            //create new simple button instance
            myButton = new SimpleButton();

            //create the look of the states
            normal = new Sprite();
            normal.graphics.lineStyle(1, 0x000000);
            normal.graphics.beginFill(0x6D7B8D);
            normal.graphics.drawRect(xLoc, yLoc, width, height);

            //the mouseover sprite
            over = new Sprite();
            over.graphics.lineStyle(1, 0x000000);
            over.graphics.beginFill(0x616D7E);
            over.graphics.drawRect(xLoc, yLoc, width, height);

            // etc ...