《Cracking the Coding Interview》——第14章:Java——题目1
2014-04-26 18:20
题目:从继承的角度,把构造函数设成private有什么意义?
解法:就不能继承了。单体模式里也这么干,目的是为了不让使用者自主生成对象,进行限制。
代码:
// 14.1 In terms of inheritance, what is the point of declaring the constructor as private?
// Answer:
// 1. private member cannot be inherited, thus the class cannot be inherited.
// 2. constructor is private, so you cannot new an object freely. Only through public static member method can you get an instance of this class.
// 3. with this you can implement Singleton Pattern, which ensures that a class has at most one instance within an application. You can but you don't have to do it.
public class TestJava {
private static TestJava instance = null; public static TestJava getInstance() {
if (instance == null) {
instance = new TestJava();
}
return instance;
} public void f() {
System.out.println("TestJava::f()");
} private TestJava() {
} public static void main(String[] args) {
System.out.println("Hello world.");
TestJava testJava = TestJava.getInstance();
testJava.f();
}
}
《Cracking the Coding Interview》——第14章:Java——题目1的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第14章:Java——题目6
2014-04-26 19:11 题目:设计一个循环数组,使其支持高效率的循环移位.并能够使用foreach的方式访问. 解法:foreach不太清楚,循环移位我倒是实现了一个,用带有偏移量的数组实现 ...
- 《Cracking the Coding Interview》——第14章:Java——题目5
2014-04-26 19:06 题目:Java中的对象反射机制是什么?有鼠么用? 解法:完全不了解,因为java编程经验太少,完全没用过.查了一些资料后,感觉反射机制是个强大并需要边用边体会的强大工 ...
- 《Cracking the Coding Interview》——第14章:Java——题目4
2014-04-26 19:02 题目:解释下C++里模板和java里泛型的区别? 解法:我很少用java,属于连语法都不过关的程度.所以这个题还真没法详细答,查了些资料以后写了以下几点. 代码: / ...
- 《Cracking the Coding Interview》——第14章:Java——题目3
2014-04-26 18:59 题目:final.finally.finalize有什么区别? 解法:烂大街之java语法题.此题被多少公司考过我不知道,反正我确实遇见过一次了. 代码: // 14 ...
随机推荐
- 修改Linux中发送邮件中附件大小的限制
方法一: 在命令中设定postfix的message_size_limit值 (但系统重启后会失效) postconf -e "message_size_limit = 20480000&q ...
- MySQL数据库实验二:单表查询
实验二 单表查询 一.实验目的 理解SELECT语句的操作和基本使用方法. 二.实验环境 是MS SQL SERVER 2005的中文客户端. 三.实验示例 1.查询全体学生的姓名.学号.所在系. ...
- redis网络模型
多路IO复用-非阻塞同步IO模型.见http://www.cnblogs.com/syyong/p/6231326.html 具体结构:http://blog.jobbole.com/100079/ ...
- Python 语法基础
之所以学习Python,第一个是他比较简单,寒假时间充裕,而且听说功能也很不错,最重要的是,我今年的项目就要用到它. 而且刘汝佳的书上说到,一个好的Acmer要是不会一点Python那就是太可惜了.废 ...
- 初始化mysql数据库时提示字符编码错误的解决办法
有时候在安装完数据库并初始化的时候会出现如下错误: root@localhost mysql-5.5.19]# bash scripts/mysql_install_db --user=mysql - ...
- 线程 Thread类 的四个构造方法简介
在线程中,Thread类有四个构造方法: 当我们使用 Thread类创建对象的时候,传入参数,就会用到构造方法.ThreadStart 和ParameterizedThreadStart 都是 委托类 ...
- redis list类型
- CentOS上安装mongodb
安装mongodb pymongo修改yum源vi /etc/yum.repos.d/10gen.repo[10gen]name=10gen Repositorybaseurl=http://down ...
- rcnn spp_net hcp
rcnn开创性工作,但是计算时间太长,重复计算太大. spp_net将重复计算避免了. hcp是yan shuicheng那边的,是用bing生成regions,然后用normalized cut将这 ...
- React后端管理系统-商品详情detail组件
import React from 'react'; import MUtil from 'util/mm.jsx' import Product from 'service/product-serv ...