actionscript3不能用private修饰构造方法,因此可以用一个内部类来做构造方法的参数:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
package DesignPatterns
2 {
3 import mx.controls.Alert;
4 public class Singleton
5 {
6 private static var singleton:Singleton;
7 public function Singleton(privateclass:privateClass)
8 {
9 Alert.show("this is pulic Class");
10 }
11
12 public static function getInstance():Singleton{
13 if(singleton==null){
14 singleton = new Singleton(new privateClass());
15 }
16 return singleton;
17 }
18
19 }
20 }
21
22 import mx.controls.Alert;
23 class privateClass{
24 public function privateClass(){
25 Alert.show("this is private Class");
26 }
27 }
2 {
3 import mx.controls.Alert;
4 public class Singleton
5 {
6 private static var singleton:Singleton;
7 public function Singleton(privateclass:privateClass)
8 {
9 Alert.show("this is pulic Class");
10 }
11
12 public static function getInstance():Singleton{
13 if(singleton==null){
14 singleton = new Singleton(new privateClass());
15 }
16 return singleton;
17 }
18
19 }
20 }
21
22 import mx.controls.Alert;
23 class privateClass{
24 public function privateClass(){
25 Alert.show("this is private Class");
26 }
27 }