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这这门语言里面,它的优点在于它本身的可移植性上面,而要做到可移植的话,本身就需要一个中介作为翻译工 ...
随机推荐
- PyCharm中设置菜单字体大小
file——>setting,然后选择appearance,下图右侧红色边框中的内容即设置菜单的字体和大小
- 将execel表格的数据导入到mysql数据库
在开发中经常会将现成的execel表格导入到数据库里,否则一个个字段插入填写,太浪费时间,效率很低.本文主要是讲如果将execel表格导入到mysql数据库,希望对各位有所帮助.使用软件:sql工具: ...
- Python+selenium之获取文本值和下拉框选择数据
Python+selenium之获取文本值和下拉框选择数据 一.结合实例进行描述 1. 实例如下所示: #新增标签操作 def func_labels(self): self.driver.find_ ...
- (转)The windows boot configuration data file dose not contain a valid OS entry
开价蓝屏,显示: Windows failed to start. A recent hardware or software change might be the case.to fix the ...
- JBPM4 经常使用表结构及其说明
首先我想说.这不一篇原创博文. 这里设置成原创.主要是为了分享,由于它对于jbpm的刚開始学习的人,真的值得一看.原作者的博文地址并没有查到,我是在还有一位转载此文的博主那儿获得的.地址在这儿. 本文 ...
- tomcat启动后,页面浏览时报错 Unable to compile class for JSP的解决方案
转:tomcat启动后,页面浏览时报错 Unable to compile class for JSP的解决方案 检查tomcat与web工程对应版本,tomcat中对应版本的jar包拷贝到web工程 ...
- 修改UE4的deriveddatacache目录位置,扩大C盘空间
按照默认安装目录,UE4会装在C盘 C:\Program Files\Epic Games\UE_4.15 DerivedDataCache缓存目录在 C:\Users\你的用户名\AppData\L ...
- oracle 在sql中拼接时间
<isNotEmpty prepend="and" property="startDate"> to_date(rs.count_date, 'YY ...
- iPhone 6 Screens Demystified
http://www.paintcodeapp.com/news/iphone-6-screens-demystified
- jquery对象转dom对象
jq取兄弟级的上移个元素 jquery对象: var a = $(this).prev("a"); console.log(a); 输出: 转dom对象: var a = $(th ...