码迷,mamicode.com
首页 > 编程语言 > 详细

类的特殊获取方式——《Thinking in Java》随笔008

时间:2017-02-27 16:46:31      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:turn   void   auth   access   oid   via   author   and   think   

//:Lauch.java
// 学习《Thinking in Java》的代码笔记 
// Soup是后面学习单例设计模式的一个基础。
package cn.skyfffire;

/**
 * 
 * @author skyfffire
 *
 */
class Soup {
    private Soup() {}
    /**
     * 可以via这个static method返回new objecy.
     * 
     * @return 新的对象的句柄
     */
    public static Soup makeSoup() {
        return new Soup();
    }
    
    /**
     * 创建静态对象并根据需求返回句柄
     * 
     * return ps1的句柄
     */
    private static Soup ps1 = new Soup();
    public static Soup access() {
        return ps1;
    }
    public void f() {
        System.out.println("Soup f function is run.");
    }
}

class Sandwich {
    public static void main(String[] args) {
        new Lauch().test();
    }
    
    public void f() {
        System.out.println("Sandwich f function is run.");
    }
}

/**
 * 当然,每一个文件都只有唯一的public class.
 *
 */
public class Lauch {
    void test() {
        Soup priv2 = Soup.makeSoup();
        Sandwich f1 = new Sandwich();
        
        priv2.f();
        f1.f();
        
        Soup.access().f();
    }
}
///:~

 

类的特殊获取方式——《Thinking in Java》随笔008

标签:turn   void   auth   access   oid   via   author   and   think   

原文地址:http://www.cnblogs.com/skyfffire/p/6474976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!