内部类(Nested Class)##

内部类:即在一个类中还包含着另外一个类,一般是作为匿名类或者是使用数据隐藏时使用的.例子:

//内部类
class Out{
private int age = 12;
class In {
public void print(){
System.out.println(age);
}
}
}
public class Demo{
public static void main(String[] args){
Out.In in = new Out().new In();
in.print();
//或者如下调用:
/*
Out out = new Out();
out.In = out.new In();
in.print();
*/
}
}

在编译后产生两个.class文件,分别是Out.class和Out\(In.class,所以\)代表了内部类的含义.需要注意亮点:

  1. 开头的Out就是为了表示用来生成的内部类是属于哪个外部类的;
  2. 要生成内部类的对象就必须先得到外部类的对象,内部类的作用就是来访问外部类的中的成员变量.

内部类中的变量访问方式####

class Out{
private int age = 12;
class In{
private int age = 13;
public void print(){
int age = 14;
System.out.println("局部变量: " + age);
System.out.println("内部类变量: " + this.age);
System.out.println("外部类变量: " + Out.this.age);
}
}
}
public class Demo{
public static void main(String[] args){
Out.In in = new Out().new In();
in.print();
//或者如下调用:
/*
Out out = new Out();
out.In = out.new In();
in.print();
*/
}
} == 运行结果 ==
局部变量 : 14
内部类变量 : 13
外部类变量 : 12

如上可以看出,在内部类中如果没有同名变量,那么可以直接访问外部类中的变量,但是在内部类中如果存在同名变量,则必须使用this指针访问内部类的成员,而用OutClassName.this来访问外部类的成员变量.

静态内部类####

如果使用static来修饰内部类,即内部类为静态类.那么同普通静态类一样,内部类内只能使用static类型的成员变量,同时也只能访问外部类中的静态变量.此时,应将上面例子中的age前加上static.

另: 由于内部类被静态化,因此可以将Out.In当为一个整体看,即不必生成Out类的对象也可以直接访问内部类.

私有内部类####

class Out{
private int age = 12;
private class In{
public void print(){
System.out.printLn(age);
}
}
public void outPrint(){
//System.out.println(age);
new In().print();
}
} public class Demo{
public static void main(String[] args){
//下面方法无效
/* Out.In in = new Out().In()
* in.print();
*/
Out out = new Out();
out.outPrint();
}
}

当使用private修饰内部类时,类的可访问属性大于下面任意成员函数的属性,所以即使私有内部类的成员函数的访问权限是public,私有内部类也仅仅能被Out类中的成员函数访问,外部不可见.

方法内部类####

在一个函数内部定义,在函数作用域外均不可见.

class Out{
private int age = 12;
public void outPrint(funal int x){
class In{
public void inPrint(){
System.out.println(x);
System.out.println(age);
}
}
new In().inPrint();
}
} public class Demo{
public static void main(String[] args){
Out out = new Out();
out.outPrint();
}
}
== 运行结果 ==
3
12

内部类的作用域仅仅限于函数内部,因此在定义完函数之后,只能在同一个函数中进行调用.同时从外部传递参数必须使用final修饰.

内部接口(Nested/Inner Interface)

访问权限:

有关访问权限,内部接口遵循以下原则:

  • 不管内部接口是定义在类还是在接口中,内部接口都隐式地声明为static。
  • 在接口中定义的内部接口隐式地定义为public
  • 在类中定义的内部接口可以使用任何访问权限的修饰符修饰
  • 只要可见,内部接口就可以被任意类实现

所以在接口中定义接口,得到的一定是public static的。

/* NestedInterfaceDemo.java */
interface A
{
interface NestedA { void aMethod(); } // modifier public and static are placed for
// demonstration, in real life avoid placing modifiers that
// are implicit to declarations
public static interface NestedAA { void aaMethod(); }
} public class NestedInterfaceDemo implements A.NestedA, A.NestedAA
{
public static void main (String args[])
{
A.NestedA na = new NestedInterfaceDemo();
na.aMethod();
A.NestedAA naa = (A.NestedAA) na;
naa.aaMethod();
} public void aMethod()
{
System.out.println("within from aMethod");
} public void aaMethod()
{
System.out.println("within from aaMethod");
}
} OUTPUT
======
D:\JavaPrograms>javac NestedInterfaceDemo.java
D:\JavaPrograms>java NestedInterfaceDemo
within from aMethod
within from aaMethod

而当内部接口定义在类中时,则可以用访问限制符修饰以限制其访问。extends的接口也必须要实现,同内部类,如果不是static的需要在new Outer().new Inner(),这样的方式才能实例化,而静态类则是不需要实例化外部类对象就可以访问内部类的构造函数,

同样的道理,在静态类(函数)中,需要保证静态,所以如果访问实例变量则需要当场实例化外部类才能访问,否则只能访问static变量。

class A
{
private interface NestedPA { void paMethod(); }
protected interface NestedA extends NestedPA { void aMethod(); }
public interface NestedAA { void aaMethod(); }
} public class NestedInterfaceDemo implements A.NestedA, A.NestedAA
{
public static void main (String args[])
{
A.NestedA na = new NestedInterfaceDemo();
na.aMethod();
na.paMethod(); A.NestedAA naa = (A.NestedAA) na;
naa.aaMethod();
} public void aMethod()
{
System.out.println("within from aMethod");
} public void aaMethod()
{
System.out.println("within from aaMethod");
} public void paMethod()
{
System.out.println("within from paMethod");
}
} OUTPUT
======
D:\JavaPrograms>javac NestedInterfaceDemo.java D:\JavaPrograms>java NestedInterfaceDemo
within from aMethod
within from paMethod
within from aaMethod

