Java编程思想读书笔记之内部类
现在是够懒得了,放假的时候就想把这篇笔记写出来,一直拖到现在,最近在读《Java编程思想》,我想会做不止这一篇笔记,因为之前面试的时候总会问道一些内部类的问题,那这本书的笔记就从内部类开始说起。
一.为什么需要内部类
1.一个内部类的对象可以访问它的外围类的私有数据。
2.对于同一个包中的其他类来说,内部类能够隐藏起来。
3.使用内部类实现多重继承。
二.内部类重要提要
1.在方法内部或者方法内部的作用域的内部类
eg:方法内部的内部类
public interface InnerInterface {
String getStr1(String xxx);
}
public class Outer {
private InnerInterface getStr() {
class Inner implements InnerInterface {
String sss = ""; @Override
public String getStr1(String xxx) {
sss = xxx;
return sss;
}
} Inner inner = new Inner();
return inner;
} public static void main(String[] args) {
InnerInterface innerInterface = new Outer().getStr();
System.out.println(innerInterface.getStr1("1234"));
}
}
eg方法作用域的内部类
public interface Inface {
void sysout();
}public class Outer {
Inface getInface(boolean flag) {
if (flag) {
class Inner implements Inface{
@Override
public void sysout() {
System.out.println("876");
}
}
Inner inner = new Inner();
return inner;
} else {
class Inner implements Inface {
@Override
public void sysout() {
System.out.println("123");
}
}
Inner inner = new Inner();
return inner;
}
} public static void main(String[] args) {
Outer outer = new Outer();
Inface inface = outer.getInface(true);
inface.sysout(); inface = outer.getInface(false);
inface.sysout();
}
}2.匿名内部类。
匿名内部类只能使用一次,主要用来简化代码
但使用匿名内部类还有个前提条件:内部类必须继承一个父类或实现一个接口
eg:实现接口的demo
public class Outer {
private String str = "123"; public interface InnerCalss{
String value(String xxx);
} public InnerCalss getInnerCalss() {
return new InnerCalss() {
private String i;
public String value(String xxx){
i = xxx;
return i;
}
};
} public static void main(String[] args) {
Outer outer = new Outer();
System.out.println(outer.getInnerCalss().value("12345"));
}
}
eg:继承父类的的demo
public class Outer {
public abstract class InnerInterface {
abstract String getStr1(String xxx);
}
private InnerInterface getStr() {
return new InnerInterface() {
private String sss = "";
@Override
public String getStr1(String xxx) {
sss = xxx;
return sss;
}
};
} public static void main(String[] args) {
InnerInterface innerInterface = new Outer().getStr();
System.out.println(innerInterface.getStr1("1234"));
}
}
看着这个实现匿名内部类是不是觉得不是很常用下面的代码或许就知道原来自己一直在写匿名内部类,只是未察觉而已
public class ThreadInnerClass {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
System.out.println("1234567");
}
};
t.start();
}
}
public class RunnableInnerClass {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
System.out.print("1234567");
}
};
Thread t = new Thread(r);
t.start();
}
}
另外匿名内部类参数必须为final类型,这里借用书里的demo
public class Parcel10 {
public Destination
destination(final String dest, final float price) {
return new Destination() {
private int cost;
// Instance initialization for each object:
{
cost = Math.round(price);
if(cost > 100)
System.out.println("Over budget!");
}
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel10 p = new Parcel10();
Destination d = p.destination("Tasmania", 101.395F);
}
} /* Output:
Over budget!
*///:~
3.嵌套类
嵌套类主要是两种
(1)嵌套类将内部类用static修饰或者是接口内部类
eg:这里引用书中的demo
public class Parcel11 {
private static class ParcelContents implements Contents {
private int i = 11;
public int value() { return i; }
}
protected static class ParcelDestination
implements Destination {
private String label;
private ParcelDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
// Nested classes can contain other static elements:
public static void f() {}
static int x = 10;
static class AnotherLevel {
public static void f() {}
static int x = 10;
}
}
public static Destination destination(String s) {
return new ParcelDestination(s);
}
public static Contents contents() {
return new ParcelContents();
}
public static void main(String[] args) {
Contents c = contents();
Destination d = destination("Tasmania");
}
}
public interface ClassInInterface {
void howdy();
class Test implements ClassInInterface {
public void howdy() {
System.out.println("Howdy!");
}
public static void main(String[] args) {
new Test().howdy();
}
}
}
关于内部类就总结这些,因为这些平时很少用到,做下笔记,当做个备忘录吧
参考文档:《Java编程思想》第四版
Java编程思想读书笔记之内部类的更多相关文章
- JAVA编程思想读书笔记(五)--多线程
接上篇JAVA编程思想读书笔记(四)--对象的克隆 No1: daemon Thread(守护线程) 参考http://blog.csdn.net/pony_maggie/article/detail ...
- JAVA编程思想读书笔记(四)--对象的克隆
接上篇JAVA编程思想读书笔记(三)--RTTI No1: 类的克隆 public class MyObject implements Cloneable { int i; public MyObje ...
- JAVA编程思想读书笔记(三)--RTTI
接上篇JAVA编程思想读书笔记(二) 第十一章 运行期类型判定 No1: 对于作为程序一部分的每个类,它们都有一个Class对象.换言之,每次写一个新类时,同时也会创建一个Class对象(更恰当的说, ...
- JAVA编程思想读书笔记(二)--容器
接上篇JAVA编程思想读书笔记(一) 第八章.对象的容纳 No1: java提供了四种类型的集合类:Vector(矢量).BitSet(位集).Stack(堆栈).Hashtable(散列表) No2 ...
- Java编程思想读书笔记(一)【对象导论】
2018年1月7日15:45:58 前言 作为学习Java语言的经典之作<Java编程思想>,常常被人提起.虽然这本书出版十年有余,但是内容还是很给力的.很多人说这本书不是很适合初学者,我 ...
- Java编程思想读书笔记
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- <Java编程思想>读书笔记(1)-对象导论、一切都是对象
1.面向对象编程:OOP (Object-oriented Programming) 2.Alan Kay 总结的面向对象语言5个基本特性: 1) 万物皆为对象 2) 程序是对象的集合,他们通过发送消 ...
- Java编程思想读书笔记(二)【一切都是对象】
begin 2018年1月9日17:06:47 第二章 一切都是对象 Java语言假设我们只进行面向对象的程序设计. 2.1 用引用操纵对象 每种编程语言都有自己的操纵内存元素的方式 操纵内存元素的方 ...
- Java编程思想读书笔记--第14章类型信息
7.动态代理 代理是基本的设计模式之一,它是你为了提供额外的或不同的操作,而插入的用来代替“实际”对象的对象.这些操作通常涉及与“实际”对象的通信,因此代理通常充当着中间人的角色. 什么是代理模式? ...
随机推荐
- Mac OSX 安装nvm(node.js版本管理器)
我的系统 1.打开github官网https://github.com/,输入nvm搜索,选择creationix/nvm,打开 2.找到Install script,复制 curl -o- http ...
- C++中文件按行读取和逐词读取 backup
http://blog.csdn.net/zhangchao3322218/article/details/7930857 #include <iostream>#include &l ...
- NPM私有服务器架设 FOR CentOS
确保计算机能够连接互连网. 一.安装 Couchdb1.6 1.(CentOS 6.7)如果版本低于6.7请使用下面命令更新系统库. yum update 2.使用下面命令安装依赖库 yum inst ...
- MyBatis知多少(11)企业数据库
企业数据库比应用程序数据库更大,其外部影响也更大.它们与其他系统之间存在更多的关系,包括依赖关系和被依赖关系.这些关系可能是Web应用程序与报表工具之间的,但也很有可 能是与其他的复杂系统和数据库的接 ...
- (转)linux文件读写的流程
转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 在<linux内核虚拟文件系统浅析>这篇文章中,我们看到文件是如何被打开 ...
- ArcGIS平台中PostgreSQL数据连接配置总结
通常用户在使用要素服务时,要求数据必须是存放在空间数据库中的.同时,需要将数据库注册到ArcGIS for Server,这样在发布服务时就不需要进行数据拷贝,从而可以节省磁盘空间及服务发布时间.以下 ...
- 轻量型ORM框架Dapper的使用
在真实的项目开发中,可能有些人比较喜欢写SQL语句,但是对于EF这种ORM框架比较排斥,那么轻量型的Dapper就是一个不错的选择,即让你写sql语句了,有进行了关系对象映射.其实对于EF吧,我说下我 ...
- OP和DBA相关的一些有用资源
最近国外blog上看到的一片资源分享博文,精而全,于是转帖分享 Must-Read Books List First of all, I would like to share a list of b ...
- 利用 word2vec 训练的字向量进行中文分词
最近针对之前发表的一篇博文<Deep Learning 在中文分词和词性标注任务中的应用>中的算法做了一个实现,感觉效果还不错.本文主要是将我在程序实现过程中的一些数学细节整理出来,借此优 ...
- NoSuchMethodError: antlr.collections.AST.getLine()I
错误完整表述: Filter execution threw an exception] with root cause java.lang.NoSuchMethodError: antlr.coll ...