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.动态代理 代理是基本的设计模式之一,它是你为了提供额外的或不同的操作,而插入的用来代替“实际”对象的对象.这些操作通常涉及与“实际”对象的通信,因此代理通常充当着中间人的角色. 什么是代理模式? ...
随机推荐
- EPLAN 软件平台中的词“点“大全
1. 中断点(Interruption Point): 在原理图绘制时,如果当前绘图区域的空间不足,需要转到其它页面继续绘制,而这两页之间存在连续的"信息流"时,可以使用& ...
- NFC 与 Windows Phone 的那点事儿
说起NFC这个词儿应该已经不陌生了,在我们的生活中有很多使用场景都是使用的这项技术,例如公交卡,门禁,还有银联的闪付卡等等.并且近些年在移动设备上使用的场景也越来越多,例如 对 NFC TAG 的读写 ...
- UIView的响应链
父视图 和子视图的关联 只有当父视图 userInteractionEnabled=YES; 是其子视图才可响应 userInteractionEnabled=NO代表不接受响应 UIView的默认 ...
- Dbvisualizer9.0.6 解决中文乱码
一.设置编辑器的编码 Tools->Tools Properties ->General->File Encoding 设置为UTF-8 二.如果数据库为UTF-8,则要在连接时做以 ...
- 在线头像上传(js)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
到这里已经写到第12篇了,前11篇基本上把Spring MVC主要的内容都讲了,现在就直接上一个项目吧,希望能对有需要的朋友有一些帮助. 一.首先看一下项目结构: InfrastructureProj ...
- [Shell] 读取脚本路径
以下是几种在 Shell 中读取路径的方法. 返回当前工作目录绝对路径 echo $(pwd) 返回 shell 第一个参数.如果被执行对象位于 PATH 路径中,则返回该对象绝对路径:否则返回被执行 ...
- ListView不规律刷新多次,重复执行getView
写ListView的时候,有时会发现ListView中的getView执行多次,有的时候又不是,搞了半天才找到原因,在http://blog.csdn.net/danielinbiti/article ...
- java中产生对象的两种方式
/* * 普通new对象的过程! */ Person pp = new Person(); System.out.println(pp); /* * 利用代用参数的构造器产生对象实例! * 首先获得相 ...
- Hadoop下面WordCount运行详解
单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为MapReduce版"Hello World",该程序的完整代码可以在Hadoop安装包的"src/ ...