综合例子:

package com.lyb.Section4;

/**
* Created by lyb on 15-7-31.
*/
public class InnerInterfaceTest implements A.NestedA, A.NestedAA{
public static void main(String[] args){
InnerInterfaceTest a = new InnerInterfaceTest();
((A.NestedA)a).pamethod();
a.aamethod(); InnerInterfaceTest.Bnonstatic bnonstatic = new InnerInterfaceTest().new Bnonstatic();
bnonstatic.BnonstaticMethod(); InnerInterfaceTest.B b = new InnerInterfaceTest.B();
b.BstaticMethod();
} private int tt = 45;
private static int ttt = 89; static class B{
void BstaticMethod(){
System.out.printf("%d,static %d \n",new InnerInterfaceTest().tt,ttt);
}
} class Bnonstatic{
void BnonstaticMethod(){
System.out.printf("%d, static %d in nonstatic\n",tt,ttt);
}
} public void pamethod(){
System.out.printf("This is the private Amethod. \n");
} @Override
public void amethod() {
System.out.printf("This is the protected Amethod. \n");
} @Override
public void aamethod() {
System.out.printf("This is the public AAmethod. \n");
}
} class A{
private interface NestedPA{
void pamethod();
} protected interface NestedA extends NestedPA{
void amethod();
} public interface NestedAA{
void aamethod();
}
}

最终,内部类和内部接口最先的作用就是提高封装性,解决命名空间的问题的。

Java-NestedClass(Interface).的更多相关文章

  1. Java Native Interface 六JNI中的异常

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 在这里只讨论调用JNI方法可能会出现的异常, ...

  2. Java Native Interface 五 JNI里的多线程与JNI方法的注册

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 JNI里的多线程 在本地方法里写有关多线程的 ...

  3. Java Native Interface 四--JNI中引用类型

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 JNI支持将类实例和数组类型(如jobjec ...

  4. Java Native Interface 二 JNI中对Java基本类型和引用类型的处理

    本文是<The Java Native Interface Programmer's Guide and Specification>读书笔记 Java编程里会使用到两种类型:基本类型(如 ...

  5. Java Native Interface 编程系列一

    本文是<Java Native Interface Programmer's Guide and Specification>的读书笔记 Java Native Interface可以让编 ...

  6. Java注释@interface的用法【转】

    Java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@SuppressWarnings为 ...

  7. Java注释@interface的用法

    转---------- java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@Suppr ...

  8. Java Native Interface Specification—Contents

    http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html 1. Introduction Java Nati ...

  9. android 学习随笔二十七(JNI:Java Native Interface,JAVA原生接口 )

    JNI(Java Native Interface,JAVA原生接口) 使用JNI可以使Java代码和其他语言写的代码(如C/C++代码)进行交互. 问:为什么要进行交互? 首先,Java语言提供的类 ...

  10. Java Interface是存放常量的最好地方吗?(转)

    虽然Inteface中默认常量就是static final 的.因此很多人就认为其实存放常量的最佳地方,effective java中就提过,不建议使用常量接口.其有一个原因就是:代码编译问题 好,我 ...

随机推荐

  1. httpclient:实现有验证码的模拟登陆

    //1.这种方式是先把验证码的图片下载到本地.并且根据网页解析获得token值//2.手动在控制台输入验证码//3.因为验证码图片已经下载下来,后面就可以使用图像文字识别package DoubanS ...

  2. yzm10铺瓷砖 一只小蜜蜂 ycb与取款机

    yzm10铺瓷砖 一天yzm10接到任务,要求用2×1大小的瓷砖,来铺2×4的地面,地面需要恰好被铺满.这对yzm10来说太容易了,于是他马上设计出了5种不同的铺法(旋转情况算不同种,如图示2.4). ...

  3. jQuery 字母大小写转换

    "ABC".toLowerCase()//转小写 "abc".toUpperCase()//转大写

  4. 利用jstack定位典型性能问题实例

    此文已由作者朱笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 问题的起因是笔者在一轮性能测试的中,发现某协议的响应时间很长,去观察哨兵监控里的javamethod监控可以 ...

  5. CodeForces 363D 【二分+贪心】

    思路: 一开始是没有办法贪心的,但是答案是可以二分的,因为窝能买k辆车的话,窝就一定能买k-1辆车:而且我最好就是拿手上钱较多的那些人去买价格便宜的车,这样肯定是能买到最多的车,而且花的少,因为对于要 ...

  6. layui 关闭当前窗口,刷新父级页面

    关闭当前窗口 layer.close(layer.index); 刷新页面 window.parent.location.reload();

  7. 始终要覆盖toString

    始终要覆盖toString   虽然java.lang.Object提供了toString方法的一个实现,但它返回的字符串通常并不是类的用户所期望看到的.它包含类的名称,以及一个"@&quo ...

  8. C#递归得到特定文件夹下问件

    List<String> listFile = new List<String>(); public void director(string path) { //绑定到指定的 ...

  9. SQL基础培训实战教程[全套]

    学习简介:林枫山根据网上搜索资料进行参考,编写制作的SQL Server实操学习教程,欢迎下载学习. 下载链接目录如下: 进度0-SQL基础语法    下载学习文档 进度1-建数据表-美化版-2018 ...

  10. 码云最火爆开源项目 TOP 50,你都用过哪些?

    前 20 名预览 排名软件排名软件 1zheng11AOSuite 2JFinal12Spiderman 3t-io13AG-Admin 4guns14renren-security 5hutool1 ...