flex 多态例子

1:写个接口
package com.interfacePoly
{
public interface MyInterface
{
function interfaceOne():void;
function interfaceTwo():void;
function interfaceThree():void;

}
}

2:写2个实现接口的类
a,
package com.interfacePoly
{
import mx.controls.Alert;
public class ImpInterfaceOne implements MyInterface
{
public function ImpInterfaceOne()
{
}

public function interfaceOne():void
{
Alert.show("one.......1");
}

public function interfaceTwo():void
{
Alert.show("one......2");
}

public function interfaceThree():void
{
Alert.show("one......3");
}

}
}

b,
package com.interfacePoly
{
import mx.controls.Alert;
public class ImpInterfaceTwo implements MyInterface
{
public function ImpInterfaceTwo()
{
}

public function interfaceOne():void
{
Alert.show("Two......1");
}

public function interfaceTwo():void
{
Alert.show("Two......2");
}

public function interfaceThree():void
{
Alert.show("Two.....3");
}

}
}

3:实现多态的类
package com.interfacePoly
{
public class Imp
{
public function Imp()
{
}
public function interfaceOne(interface1:MyInterface):void
{
interface1.interfaceOne();
}
public function interfaceTwo(interface2:MyInterface):void
{
interface2.interfaceTwo();
}

}
}
4:调用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="" layout="absolute">
<mx:Script>
<![CDATA[
import com.interfacePoly.ImpInterfaceTwo;
import com.interfacePoly.ImpInterfaceOne;
import com.interfacePoly.Imp;
import com.flex.Polymorphism.OnePoly;
import com.flex.Polymorphism.TwoPoly;
import com.flex.Polymorphism.UsePoly;
import com.flex.impInterface.ImpInterface;
import com.flex.sonclass.bookSonClass;
import mx.controls.Alert;
private function btx():void
{
var test:Imp=new Imp();
//test.interfaceOne(new ImpInterfaceTwo);
test.interfaceTwo(new ImpInterfaceTwo);

}

]]>
</mx:Script>
<mx:Button id="bt1" x="177.5" y="142" label="Button" height="82" width="91" click="btx()"/>
</mx:Application>

你可能感兴趣的:(xml,Flex)