2014-04-26 19:02

题目:解释下C++里模板和java里泛型的区别?

解法:我很少用java,属于连语法都不过关的程度。所以这个题还真没法详细答,查了些资料以后写了以下几点。

代码:

 // 14.4 tell me about the differences between C++ template and java generics.
// Answer:
// 1. C++ template can be used on built-in type and user-defined types, java generics can only be used on classes. Integer for int, Double for double.
// 2. java generics can put some restrictions on the <T>, such as <T extends superclass>, whereas this is not practical in C++.
// 3. You may use downcast instead of generics, but generics enhance the resuability of code, so is template in C++.
import java.util.Vector; public class TestJava<T> {
public T data; public TestJava(T data) {
// TODO Auto-generated constructor stub
this.data = data;
} String getType() {
return this.data.getClass().getName();
} public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.add(2);
v.add(1); TestJava<Vector<Integer>> testJava = new TestJava<Vector<Integer>>(v);
System.out.println(testJava.getType());
System.out.println(v);
}
}

《Cracking the Coding Interview》——第14章:Java——题目4的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第14章:Java——题目6

    2014-04-26 19:11 题目:设计一个循环数组,使其支持高效率的循环移位.并能够使用foreach的方式访问. 解法:foreach不太清楚,循环移位我倒是实现了一个,用带有偏移量的数组实现 ...

  8. 《Cracking the Coding Interview》——第14章:Java——题目5

    2014-04-26 19:06 题目:Java中的对象反射机制是什么?有鼠么用? 解法:完全不了解,因为java编程经验太少,完全没用过.查了一些资料后,感觉反射机制是个强大并需要边用边体会的强大工 ...

  9. 《Cracking the Coding Interview》——第14章:Java——题目3

    2014-04-26 18:59 题目:final.finally.finalize有什么区别? 解法:烂大街之java语法题.此题被多少公司考过我不知道,反正我确实遇见过一次了. 代码: // 14 ...

随机推荐

  1. linux上传、下载文件rz、sz命令

    1.介绍 sz命令是利用ZModem协议来从linux服务器传送文件到本地,一次可以传送一个或多个文件.相对应的从本地上传文件到Linux服务器,可以使用rz命令. 2.参数说明 -a,以文本方式传输 ...

  2. bzoj4836 [Lydsy2017年4月月赛]二元运算

    Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c  你需要求出有多少对 (i, j) ...

  3. Linux操作系统下的三种Java环境配置方法

    方法1:修改/etc/profile 文件  所有用户的 shell都有权使用这些环境变量 (1)在 shell终端执行命令:vi /etc/profile (2)在 profile文件末尾加入: e ...

  4. Getting aCC Error :name followed by "::" must be a class or namespace name"

    Getting aCC Error :name followed by "::" must be a class or namespace name" 原始是这样子的: ...

  5. ThinkPHP:create()方法有什么用呢?

    1.create方法可以对POST提交的数据进行处理(通过表中的字段名称与表单提交的名称对应关系自动封装数据实例),例如user表中有一个字段名叫"username",如果表单中有 ...

  6. URL中传递JSON数据

    有关于JSON如何在前后之间进行传递,转换成对象等操作,请查看  C#Json数据交互   (这篇文章主要介绍了如何转化Json格式的数据,以及如何使用) 我们在URL中传递数据一般都是 XX.asp ...

  7. HTML第三章:表单

    第三章:表单 表单标签form:<form></form>//相当于一张记录用户信息的单子    常用属性:method:表单的提交方式,常用的值有两个             ...

  8. 实现一个clone函数,对javascript中的5种数据类型进行值复制

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. element el-tooltip 内容换行的方法

    <el-tooltip class="item" effect="light" placement="top-start">  ...

  10. Java开发.gitignore文件包含.iml,.log的看法

    有一个开源项目https://github.com/github/gitignore 主要用来规范所有开发项目的.gitignore文件的编写,基本涵盖了所有的开发语言.开发环境等.今日我向JetBr ...