如何从classB访问ClassA的方法?

时间:2015-04-30 12:22:34

标签: actionscript-3

我真的不知道问题到底要问什么。所以我将在这里解释一切:

我正在使用桌面AIR数据库应用程序我有一个名为“数据库屏幕”的类,它链接到一个影片剪辑:

package  {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.InteractiveObject;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;

public class DatabaseScreen extends MovieClip {

    private var allButtons:Array;
    private var codeSelected:Boolean;
    private var nameSelected:Boolean;
    private var xmlDatabase:XML;
    private var dbFile:File;
    public var xmlList:XMLList;
    private var totalProducts:uint;
    private var totalItems:uint = 0;
    private var productExists:Boolean;
    private var productId:Number;
    private var XMLReader:xmlReader;

    public function DatabaseScreen() {
        allButtons = [searchPanel.search_btn, addNewPanel.addNew_btn, dbStatsPanel.xls_btn, searchPanel.code_radio, searchPanel.name_radio];
        init();
    }

    private function init():void
    {
        XMLReader = new xmlReader();
        //adding event listeners..
        for(var i:int = 0; i<allButtons.length; i++)
        {
            allButtons[i].addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.MOUSE_DOWN, onDown, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.MOUSE_UP, onUp, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
            allButtons[i].buttonMode = true;
        }

        //creating a new file for the database
        dbFile = File.documentsDirectory.resolvePath("BlackCat/Database.xml");
        xmlDatabase = new XML();
        getXML();
        saveXML();
        getTotalItems();
        dbStatsPanel.items.text = String(totalItems);
    }
    private function getXML():void
    {
        if(dbFile.exists)
        {
            var stream:FileStream = new FileStream();
            stream.open(dbFile, FileMode.READ);
            xmlDatabase = new XML(stream.readUTFBytes(stream.bytesAvailable));
            stream.close();
            xmlList = xmlDatabase.Product;
            dbStatsPanel.products.text = xmlList.length();
            XMLReader.xmlData = xmlList;
            XMLReader.updateXMLList(xmlList);

        }
        else
        {
            xmlDatabase = <Products></Products>
        }
    }
    public function getXMlList():XMLList
    {
        return xmlList;
    }
    private function getTotalItems():void
    {
        for(var i:int = 0; i < xmlList.length(); i++)
        {
            totalItems += uint(xmlList[i].@Qty);
        }
    }
    private function saveXML():void
    {
        var stream:FileStream = new FileStream();
        stream.open(dbFile, FileMode.WRITE);
        stream.writeUTFBytes(xmlDatabase.toXMLString());
    }
    private function onDown(evt:MouseEvent):void
    {
        evt.target.scaleX = evt.target.scaleY = .8;
    }
    private function onUp(evt:MouseEvent):void
    {
        evt.target.scaleX = evt.target.scaleY = 1;

    }
    private function onOver(evt:MouseEvent):void
    {
        evt.target.alpha = .5;
    }
    private function onOut(evt:MouseEvent):void
    {
        evt.target.alpha = 1;
        evt.target.scaleX = evt.target.scaleY = 1;
    }
    private function onClick(evt:MouseEvent):void
    {
        switch(evt.target.name)
        {
            case "code_radio":
                codeSelected = true;
                nameSelected = false;
                searchPanel.code_radio.gotoAndStop(1);
                searchPanel.name_radio.gotoAndStop(2);
            break;

            case "name_radio":
                nameSelected = true;
                codeSelected = false;
                searchPanel.code_radio.gotoAndStop(2);
                searchPanel.name_radio.gotoAndStop(1);
            break;

            case "addNew_btn":
                //Checking if the product already exists..
                for(var i:int = 0; i < xmlList.length(); i++)
                {
                    if(xmlList[i].@code == addNewPanel.add_code_txt.text)
                    {
                        productExists = true;
                        productId = i;
                    }
                }
                if(productExists)
                {
                    var PQty:uint = uint(xmlList[productId].@Qty);
                    var PQtoAdd:uint = uint(addNewPanel.add_qty_txt.text);
                    PQty += PQtoAdd;
                    xmlList[productId].@Qty = String(PQty);
                    productExists = false;
                }
                else
                {
                    xmlDatabase.appendChild(<Product code={addNewPanel.add_code_txt.text} 
                    name={addNewPanel.add_name_txt.text} Price={addNewPanel.add_price_txt.text} 
                    Qty={addNewPanel.add_qty_txt.text}></Product>);
                }
                totalItems += uint(addNewPanel.add_qty_txt.text);
                xmlList = xmlDatabase.Product;
                dbStatsPanel.products.text = xmlList.length();
                dbStatsPanel.items.text = String(totalItems);
                XMLReader.updateXMLList(xmlList);
                saveXML();
                trace(xmlDatabase);
            break;

        }
    }
    public function setVisiblity(visibility:Boolean):void
    {
        switch(visibility)
        {
            case true:
                this.visible = true;
            break;

            case false:
            this.visible = false;
            break;
        }
    }
    public function getXMLList():XMLList
    {
        return xmlList;
    }


}

}

