以OOP正确方式访问父类

时间:2012-03-11 15:23:28

标签: actionscript-3 oop user-interface scope control-flow

我刚开始编程OOP而且我遇到了范围问题。 在下面的项目中,我有一个名为App的masterClass。 App-class有Screens:Screen-class和Navigation-class,因为它是孩子们。从导航类我想控制将显示哪些屏幕。我不知道怎么做......

请检查代码以完全理解我的意图

我非常感谢您的帮助,我很乐意学习编程,而不仅仅是一个肮脏的解决方案:)但欢迎所有建议!

// Main Class //
public class App extends Sprite
{
    private var screens:Array;
        private var screen1:Screen;
        private var screen2:Screen;
        private var screen3:Screen;
        private var screen4:Screen;

    public var currentScreen:String;
    //



    private var navigation:Navigation;

    public function App()
    {
        init();
    }

    private function init():void {
        buildScreens();

        buildNavigation();
    }

    private function buildScreens():void {
        screen1 = new Screen();
        screen1.name = 'startScreen';
        currentScreen = screen1.name;
        addChild(screen1);

        screen2 = new Screen();
        screen2.name = 'irrelevantA';

        screen3 = new Screen();
        screen3.name = 'irrelevantB';

        screen4 = new Screen();
        screen4.name = 'irrelevantC';


        screens = new Array(screen1, screen2, screen3, screen4);


    }

    private function buildNavigation():void {
        navigation = new Navigation(screens);
    }
}

// Screen Class //
public class Screen extends Sprite
{
    public function Screen()
    {
        // creates a new screen
    }
}


// Navigation Class //
public class Navigation extends Sprite
{
    private var buttons:Array;

    public function Navigation(screens:Array)
    {
        addButtons(screens);
    }

    private function addButtons(screens:Array):void {
        buttons = new Array();

        for each(var screen:Screen in screens) {
            var button:Button = new Button();
            button.link = screen.name;
            button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
            buttons.push(button);
        }
    }

    private function mouseDown(e:MouseEvent):void {
        // THIS IS WHAT MY QUESTION IS ABOUT: How should I talk to the parent class in an OOP correct way?
        // and how can I add and remove a screen in the App class from here?

        // Here some of my tries
        // I don't think using parent to get there is a good way because next time it might be; parent.parent.parent
        trace(e.target.parent.parent.currentScreen);
        this.parent.currentScreen;
        stage.App.currentScreen;
        App.currentScreen;


        //---------------------------------
    }
}

// Button Class //
public class Button extends Sprite
{
    public var link:String;

    public function Button()
    {
        // creates a new button
    }
}

2 个答案:

答案 0 :(得分:7)

如果您直接从子对象访问父类,则会创建强耦合 - 这正是您在精心构建的系统中不希望想要的。最好不要直接访问应用程序对象,而是使用事件侦听器和自定义事件来促进对例如导航。

这是一个例子。首先,创建一个自定义事件:

public class MyCustomEvent extends Event {

    public static const MENU_ITEM_SELECTED : String = "MENU_ITEM_SELECTED";
    public var selectedItem:String;
}

然后,当点击一个按钮时,让导航发送它:

public class Navigation extends Sprite () {
    // ...
    private function onButtonClicked(ev:Event) : void {
        ev.stopPropagation();
        var custEvent:MyCustomEvent = new MyCustomEvent(MyCustomEvent.MENU_ITEM_SELECTED);
        custEvent.selectedItem = ev.target.name;
        this.dispatchEvent (custEvent);
    }
    // ...
}

最后,让应用程序处理自定义事件并显示不同的屏幕:

public class App {
    // ...
    public function createNavigation () : void {
        navigation = new Navigation ();
        navigation.addEventListener (MyCustomEvent.MENU_ITEM_SELECTED, onMenuItemSelected);
        // ... more stuff happening
    }
    // ...

    private function onMenuItemSelected (ev:MyCustomEvent) : void {
        switchToScreen (ev.selectedItem);
    }

    private function switchToScreen (name:String) : void {
        // choose screen by name, etc.
    }
}

对于所有这些,屏幕和导航都不必知道任何其他涉及的对象,因此您可以轻松地替换每个对象而不会破坏系统的其余部分。

答案 1 :(得分:0)

您基本上希望通过调用公共函数将引用作为参数(与屏幕数组一样)和向上(子到父)传递向下(父对子)。

所以,在你的情况下是这样的:

App类:

private function buildNavigation():void {
   navigation = new Navigation(this, screens);
}
//etc
public function changeScreen(newScreen:int):void{
    //Your logic for adding/removing screens goes here
}

导航类:

 private var app:App

 public function Navigation(app:App, screens:Array)
    {
        this.app = app
        addButtons(screens);
    }

    private function addButtons(screens:Array):void {
        buttons = new Array();

        for each(var screen:Screen in screens) {
            var button:Button = new Button();
            button.link = screen.name;
            button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
            buttons.push(button);
        }
    }

    private function mouseDown(e:MouseEvent):void {
       app.changeScreens(2);
    }
}

显然,更改实现以满足您的需求(例如,现在您已经引用了App类,请考虑是否需要传递对屏幕数组的单独引用) - 这只是一个示例你如何沟通