About the overloading in Flex

Categories: Flex; Tagged with: ; @ July 16th, 2011 13:05

Those days I’m looking a new job related to Java and Flex… Seems that this is a popular interview question, I event didn’t encounter it in the ACE. But I was asked more than 3 times.

Talk about overloading in OOPS:

Overloading is between the methods with the same name, If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

ActionScript 3 support overriding, But why it is not support overloading?

It’s a good question, and can be the answer of what’s the difference between Java and Flex?
Based on the Standard ECMA-262 (ECMAScript Language Specification), Action Script does not implement the overloading. and every method in the class is a Function object(property)

Since AS doesn’t support overloading, what can you do to implement the overloading?

As I known, Basically there are three way to handle the overload requirement:

1. Using optional parameters, such as:
getUser(userID:int = –1, userName: String = null):User {..}

2. Using rich parameters:
if(args.length == 0) {
return null;
}else if (args.length…)….

3. Using Object as the parameters, like:
getUser(arg: Object):User {
if(arg is String) {
return …. get user by the userName;
} else {
if (arg is int) {
return …get user by the userID;
}//end of if.

According those codes, you can see those methods all need very clear comments, and not easy for other to use them. in the comment we need to tell others the parameters type or sequence.

In fact, Since the AS does not support the overloading, and the comments need be very very clear, and usually, there will be two public methods: getUserByID(), getUserByName(), maybe a private or protected method using the above 3 ways to do the get user job.

Let the Flex thread sleep for a while

Categories: Flex; Tagged with: ; @ July 15th, 2011 22:26

Sometimes we use thread.sleep to let the thread stop running for a while in Java, but in Flex, we can’t  control the thread, but sometimes we want to let the method sleeping,  and here is what I’ll do:

public class SleepUtils
	// Constructor
	public function SleepUtils() {
	 * Let the thread sleep. 
	 * @param ms Million seconds you want to sleep.
	public static function startSleep(ms:Number):void {
		var timeBegin:Date = new Date();
		trace("Sleep begin: " + timeBegin.toTimeString());
		while((new Date()).getTime() - timeBegin.getTime() < ms) {
		trace("Sleep end: " + new Date().toTimeString());
} // End of class

Flex Event stopimmediatepropagation VS stoppropagation

Categories: Flex; Tagged with: ; @ June 17th, 2011 17:35

What’s the difference ? Here is the test codes:

We will create a button in a Group, and listen the button click event(MouseEvent.Click):

		protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void {
			//  Create button and container
			var hbox:HGroup = new HGroup();
			var button:Button = new Button();
			button.label = "Button";

			// Add Eventer litener
			button.addEventListener(MouseEvent.CLICK, onButtonClick1);
			button.addEventListener(MouseEvent.CLICK, onButtonClick2);
			hbox.addEventListener(MouseEvent.CLICK, onButtonClick3);


			// Button click handler 1
			private function onButtonClick1(e:MouseEvent):void {
				e.stopImmediatePropagation(); // or e.stopPropagation();
				trace("Handler 1");

			// Button click handler 2
			private function onButtonClick2(e:MouseEvent):void {
				trace("Handler 2");

			// Button click handler 3 listened by hbox
			private function onButtonClick3(e:MouseEvent):void {
				trace("Handler 3");

Here is the difference:

when use stop propagation in the handler 1st, the trace is:

Handler 1
Handler 2

but when use stopImmediatePropagation(), the trace is:

Handler 1

That’s pretty easy to find the differences:

“The stopImmediatePropagation() method also prevents the Event objects from moving on to the next node, but it does not allow any other event listeners on the current node to execute.” (Adobe livedocs)

Flex 自定义Event 低级错误一例: 未重写Clone方法, 导致类型转换失败

Categories: Flex; Tagged with: ; @ December 10th, 2010 13:59

在Event监听并转发过程中, 因为没用重写Clone方法, 导致实例被Clone为普通Event实例.

自定义Event: EventTest: 继承Event, 增加了一些属性, 重写了toString(), 未重写Clone.

出现问题: 创建EventTest实例后, 多次监听并转发, 在监听, 转发过程中, Event实例对象被Clone, 因为未重写Clone方法, 会通过Event类的Clone方法进行复制, 仅会复制其EventType, 且类型为Event.

形象的说就是: eventTest –> 被监听到, 准发 ->框架Clone eventTest(EventTest类中未重写Clone方法, 于是eventTest被克隆为一个普通Event) –> 再次监听, Event类型不吻合, 报错.

公司代码规范中严格规定了自定义Event必须及时重写Clone与toString方法, 写了这么久, 我才领悟到原因. 败了

Flex4 Spark TextArea 设置自适应大小及派发鼠标滚动事件

Categories: Flex; Tagged with: ; @ November 14th, 2010 16:43

记得在使用Flex4之前, 看过一篇介绍TextArea自适应高度的文章, 监听Event, 动态改变高度.  很少用TextArea, 也没用过. 在Flex4, 使用Spark界面时, TextArea可设为自动适应.

设置heightInLines = NaN -  如果该属性为 NaN(默认值),则组件的默认高度由要显示的文本确定。
See: http://tinyurl.com/2ubwrta

但当鼠标在TextArea之上时, 鼠标滚动, 整个Application的scrollbar竟然没反应…猜是没有dispatch event, 于是增加监听函数:

	/** 响应TextArea的鼠标滚动事件, 接收到后派发出去, 以便引起外部UI可获得该事件, 并进行响应的滚动. */
	protected function onMouseEventWheel(e:MouseEvent):void {


Newer Posts <-> Older Posts

// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.