注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

神魔破杜梓的叨叨堂

Programming every day!

 
 
 

日志

 
 
 
 

AS3中同一类型的对象能够共享自身的私有变量?  

2008-10-09 09:26:02|  分类: My Tech |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
原文来自joshblog

Today, I was surprised to discover something that I didn't know about private variables in AS3. If an instance of class A has a reference to another instance of class A, then the second object's private variables are accessible to the first object. No restrictions. No special tricks. You just use obj.variableName like any other property request. I'll show you some example code, and you can try it yourself.

First, let's set up the situation. Alice has a secret in her diary. Trudy wants to know Alice's secret, so she's going to sneak in and take a peek. Here's a simple document class that presents our story in code:

Actionscript:
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.  
  5.     public class ISeeYourPrivates extends Sprite
  6.     {
  7.         public function ISeeYourPrivates()
  8.         {
  9.             //the intruder
  10.             var trudy:Person = new Person("Trudy");
  11.            
  12.             //the victim
  13.             var alice:Person = new Person("Alice", "I kissed a girl, and I liked it!");
  14.            
  15.             //Trudy is curious, and she wants to read Alice's diary
  16.             trudy.peekIntoDiary(alice);
  17.         }
  18.     }
  19. }

Finally, this UML-like diagram below also shows what's happening:

UML-like diagram. Alice and Trudy are instances of Person. Through peekIntoDiary(), Trudy can access Alice's private variable _secret.

Next, let's look at the Person class and see how the peekIntoDiary() method works:

Actionscript:
  1. package
  2. {
  3.     public class Person
  4.     {
  5.         public function Person(name:String, secret:String = null)
  6.         {
  7.             this.name = name;
  8.             this._secret = secret;
  9.         }
  10.        
  11.         public function peekIntoDiary(target:Person):void
  12.         {
  13.             trace(this.name, "is looking at ", target.name + "'s secret:", "\"" + target._secret + "\"");
  14.         }
  15.        
  16.         public var name:String;
  17.         private var _secret:String;
  18.     }
  19. }

As you can see the secret value sent to the constructor gets stored in the private variable _secret. Another Person instance is passed to the peekIntoDiary() method. In this case, the Trudy object (this) now has access to the Alice object (target). The trace() output reads:

Trudy is looking at Alice's secret: "I kissed a girl, and I liked it!"

Yes, Alice's private variable _secret has just been accessed by Trudy. Bet you didn't expect that to work. I know I didn't. Don't believe me? Try it yourself. The code above compiles and runs.

It makes some sense if you consider that the private access modifier is actually a namespace. Basically, behind the scenes, the compiler automatically creates a separate namespace for each individual class that it names private. This private namespace is only accessible to the one class for which it is defined. That's why other classes and subclasses can't access private variables. However, due to the scope of the namespace, all instances of the same class can access the private members of other instances. To be truly private, each individual instance of a class would needs its own namespace that other instances wouldn't know about. I'm not even sure if that's possible, but I have a vague idea of how it might work.

Does it matter? No, not really. You'll rarely find yourself in an situation where this behavior can be useful (see the credited link below for one possibility). I can't think of a case where this can be considered a security risk either. It's not much different than using object["variableName"] to get at private variables in AS2. Mainly, I wanted to share because it's somewhat unexpected behavior.

Credit: Thanks to Nick Bilyk for pointing this out in a post about cloning objects.


  评论这张
 
阅读(409)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017