[05-D2] static, interface, extends

1. static 함수
   - [new -- ActionScript Class]

>>>>> src/test/Test.as
package www.test{
    import mx.controls.Alert;
    
    public class Test{
        public var cnt:int = 0;              // 인스턴스 변수 선언 
        public static var staticCnt:int = 0; // 스태틱 변수 선언 

        // 생성자
        public function Test() {
            cnt = cnt + 1;
            staticCnt = staticCnt + 1; 
        }

        public function instFunc():void {    // 멤머 메서드 
            mx.controls.Alert.show("instance 변수: " + cnt);
        }        

        public static function staticFunc():void { // 스태틱 메서드 
            mx.controls.Alert.show("static 변수: " + staticCnt);
        }        

    }
}





>>>> src/TestUI.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" height="387">
    <mx:Script>
    <![CDATA[
    import www.test.*;
    
    private function instanceDisplay():void {
        var obj1:Test = new Test();
        obj1.instFunc();
        var obj2:Test = new Test();
        obj2.instFunc();
        var obj3:Test = new Test();
        obj3.instFunc();
    }

    private function staticDisplay():void {
        Test.staticFunc();
    }
    ]]>
    </mx:Script>
    <mx:Button label="instance변수" click="instanceDisplay()"/>
    <mx:Button label="static 변수" click="staticDisplay()"/>
</mx:Application>





4. implements: interface

※ 추상 클래스는 ActionScript 3.0에서는 지원하지 않음

>>>>> Printer.as

package test.inter{
    public interface Printer{
        function start():String;
        function print():String;
        function stop():String;
        
    }
}





>>>>> Hp.as
package test.inter
{
// 인터페이스 구현 
    public class Hp implements Printer
    {
        public function Hp(){
            
        }

        public function start():String
        {
            return "인쇄를 시작합니다.";
        }
        
        public function print():String
        {
            return "인쇄 중입니다...........";
        }
        
        public function stop():String
        {
            return "인쇄를 종료합니다.";
        }
        public function scanner():String{
           return "스캔을 시작합니다.";
        
    }
}




>>>>> Cannon.as



>>>>> Epson.as



>>>>> InterfaceTest.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.*;
import test.inter.*;
public function execute():void{
var msg:String = "";
var obj:Printer = null;
txaMsg.text = "";
obj = new Hp();
msg += obj.start()+"\n";
msg += obj.print()+"\n";
msg += obj.stop()+"\n";
txaMsg.text = msg;                    
}
public function typeChange():void{
var msg:String = "";
var obj:Printer = null; // 인터페이스 선언 
txaMsg.text = "";
// var obj:Printer = new Hp();
obj = new Hp();      // Hp 객체 생성
// obj.
var hp:Hp = Hp(obj); // 형변환, (Hp)obj;
msg += hp.scanner() + "\n";
msg += hp.start() + "\n";
var hp2:Hp = obj as Hp;
hp2.scanner();

// var hp3:Hp = (Hp)obj; // ERROR
                  
txaMsg.text = msg;                 
}
]]>
</mx:Script>
<mx:TextArea x="10" y="10" width="463" height="373" id="txaMsg" fontSize="18"/>
<mx:Button x="10" y="391" label="Interface 테스 트" id="btnExecute" fontSize="18" click="execute()"/>
<mx:Button x="202" y="391" label="객체 형변 환" id="btnType" fontSize="18" click="typeChange()"/>
</mx:Application>




5. extends: 상속, 다중 상속 안됨

>>>>> IT.as

package test.extend{
    public class IT{
        public function IT(){
        }
        
        public function itIntroduce():String{
            return "▶ IT는 하드웨어 소프트웨어로 구성";
        }
    }
}




>>>>> SW.as



>>>>> DB.as



>>>>> extendsTest.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.controls.*;
            import test.extend.*;
            
            public function execute():void{
                var msg:String = "";
                var obj:DB = new DB();
                
                txaMsg.text = "";
                msg += obj.itIntroduce() + "\n";
                msg += obj.swIntroduce() + "\n";
                msg += obj.dbIntroduce() + "\n";
                msg += obj.dbDevelop() + "\n";
                
                txaMsg.text = msg;                 
            }
        ]]>
    </mx:Script>
    <mx:TextArea x="10" y="10" width="457" height="403" id="txaMsg" fontSize="18"/>
    <mx:Button x="10" y="421" label="상속 테스트" id="btnExtends" fontSize="18" click="execute()"/>
    
</mx:Application>




Posted by ▶파이팅◀

블로그 이미지
Let's start carefully from the beginning
▶파이팅◀

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함