Singleton: this & instance
public class Singleton{
private static final Singleton instance = new Singleton();
private String name;
private int age;
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
public void instanceSetter(String name, int age){
instance.name = name;
instance.age = age;
}
public void thisSetter(String name, int age){
this.name = name;
this.age = age;
}
public void instanceGetter(){
System.out.println("instance.name: " + instance.name);
System.out.println("instance.age: " + instance.age);
}
public void thisGetter(){
System.out.println("this.name: " + this.name);
System.out.println("this.age: " + this.age);
}
public static void main(String[] args){
Singleton s = Singleton.getInstance();
System.out.println(s);
System.out.println("Before setter()");
s.instanceGetter(); //null, 0
s.thisGetter(); //null, 0
System.out.println();
s.instanceSetter("lxw", 26);
System.out.println("After intanceSetter()");
s.instanceGetter(); //lxw, 26
s.thisGetter(); //lxw, 26
System.out.println();
s.thisSetter("wxl", 29);
System.out.println("After thisSetter()");
s.instanceGetter(); //wxl, 29
s.thisGetter(); //wxl, 29
}
}
Output:
lxw@lxw::::~$ java Singleton
Singleton@15db9742
Before setter()
instance.name: null
instance.age:
this.name: null
this.age: After intanceSetter()
instance.name: lxw
instance.age:
this.name: lxw
this.age: After thisSetter()
instance.name: wxl
instance.age:
this.name: wxl
this.age:
Singleton: this & instance的更多相关文章
- 【白话设计模式四】单例模式(Singleton)
转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...
- Java设计模式之单例模式(Singleton)
前言: 在总结okHttp的时候,为了管理网络请求使用到了单例模式,晚上实在没啥状态了,静下心来学习总结一下使用频率最高的设计模式单例模式. 单例模式: 单例模式确保某个类只有一个实例,而且自行实例化 ...
- Net设计模式实例之单例模式( Singleton Pattern)
一.单例模式简介(Brief Introduction) 单例模式(Singleton Pattern),保证一个类只有一个实例,并提供一个访问它的全局访问点.单例模式因为Singleton封装它的唯 ...
- 单例模式 - Singleton
对今天学习的Singleton Pattern简单总结下: 定义:保证一个类只有一个实例,必须自己创建自己的实例,并提供一个访问它的全局访问点. private 构造函数: private stati ...
- Singleton(单例模式)
一. /** * lazy man(不是线程安全的) * @author TMAC-J * */ public class Singleton { private static Singleton i ...
- SingleTon单例模式总结篇
在Java设计模式中,单例模式相对来说算是比较简单的一种构建模式.适用的场景在于:对于定义的一个类,在整个应用程序执行期间只有唯一的一个实例对象. 一,懒汉式: 其特点是延迟加载,即当需要用到此单一实 ...
- singleton pattern的推荐实现
一.单例模式的C#实现: (1)使用double-checked locking的方式: public sealed class Singleton { private static volatile ...
- Multiton & Singleton
From J2EE Bloger http://j2eeblogger.blogspot.com/2007/10/singleton-vs-multiton-synchronization.html ...
- Java中的GOF23(23中设计模式)--------- 单例模式(Singleton)
Java中的GOF23(23中设计模式)--------- 单例模式(Singleton) 在Java这这门语言里面,它的优点在于它本身的可移植性上面,而要做到可移植的话,本身就需要一个中介作为翻译工 ...
随机推荐
- CSS:scrollbar的属性及样式分类
overflow 内容溢出时的设置(设定被设定对象是否显示滚动条) overflow-x 水平方向内容溢出时的设置 overflow-y 垂直方向 ...
- ThinkPHP种where的使用(_logic and _complex)的使用实例
1.对于thinkphp中的 and ,or 等复合型的查询,我要正确的使用相关的方法. a.实例 b.实例
- iOS 圆角投影
self.backgroundColor = [UIColor whiteColor]; self.layer.shadowColor = [UIColor lightGrayColor].CGCol ...
- cocos2dx游戏--三国关羽传【角色扮演类】Demo的制作及实现
项目地址:https://github.com/moonlightpoet/GuanYuZhuan 主要类及其对应效果: MainScene:菜单界面(用于选择不同剧本) StoryScene:故事界 ...
- std::condition_variable
/* std::condition_variable 提供了两种 wait() 函数.当前线程调用 wait() 后将被阻塞(此时当前线程应该获得了锁(mutex),不妨设获得锁 lck),直到另外某 ...
- 【BZOJ3622】已经没有什么好害怕的了 容斥+DP
[BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...
- node sever
一.基础创建服务器 // 引入Http var http = require("http"); // 创建服务器 http.createServer(function(reques ...
- eclipse中设置在编译运行项目之前自动保存修改的文件
Window -> Preferences -> General -> Workspace -> “Save automatically before build” Windo ...
- 谨防in、or 公用性能问题
今天遇到一个奇葩的问题:在where条件中用了 m in(×××) or m>=10,查询直接超时,我看了一下,数据库中就2万条数据 我将查询改为了union all 结果就不超时了
- imToken 测评通关攻略
imToken 测评通关攻略 2017-10-19 imToken 在 1.3.3 版本新增了用户风险测评系统, 目的是为了让更多的用户了解钱包安全知识以及区块链的基本概念, 从某种程度上提升了整个区 ...