现在是够懒得了,放假的时候就想把这篇笔记写出来,一直拖到现在,最近在读《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编程思想读书笔记之内部类的更多相关文章

  1. JAVA编程思想读书笔记(五)--多线程

    接上篇JAVA编程思想读书笔记(四)--对象的克隆 No1: daemon Thread(守护线程) 参考http://blog.csdn.net/pony_maggie/article/detail ...

  2. JAVA编程思想读书笔记(四)--对象的克隆

    接上篇JAVA编程思想读书笔记(三)--RTTI No1: 类的克隆 public class MyObject implements Cloneable { int i; public MyObje ...

  3. JAVA编程思想读书笔记(三)--RTTI

    接上篇JAVA编程思想读书笔记(二) 第十一章 运行期类型判定 No1: 对于作为程序一部分的每个类,它们都有一个Class对象.换言之,每次写一个新类时,同时也会创建一个Class对象(更恰当的说, ...

  4. JAVA编程思想读书笔记(二)--容器

    接上篇JAVA编程思想读书笔记(一) 第八章.对象的容纳 No1: java提供了四种类型的集合类:Vector(矢量).BitSet(位集).Stack(堆栈).Hashtable(散列表) No2 ...

  5. Java编程思想读书笔记(一)【对象导论】

    2018年1月7日15:45:58 前言 作为学习Java语言的经典之作<Java编程思想>,常常被人提起.虽然这本书出版十年有余,但是内容还是很给力的.很多人说这本书不是很适合初学者,我 ...

  6. Java编程思想读书笔记

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. <Java编程思想>读书笔记(1)-对象导论、一切都是对象

    1.面向对象编程:OOP (Object-oriented Programming) 2.Alan Kay 总结的面向对象语言5个基本特性: 1) 万物皆为对象 2) 程序是对象的集合,他们通过发送消 ...

  8. Java编程思想读书笔记(二)【一切都是对象】

    begin 2018年1月9日17:06:47 第二章 一切都是对象 Java语言假设我们只进行面向对象的程序设计. 2.1 用引用操纵对象 每种编程语言都有自己的操纵内存元素的方式 操纵内存元素的方 ...

  9. Java编程思想读书笔记--第14章类型信息

    7.动态代理 代理是基本的设计模式之一,它是你为了提供额外的或不同的操作,而插入的用来代替“实际”对象的对象.这些操作通常涉及与“实际”对象的通信,因此代理通常充当着中间人的角色. 什么是代理模式? ...

随机推荐

  1. The file 'MemoryStream' is corrupted! 的解决办法

    The file 'MemoryStream' is corrupted! Remove it and launch unity again! [Position > ] 有时候我们会遇到这个报 ...

  2. Sass for循环中编译%时报错解决方案

    sass功能强大,特别是支持for循环,节省大量开发时间,但是在开发时遇到一个问题,直接使用%时没有问题,当有变量时再加% 单位在编译时报错: 这样没有问题: @for $width from 0 t ...

  3. toad 常用快捷键与配置

    F8 调出以前执行的sql命令 F9 执行全部sql Ctrl+. 补全table_name Ctrl+t 补全table_name,或者显示字段 alt+ 箭头上下 看sql history Ctr ...

  4. 【转】ContextMenuStrip菜单应用

    测试可用的代码: #region 右键快捷菜单单击事件 private void contextMenuStrip1_ItemClick(object sender, EventArgs e) { T ...

  5. int.class 与 Integer.class

    TYPE 表示的引用类型所对应的基本类型的Class对象!

  6. [ML] Naive Bayes for Text Classification

    TF-IDF Algorithm From http://www.ruanyifeng.com/blog/2013/03/tf-idf.html Chapter 1, 知道了"词频" ...

  7. [转载]我们可以用SharePoint做什么

    前言 不知不觉作为一个SharePoint的开发人员若干年了,从SharePoint API开始学习,到了解SharePoint的结构,逐渐一点点了解sharepoint的体系:从SharePoint ...

  8. Sequence.js - 适合电子商务网站的图片滑块

    Sequence.js 是一个非常现代的图片滑动效果,特别适合电子商务网站或者企业产品展示功能.带有图片缩率图,能够呈现全屏图片浏览效果.结合 CSS3 Transition 实现响应式的滑块效果. ...

  9. Laravel 5 事件的使用

    事件类通常被保存在 app/Events 目录下,而它们的处理程序则被保存在 app/Handlers/Events 目录下. 事件的创建 下面我们用artisan来创建一个事件,比如叫CqhTest ...

  10. 对于Discuz!NT不允许新用户注册的解决办法

    客户论坛用的是Discuz!NT,但是用户注册总是提示不允许新用户注册,对于这个问题,网上好多说的是管理员登录后台,在"用户与访问控制"里将允许新用户注册改为"是&quo ...