Java编程思想:内部类中级部分
public class Test {
public static void main(String[] args) {
// Parcel4Test.test();
// Parcel5_1.test();
// Parcel6.test();
// Parcel8.test();
// Parcel9.test();
// AnonymousConstructor.test();
Parcel10.test();
}
}
/*
当内部类向上转型为其基类,尤其是转型为一个接口时,内部类就有了用武之地。
(从实现了某个接口的对象,得到对此接口的引用,与向上转型为这个对象的基类
实质上效果是一样的。)这是因为此内部类——某个接口的实现——能够完全不可
见,并且不可用。所得到的只是指向基类或者接口的引用,能方便的隐藏实现细节
*/
interface Destination{
String readLable();
}
interface Content {
int value();
}
class Parcel4 {
private class PContents implements Content {
private int i = 11;
@Override
public int value() {
return i;
}
}
protected class PDestination implements Destination {
private String label;
@Override
public String readLable() {
return label;
}
public PDestination(String label) {
this.label = label;
}
}
//这个地方就发生了向上转型
public Destination to(String toWhere) {
return new PDestination(toWhere);
}
//这个地方就发生了向上转型
public Content contents() {
return new PContents();
}
}
/*
private 内部类给类的设计者提供了一种途径,通过这种方式完全可以阻止任何依赖于类型
的编码,并且完全隐藏了实现细节。
*/
class Parcel4Test {
public static void test() {
Parcel4 p = new Parcel4();
Content c = p.contents();
Destination d = p.to("E");
//无法访问
//Parcel4.PContents pc = p.new PContents();
}
}
/*
定义在方法中的类:
在destination()中定义了内部类PDestination,并不代表一点destination()方法
执行完毕了,PDestination就不可用了。
——如果,这个内部内访问了方法里的参数的话,会怎样呢?
*/
class Parcel5 {
public Destination destination(String s) {
/*
实验:看是否方法内部的类,能不能访问方法里的数据
结论:
可以访问?
——那这个方法消失后,这些数据会怎样呢?
*/
int h = 100;
class PDestination implements Destination {
private String Label;
private int a;
public PDestination(String label) {
a = h;
Label = label;
}
@Override
public String readLable() {
return Label;
}
}
return new PDestination("F");
}
public static void test() {
Parcel5 p = new Parcel5();
Destination d = p.destination("H");
}
}
/*
实验:
1.定义在方法内部的内,能否访问方法里的参数
2.方法执行完毕后,这个内部内还能访问这些参数么
3.如果方法里的参数是基本数据类型(储存在栈中),还可以访问么
结论:
可以访问哦,这就有点恐怖了!这有点香Lua中的闭包了,我能理解它,但是
这和我在C++里学的有很大的出入。
*/
class Parcel5_1 {
public Destination destination(String s) {
int[] arr = new int[100];
int a = 10;
for (int i = 0; i < 100; i++) {
arr[i] = i;
}
class PDestination implements Destination {
private String Label;
private int i = 0;
public PDestination(String label) {
Label = label;
}
@Override
public String readLable() {
// return arr[i++]+"";
return a+"";
}
}
return new PDestination("F");
}
public static void test() {
Parcel5_1 p = new Parcel5_1();
Destination d = p.destination("H");
// System.out.println("f:"+d.readLable());
// System.out.println("s:"+d.readLable());
// System.out.println("t:"+d.readLable());
System.out.println("a:"+d.readLable());
System.out.println("a:"+d.readLable());
System.out.println("a:"+d.readLable());
}
}
/*
定义在作用域的类,此作用域在方法的内部
*/
class Parcel6 {
private void internalTracking(boolean b){
if(b){
class TrackingSlip{
private String id;
TrackingSlip(String s) {
id = s;
}
String getSlip() {
return id;
}
}
TrackingSlip ts = new TrackingSlip("slip");
String s = ts.getSlip();
}
}
public void track() {
internalTracking(true);
}
public static void test() {
Parcel6 p = new Parcel6();
p.track();
}
}
/*
实现了接口的匿名类
*/
class Parcel7 {
public Content contents() {
return new Content() {
private int i = 11;
@Override
public int value() {
return i;
}
};
}
public static void test() {
Parcel7 p = new Parcel7();
Content c = p.contents();
}
}
/*
一个匿名类,它扩展了有非默认构造器的类
你的基类需要一个有参数的构造器,应该怎么办?
*/
class Wrapping {
private int i;
public Wrapping(int x) {
this.i = x;
}
public int value() {
return i;
}
}
class Parcel8 {
public Wrapping wrapping(int x) {
return new Wrapping(x){
public int values() {
return super.value() * 47;
}
};
}
public static void test() {
Parcel8 p = new Parcel8();
Wrapping w = p.wrapping(10);
System.out.println("w.value():"+w.value());
}
}
/*
一个匿名类,它执行字段的初始化
如果定义一个匿名内部类,并希望它使用一个在外部定义的对象,那么编译器会要求其参数的引用
是final的,就像你在destination()中看到的那样。
*/
class Parcel9 {
public Destination destination(final String dest) {
return new Destination() {
private String label = dest;
@Override
public String readLable() {
return label;
}
};
}
//实际上,我没有传入一个final值,编译器没有报任何错误,时代不一样了
public Destination destination2(String dest) {
return new Destination() {
private String label = dest;
@Override
public String readLable() {
return label;
}
};
}
public static void test() {
Parcel9 p = new Parcel9();
Destination d1 = p.destination("CCC");
Destination d2 = p.destination2("HHH");
System.out.println("d1:"+d1.readLable());
System.out.println("d2:"+d2.readLable());
}
}
/*
一个匿名类,它通过实例初始化实现构造(匿名类不可能有构造器)
需求:
如果想做一些类似的构造器的行为,该怎么办?在匿名类中不可能有命名的构造器
(因为它根本没有名字!),但通过实例初始化,就能够达到为匿名内部类创造一
个构造器的效果
*/
abstract class Base {
public Base(int i) {
System.out.println("Base constructor i = " + i);
}
public abstract void f();
}
class AnonymousConstructor {
public static Base getBase(int i) {
return new Base(i) {
{
System.out.println("Inside instance initializer.");
}
@Override
public void f() {
System.out.println("In anonymous f()");
}
};
}
public static void test() {
Base base = getBase(47);
base.f();
}
}
class Parcel10 {
public Destination destination(final String dest, final float price) {
return new Destination() {
private int cost;
{
cost = Math.round(price);
if (cost > 100) {
System.out.println("Over budget!");
}
}
private String label = dest;
@Override
public String readLable() {
return label;
}
};
}
//测试不用final关键字会怎样
public Destination destination2(String dest,float price) {
return new Destination() {
private int cost;
{
cost = Math.round(price);
if (cost > 100) {
System.out.println("Over budget!");
}
}
private String label = dest;
@Override
public String readLable() {
return label;
}
};
}
public static void test() {
Parcel10 p = new Parcel10();
Destination d1 = p.destination("AAA", 10.f);
Destination d2 = p.destination2("BBB", 11.f);
}
}
Java编程思想:内部类中级部分的更多相关文章
- Java编程思想学习(八) 内部类
可以将一个类的定义放在另一个类的定义内部,这就是内部类. 内部类的定义是简单的,但是它的语法确实很是复杂,让人不是很好理解.下面就内部类做一个小结. 一.内部类的分类 总的来讲内部类分为普通内部类,匿 ...
- Java编程思想重点笔记(Java开发必看)
Java编程思想重点笔记(Java开发必看) Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而 ...
- Java编程思想读书笔记之内部类
现在是够懒得了,放假的时候就想把这篇笔记写出来,一直拖到现在,最近在读<Java编程思想>,我想会做不止这一篇笔记,因为之前面试的时候总会问道一些内部类的问题,那这本书的笔记就从内部类开始 ...
- java编程思想
Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而且在大型项目开发中也是常用的知识,既有简单的概念理 ...
- Java编程思想 (1~10)
[注:此博客旨在从<Java编程思想>这本书的目录结构上来检验自己的Java基础知识,只为笔记之用] 第一章 对象导论 1.万物皆对象2.程序就是对象的集合3.每个对象都是由其它对象所构成 ...
- Java 编程思想 Chapter_14 类型信息
本章内容绕不开一个名词:RTTI(Run-time Type Identification) 运行时期的类型识别 知乎上有人推断作者是从C++中引入这个概念的,反正也无所谓,理解并能串联本章知识才是最 ...
- Java编程思想(前十章)
Java编程思想 有C++编程基础的条件下, 前10章可以快速过一下,都是基本语法,不需要花太多时间. 着重中后段的一些章节,类型信息.泛型.容器.IO.并发等. 中文翻译版 阅读地址 对于一个架构师 ...
- 《Java编程思想第四版完整中文高清版.pdf》-笔记
D.2.1 安插自己的测试代码 插入下述“显式”计时代码,对程序进行评测: long start = System.currentTimeMillis(); // 要计时的运算代码放在这儿 long ...
- 《Java编程思想第四版》附录 B 对比 C++和 Java
<Java编程思想第四版完整中文高清版.pdf>-笔记 附录 B 对比 C++和 Java “作为一名 C++程序员,我们早已掌握了面向对象程序设计的基本概念,而且 Java 的语法无疑是 ...
- Java编程思想中关于闭包的一个例子
Java编程思想中的一个例子,不是很理解使用闭包的必要性,如果不使用闭包,是不是有些任务就不能完成?继续探索. package InnerClass; interface Incrementable ...
随机推荐
- GlusterFS集群文件系统研究(负载均衡的最常用办法) good
http://blog.csdn.net/liuaigui/article/details/6284551 http://blog.csdn.net/liuaigui/article/details/ ...
- JS 浮点加减乘除运算
//浮点数加法运算 function FloatAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(".")[1]. ...
- 关于XML异步
记得有次面试的时候面试官问我知道AJAX吗?当时我回答听过但是没去看过,当时只是知道它和异步的概念有关. 经过查资料,弄明白了些头绪,下面就把我自己对AJAX的理解说说. 大多数浏览器是支持XMLHt ...
- Linux之文件的压缩与解压缩
压缩格式 .zip,.rar,.7z,.tar,.gz,.xz,.bz2,.tar.gz,.tar.xz,.tar.bz2,其中,形如*.tar.gz为tar打包,gz压缩的文件 zip压缩打包程序 ...
- 【Windows Universal Platform】只是学习笔记 - 开始
我是初学,之前没有windows/windows phone的应用开发经验:开博的目的只是记录和督促自己学习. 心血来潮也好,或是个人喜好的原因,想学着自己开发APP了(PS:以前做过web 开发) ...
- VC6下 try catch 在release下的杯具(默认情况下,要加上throw语句catch才不会被优化掉)
IDE:VC6 今天遇到一个小问题,把我郁闷了好久,××医生的VulEngine不时在wcsstr处发生crash,加了一番强大的参数检查后,再加上了强大的try catch,其实不是很喜欢用try和 ...
- 一道经典的js面试题
# 声明:学习编程语言最好的方式就是通过实例学习 ## 下面是我在博客上看到的一道js面试题,可以说非常经典,下面会以最简单的方式让你理解题目:```bashfunction Foo() { getN ...
- javascript (java)动态时钟
<script language="javascript"> var t = null; t = setTimeout(time,1000);//开始执行 functi ...
- 使用wireshark抓包分析-抓包实用技巧
目录 使用wireshark抓包分析-抓包实用技巧 前言 自定义捕获条件 输入配置 输出配置 命令行抓包 抓取多个接口 抓包分析 批量分析 合并包 结论 参考文献 使用wireshark抓包分析-抓包 ...
- Babel是什么?
要是官方文档写得好的话,我也许就不用自己做个笔记. 官方文档 Babel 是一个工具链,主要用于将 ECMAScript 2015+ 版本的代码转换为向后兼容的 JavaScript 语法,以便能够运 ...