我有另一个名为“NewOrder”的类,在这个类中我试图从“DatabaseScreen”类访问xmlList,但我无法这样做。

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import com.greensock.TweenMax;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;


public class NewOrder extends MovieClip {
    private var allButtons:Array;
    private var shoppingCart:Array;
    private var scroller:FlickScroll;
    private var db:DatabaseScreen;
    private var XMLReader:xmlReader;

    public function NewOrder() {
        this.visible = false;

        init(db);
    }
    private function init(dbase:DatabaseScreen):void
    {
        XMLReader = new xmlReader();
        trace(XMLReader.xmlData);

        scroller = new FlickScroll(masker, container, 84.05, -500);
        addChild(scroller);
        allButtons = [scan_btn, print_invoice];
        shoppingCart = [];
        for(var i:int = 0; i < allButtons.length; i++)
        {
            allButtons[i].addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.MOUSE_DOWN, onDown, false, 0, true);
            allButtons[i].addEventListener(MouseEvent.MOUSE_UP, onUp, false, 0, true);
            allButtons[i].mouseChildren = false;
            allButtons[i].buttonMode = true;
        }
    }

    private function onOver(evt:MouseEvent):void
    {
        evt.target.alpha = .5;
    }
    private function onOut(evt:MouseEvent):void
    {
        evt.target.alpha = 1;
        evt.target.scaleX = evt.target.scaleY = 1;
    }
    private function onDown(evt:MouseEvent):void
    {
        evt.target.scaleX = evt.target.scaleY = .8;
    }
    private function onUp(evt:MouseEvent):void
    {
        evt.target.scaleX = evt.target.scaleY = 1;
        if(evt.target.name == "scan_btn")
        {
            var cartElement:CartElement = new CartElement();
            container.addChild(cartElement);
            cartElement.x = 0;
            cartElement.alpha = 0;
            TweenMax.to(cartElement, .4, {alpha:1});
            shoppingCart.push(cartElement);
            cartElement.id_txt.text = String(shoppingCart.length) + " - ";

            if(shoppingCart.length < 2)
            {
                cartElement.y = container.height - 30;
            }
            else
            {
                cartElement.y = container.height + 5;
            }

        }
    }
}

}

还有一个Main类,我处理所有屏幕,例如DatabaseScreen,NewOrder ..这意味着我已将这些添加到Main类的显示列表中。为了从DatabaseScreen访问xmlList,我尝试在NewOrder类中创建一个新实例,但它创建了DatabaseScreen。我还尝试创建另一个名为“xmlReader”的类:     包{

public class xmlReader {
    public var xmlData:XMLList;

    public function xmlReader() {

    }

    public function updateXMLList(updatedList:XMLList):void
    {
        xmlData = updatedList;
    }

}

}

在databaseScreen类中,只要我填充xmlList,我就将xmlReader类的xmlData设置为等于databaseScreen类中的xmlList。当我在databaseScreen中跟踪XMLReader.xmlData时,它工作正常。但是当我在NewOrder类中做同样的事情时,我在输出窗口中得到“null”。 我该如何解决这个问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要将引用传递给Order类才能访问DatabaseScreen类。这样的事情。

var ds = new  DatabaseScreen();
var or = new NewOrder(ds)

在NewOrder中,您可以保存参考号。

public class NewOrder {
    private var screens:DatabaseScreens;

    public function NewOrder(ds:DatabaseScreens) {
        screens = ds;
        // Now you can call screens.xmlList
    }
}