gpt4 book ai didi

actionscript-3 - 以 OOP 正确的方式访问父类

转载 作者:行者123 更新时间:2023-12-05 00:00:43 26 4
gpt4 key购买 nike

我刚开始编写面向对象编程,但遇到了范围问题。
在下面的项目中,我有一个名为 App 的 masterClass。 App-class 有 Screens:Screen-class 和 Navigation-class 作为它的 child 。从导航类我想控制将显示哪些屏幕。我不知道如何做到这一点...

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

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

// 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
}
}

最佳答案

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

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

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.
}
}

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

关于actionscript-3 - 以 OOP 正确的方式访问父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9656216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com