几道笔试题
2015-11-26 02:48
405 查看
1.单例模式
饿汉模式:
特点:加载类慢,获取对象速度快
特点:加载类快,获取对象慢
2.下面代码输出结果为
答案为:1234
3.下面运行结果为
输出:
static A
static B
Hello A
I'm A class
Hello B
I'm B class
先执行静态语句 在自上而下
4.判断一个对象是否是一个已知类的对象,关键字 instanceof
例
t = 3;
System.out.println(t instanceof Integer);
饿汉模式:
特点:加载类慢,获取对象速度快
public class Singleton{ //1.创建私有构造方法 private Singleton(){ }<span> </span> private static Singleton instance=new Singleton();//2.创建类的唯一实例 <span> </span>//3.提供一个用于获取实例的方法 public static Singleton getInstance(){ return instance; } }懒汉模式:
特点:加载类快,获取对象慢
public class Singleton{ //1.创建私有构造方法 private Singleton(){ } private static Singleton instance;//2.声明类的唯一实例 //3.提供一个用于获取实例的方法 public static Singleton getInstance(){ if(instance==null){ instance=new Singleton(); } return instance; } }
2.下面代码输出结果为
public class Test1 { public static void changeStr(String str) { str += "welcome"; } public static void main(String[] args) { String str = "1234"; changeStr(str); System.out.println(str); } }
答案为:1234
System.out.println(str); //输出的只是main 方法中的str
3.下面运行结果为
class HelloA { public HelloA() { System.out.println("HelloA"); } { System.out.println("I'm A class"); } static { System.out.println("static A"); } } public class HelloB extends HelloA { public HelloB() { System.out.println("HelloB"); } { System.out.println("I'm B class"); } static { System.out.println("static B"); } public static void main(String[] args) { new HelloB(); } }
输出:
static A
static B
Hello A
I'm A class
Hello B
I'm B class
先执行静态语句 在自上而下
4.判断一个对象是否是一个已知类的对象,关键字 instanceof
例
t = 3;
System.out.println(t instanceof Integer);
相关文章推荐
- RecyclerView的使用记录
- LeetCode "Best Time to Buy and Sell Stock with Cooldown" !
- 极光推送详细讲解+错误知识点
- 第5章(5) 枚举类型
- thrift安装以及编写小程序
- JAVA中for的用法
- python 入门
- 利用CentOS系统IPtables防火墙添加网站IP白名单
- 12个球找出奇异球
- [Redux] Implementing Store from Scratch
- 解决Maven不能下载“oracle、aspectjweaver、com.springsource.net.sf.cglib”jar
- CentOS 7安装VMware Workstation12
- 合并区间
- Win7中安装不了Arduino驱动怎么办
- 机器学习笔记_ 降维_3:SVD
- Linux正则表达式
- **LeetCode-Minimum Window Substring
- 第5章(4) 可变参数列表
- 时间TDateTime相当于是Double,即双精度数64位,终于查到它用11位表示e,53位表示精度(整数小数一起),最前面一位表示正负
- Deep learning-contrast normalization