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这这门语言里面,它的优点在于它本身的可移植性上面,而要做到可移植的话,本身就需要一个中介作为翻译工 ...
随机推荐
- Python之查询美国护照状态
该程序会每隔至少1秒进行一次护照状态查询 需要修改passportNo变量为自己的护照号码. 另外需要pip install beautifulsoup4 #coding=utf-8 import r ...
- ResNet 结构理解
博客来源于:https://blog.csdn.net/buyi_shizi/article/details/53336192:https://blog.csdn.net/dcrmg/article/ ...
- WebApi(6) 后台C#调用WebApi
https://www.cnblogs.com/cxd1008/p/6640015.html 今天来写一下后台C#代码如何访问webapi 这里使用HttpClient方法访问webapi也是很常用的 ...
- 13个非常实用的JavaScript小技巧
使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是否有一个有效值,如果存在就返回true值.为了做这样的验证,我们可以使用!!操作符来实现是非常的方便与简单.对于变量可以使用 ...
- Android得到SD卡文件夹大小以及删除文件夹操作
float cacheSize = dirSize(new File(Environment.getExternalStorageDirectory() + AppConstants.APP_CACH ...
- [LintCode] 最多有多少个点在一条直线上
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...
- JS:ES5数组基本操作
一.添加删除 push(): 尾部添加,返回数组 pop(): 尾部删除,返回删除项 unshift() : 头部添加,返回数组 shift() : 头部删除,返回删除项 二.插入.替换 万能spli ...
- 文艺青年装B指南
和大龄文艺青年们去凤凰的时候,很难不注意到狭窄小道旁边的文艺小店.有提供焦糖玛奇朵的咖啡店,有兜售梦露赫本明信片和烟雨 凤凰笔记本的店铺,还有复古式的静吧,常驻唱民谣小众歌曲的流浪歌手.我每看 ...
- Javascript-const 常量
const 常量 常量是块级作用域,很像使用 let语句定义的变量.常量的值不能通过重新赋值来改变,并且不能重新声明. 此声明创建一个常量,其作用域可以是全局或本地声明的块. 与var变量不同,全局常 ...
- HDU 4348 To the moon(可持久化线段树)
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...