`

java内部类的学习笔记-总结-考试

阅读更多
内部类大致分五种情况:静态成员类(static member class),成员类(member class),静态局部类(static local class),局部类(local class),匿名类(anonymous class),下面我一一实例讲解:
package com.fruitking.innerclass;
/**
* 静态成员类:类声明中包含"static"关键字的内部类。
* 静态成员类有public/private/default权限修饰符。
* 静态成员类的使用方式与一般顶层类的使用方式基本相同。
* 静态成员类只能定义于外部类的顶层代码或外部类其它静态成员类的顶层代码中(嵌套定义);不能定义于外部类的某个函数中。
* 外部类的静态成员属性、静态方法可以供内部类直接调用。
* 外部类的非静态成员属性、非静态方法不能被内部类调用(没有实例引用)。
* 内部类的静态成员属性、静态方法可以供外部类调用(内部类前缀加调用属性名称或方法名称)。
* 内部类的非静态成员属性、非静态方法需要实例化一个内部类才能被这个内部类实例所调用。
*/
public class TestStaticInnerClass {
private static String OUTCLASS_STATIC_VARIABLE_NAME = "OUTCLASS_STATIC_VARIABLE_NAME";
public String outVariableName = "out Variable Name";
private static String outStaticMethod(){
System.out.println("Out class static method!");
return "invoke out class' outStaticMethod()";
}
private static String innerStaticMethodByOutStaticMethod(){
System.out.println("Out Static Method invoke inner Static Method!");
return StaticInnerClass.innerStaticMethod();
}
private static String innerNonStaticMethodByOutStaticMethod(){
System.out.println("Out Static Method invoke inner not Static Method!");
System.out.println(new StaticInnerClass().innerVariableName);
return new StaticInnerClass().innerNonStaticMethod();
}
public static class StaticInnerClass{
public static String INNERCLASS_STATIC_VARIABLE_NAME = "INNERCLASS_STATIC_VARIABLE_NAME";
public static String INNERCLASS_OUT_STATIC_VARIABLE_NAME = OUTCLASS_STATIC_VARIABLE_NAME;
public String innerVariableName = "inner Variable Name";
public String innerNonStaticMethod(){
//compile error:Cannot make a static reference to the non-static field outVariableName
//System.out.println(outVariableName);
System.out.println("inner not-static method!");
return "invoke inner class not-static method!";
}
public static String innerStaticMethod(){
System.out.println("inner class method!");
return "return inner class name";
}
public static String outStaticMethodByInnerStaticMethod(){
System.out.println("Inner Static Method invoke out Static Method!");
return outStaticMethod();
}
}
public static void main(String[] args) {
System.out.println(TestStaticInnerClass.innerStaticMethodByOutStaticMethod());
System.out.println(TestStaticInnerClass.innerNonStaticMethodByOutStaticMethod());
System.out.println(TestStaticInnerClass.StaticInnerClass.INNERCLASS_STATIC_VARIABLE_NAME);
System.out.println(TestStaticInnerClass.StaticInnerClass.INNERCLASS_OUT_STATIC_VARIABLE_NAME);
System.out.println(TestStaticInnerClass.StaticInnerClass.innerStaticMethod());
System.out.println(TestStaticInnerClass.StaticInnerClass.outStaticMethodByInnerStaticMethod());
StaticInnerClass inner = new TestStaticInnerClass.StaticInnerClass();
System.out.println(inner.innerVariableName);
System.out.println(inner.innerNonStaticMethod());
}
}
package com.fruitking.innerclass;

/**
* 一个静态成员类,若去掉"static"关键字,就成为成员类。
* 类似于外部类的实例函数,成员类有public/private/default权限修饰符.
* 一个成员类实例必然所属一个外部类实例,成员类可访问外部类的任一个实例字段和实例函数。
* 不能在成员类中定义static字段、方法和类(static final形式的常量定义除外)。
* 成员类不能是接口(interface)。
*/
public class TestNonStaticInnerClass {
private static String OUTCLASS_STATIC_VARIABLE_NAME = "OUTCLASS_STATIC_VARIABLE_NAME";
public String outVariableName = "out Variable Name";
private String sameVariableName = "out Variable Name";
private static String outStaticMethod(){
System.out.println("Out class static method!");
return "invoke out class' outStaticMethod()";
}
private String outNotStaticMethod(){
System.out.println("Out class non-static method!");
return "invoke out class' outNotStaticMethod()";
}
private String outNonStaticMethodByInnerClass(){
System.out.println("Out class non-static method!");
//一下两行代码是等价的
StaticInnerClass innerClass = TestNonStaticInnerClass.this.new StaticInnerClass();
innerClass = this.new StaticInnerClass();
System.out.println("inner Variable INNERCLASS_STATIC_FINAL_VARIABLE_NAME:" + StaticInnerClass.INNERCLASS_STATIC_FINAL_VARIABLE_NAME);
System.out.println("inner Variable innerVariableName:" + innerClass.innerVariableName);
System.out.println("inner Variable sameVariableName:" + innerClass.sameVariableName);
System.out.println("invoke inner class non-static method'innerNonStaticMethod:" + innerClass.innerNonStaticMethod());
System.out.println("invoke inner class non-static private method'innerNonStaticPrivateMethod:" + innerClass.innerNonStaticPrivateMethod());
return "invoke out class' outStaticMethod()";
}
private static String outStaticMethodByInnerClass(){
System.out.println("Out class non-static method!");
//静态外部方法不能调用非静态内部类,this不能这么使用
//System.out.println("invoke inner class non-static method'innerNonStaticMethod:" + this.new StaticInnerClass().innerNonStaticMethod());
//那么先new一个外部类,然后在new内部类,这样可以使用,但使用意义估计不大
TestNonStaticInnerClass outClass = new TestNonStaticInnerClass();
StaticInnerClass innerClass = outClass.new StaticInnerClass();
System.out.println("inner Variable innerVariableName:" + innerClass.innerVariableName);
System.out.println("inner Variable sameVariableName:" + innerClass.sameVariableName);
System.out.println("invoke inner class non-static method'innerNonStaticMethod:" + innerClass.innerNonStaticMethod());
System.out.println("invoke inner class non-static private method'innerNonStaticPrivateMethod:" + innerClass.innerNonStaticPrivateMethod());
return "invoke out class' outStaticMethod()";
}
public class StaticInnerClass{
//compile error:The field INNERCLASS_STATIC_VARIABLE_NAME cannot be declared static;
//static fields can only be declared in static or top level types
//public static String INNERCLASS_STATIC_VARIABLE_NAME = "INNERCLASS_STATIC_VARIABLE_NAME";
//compile error:The method innerStaticMethod cannot be declared static;
//static methods can only be declared in a static or top level type
//public static String innerStaticMethod(){
// return "";
//}
public static final String INNERCLASS_STATIC_FINAL_VARIABLE_NAME = "INNERCLASS_STATIC_FINAL_VARIABLE_NAME";
public String innerVariableName = "inner Variable Name";
public String innerVariableOutName = outVariableName;
public String sameVariableName = "inner Variable Name";
public String innerNonStaticMethod(){
System.out.println("inner not-static method!");
System.out.println("out Variable OUTCLASS_STATIC_VARIABLE_NAME:" + OUTCLASS_STATIC_VARIABLE_NAME);
System.out.println("out Variable outVariableName:" + outVariableName);
System.out.println("out Variable sameVariableName:" + TestNonStaticInnerClass.this.sameVariableName);
System.out.println("invoke out class static method'outStaticMethod:" + outStaticMethod());
System.out.println("invoke out class non-static method'outNotStaticMethod:" + outNotStaticMethod());
System.out.println("inner Variable INNERCLASS_STATIC_FINAL_VARIABLE_NAME:" + INNERCLASS_STATIC_FINAL_VARIABLE_NAME);
System.out.println("inner Variable innerVariableName:" + innerVariableName);
System.out.println("inner Variable sameVariableName:" + sameVariableName);
System.out.println("inner Variable this.sameVariableName:" + this.sameVariableName);
System.out.println("invoke inner class non-static private method'innerNonStaticPrivateMethod:" + innerNonStaticPrivateMethod());
return "invoke inner class not-static method!";
}
private String innerNonStaticPrivateMethod(){
System.out.println("inner Variable innerVariableName:" + innerVariableName);
System.out.println("inner Variable sameVariableName:" + sameVariableName);
return "invoke inner class not-static private method!";
}
}
public static void main(String[] args) {
System.out.println(TestNonStaticInnerClass.outStaticMethodByInnerClass());
System.out.println(new TestNonStaticInnerClass().outNonStaticMethodByInnerClass());
}
}
package com.fruitking.innerclass;

/**
* 对一个静态成员类,去掉其声明中的"static"关键字,
* 将其定义移入其外部类的静态方法或静态初始化代码段中就成为了局部静态成员类。
* 局部静态成员类与静态成员类的基本特性相同。
* 局部类能且只能访问其所属代码段中的声明为final的局部变量。
* 局部类只在定义它的代码段中可见,不能在它所属代码段之外的代码中使用;因此也就没有public/private/default权限修饰符(无意义)
*/
public class TestStaticLocalInnerClass {
private static String OUTCLASS_STATIC_VARIABLE_NAME = "OUTCLASS_STATIC_VARIABLE_NAME";
public String outVariableName = "out Variable Name";
private static String outStaticMethod(){
System.out.println("Out class static method!");
return "invoke out class' outStaticMethod()";
}
private static String innerStaticMethodByOutStaticMethod(){
System.out.println("Out Static Method invoke inner Static Method!");
String outMethodVariable = "outMethodVariable";
final String outMethodFinalVariable = "outMethodFinalVariable";
class StaticInnerClass{
public final static String INNERCLASS_STATIC_VARIABLE_NAME = "INNERCLASS_STATIC_VARIABLE_NAME";
//The field INNERCLASS_OUT_STATIC_VARIABLE_NAME cannot be declared static; static
// fields can only be declared in static or top level types
//public static String INNERCLASS_OUT_STATIC_VARIABLE_NAME = "INNERCLASS_OUT_STATIC_VARIABLE_NAME";
public String innerVariableName = "inner Variable Name";
public String innerNonStaticMethod(){
System.out.println("inner not-static method!");
System.out.println("out Variable OUTCLASS_STATIC_VARIABLE_NAME:" + OUTCLASS_STATIC_VARIABLE_NAME);
//compile error:Cannot make a static reference to the non-static field outVariableName
//System.out.println("out Variable outVariableName:" + outVariableName);
//Cannot refer to a non-final variable outMethodVariable inside an inner class
//defined in a different method
//System.out.println("out method Variable outMethodVariable:" + outMethodVariable);
System.out.println("out method Variable outMethodFinalVariable:" + outMethodFinalVariable);
System.out.println("invoke out class static method'outStaticMethod:" + outStaticMethod());
System.out.println("inner Variable INNERCLASS_STATIC_VARIABLE_NAME:" + INNERCLASS_STATIC_VARIABLE_NAME);
System.out.println("inner Variable innerVariableName:" + innerVariableName);
System.out.println("invoke inner class static method'outStaticMethodByInnerStaticMethod:" + outStaticMethodByInnerStaticMethod());
return "invoke inner class not-static method!";
}
//The method innerStaticMethod cannot be declared static; static methods can only
//be declared in a static or top level type
//public static String innerStaticMethod(){
// System.out.println("inner class method!");
// return "return inner class name";
//}
public String outStaticMethodByInnerStaticMethod(){
System.out.println("Inner Static Method invoke out Static Method!");
return outStaticMethod();
}
}
System.out.println("inner Variable INNERCLASS_STATIC_VARIABLE_NAME:" + StaticInnerClass.INNERCLASS_STATIC_VARIABLE_NAME);
//Cannot make a static reference to the non-static field
// StaticInnerClass.innerVariableName
//System.out.println("inner Variable innerVariableName:" + StaticInnerClass.innerVariableName);
System.out.println("invoke inner class static method'innerNonStaticMethod:" + new StaticInnerClass().innerNonStaticMethod());
System.out.println("invoke inner class static method'outStaticMethodByInnerStaticMethod:" + new StaticInnerClass().outStaticMethodByInnerStaticMethod());
return "invokeOut Static Method!";
}
public static void main(String[] args) {
System.out.println(TestStaticLocalInnerClass.innerStaticMethodByOutStaticMethod());
}
}
package com.fruitking.innerclass;

/**
* 对一个成员类,将其定义移入其外部类的实例方法或实例初始化代码中就成为了局部成员类。
* 局部成员类与成员类的基本特性相同。
* 局部类能且只能访问其所属代码段中的声明为final的局部变量。
* 局部类只在定义它的代码段中可见,不能在它所属代码段之外的代码中使用;因此也就没有public/private/default权限修饰符(无意义)
*/
public class TestNonStaticLocalInnerClass {
private static String OUTCLASS_STATIC_VARIABLE_NAME = "OUTCLASS_STATIC_VARIABLE_NAME";
public String outVariableName = "out Variable Name";
private String sameVariableName = "out Variable Name";
private static String outStaticMethod(){
System.out.println("Out class static method!");
return "invoke out class' outStaticMethod()";
}
private String outNotStaticMethod(){
System.out.println("Out class non-static method!");
return "invoke out class' outNotStaticMethod()";
}
private String outNonStaticMethodByInnerClass(){
System.out.println("Out class non-static method!");
String outMethodVariable = "outMethodVariable";
final String outMethodFinalVariable = "outMethodFinalVariable";
class StaticInnerClass{
public static final String INNERCLASS_STATIC_FINAL_VARIABLE_NAME = "INNERCLASS_STATIC_FINAL_VARIABLE_NAME";
public String innerVariableName = "inner Variable Name";
public String innerVariableOutName = outVariableName;
public String sameVariableName = "inner Variable Name";
public String innerNonStaticMethod(){
System.out.println("inner not-static method!");
//Cannot refer to a non-final variable outMethodVariable inside an inner class
//defined in a different method
//System.out.println("out method Variable outMethodVariable:" + outMethodVariable);
System.out.println("out method Variable outMethodFinalVariable:" + outMethodFinalVariable);
System.out.println("out Variable OUTCLASS_STATIC_VARIABLE_NAME:" + OUTCLASS_STATIC_VARIABLE_NAME);
System.out.println("out Variable outVariableName:" + outVariableName);
System.out.println("out Variable sameVariableName:" + TestNonStaticLocalInnerClass.this.sameVariableName);
System.out.println("invoke out class static method'outStaticMethod:" + outStaticMethod());
System.out.println("invoke out class non-static method'outNotStaticMethod:" + outNotStaticMethod());
System.out.println("inner Variable INNERCLASS_STATIC_FINAL_VARIABLE_NAME:" + INNERCLASS_STATIC_FINAL_VARIABLE_NAME);
System.out.println("inner Variable innerVariableName:" + innerVariableName);
System.out.println("inner Variable sameVariableName:" + sameVariableName);
System.out.println("inner Variable this.sameVariableName:" + this.sameVariableName);
System.out.println("invoke inner class non-static private method'innerNonStaticPrivateMethod:" + innerNonStaticPrivateMethod());
return "invoke inner class not-static method!";
}
private String innerNonStaticPrivateMethod(){
System.out.println("inner Variable innerVariableName:" + innerVariableName);
System.out.println("inner Variable sameVariableName:" + sameVariableName);
return "invoke inner class not-static private method!";
}
}
//一下两行代码是等价的
StaticInnerClass innerClass = new StaticInnerClass();
System.out.println("inner Variable INNERCLASS_STATIC_FINAL_VARIABLE_NAME:" + StaticInnerClass.INNERCLASS_STATIC_FINAL_VARIABLE_NAME);
System.out.println("inner Variable innerVariableName:" + innerClass.innerVariableName);
System.out.println("inner Variable sameVariableName:" + innerClass.sameVariableName);
System.out.println("invoke inner class non-static method'innerNonStaticMethod:" + innerClass.innerNonStaticMethod());
System.out.println("invoke inner class non-static private method'innerNonStaticPrivateMethod:" + innerClass.innerNonStaticPrivateMethod());
return "invoke out class' outStaticMethod()";
}
public static void main(String[] args) {
System.out.println(new TestNonStaticLocalInnerClass().outNonStaticMethodByInnerClass());
}
}
package com.fruitking.innerclass;
/**
* 没有类名的局部类就是匿名类。
* 用一条语句完成匿名类的定义与实例创建。
*/
public class TestAnonymousClass {
public void instanceMethod() {
        //define a nonymous class which implements Action interface and creat an instance of it
        Action action = new Action() {
            public void doAction() {
                System.out.println("a simple anonymous class demo");
            }};
        action.doAction();
        //define a nonoymous class which extends BaseClass and create an instance of it
        new AnonymousClass(5){
            public void printData(){
                System.out.println("data = " + getData());
            }
        }.printData(); //"data = 5" will be outputed
    }
public static void main(String[] args) {
new TestAnonymousClass().instanceMethod();
}
}
interface Action {
    void doAction();
}
class AnonymousClass {
    private int data;
    public AnonymousClass(int data) {
        this.data = data;
    }
    public int getData() {
        return data;
    }
}
分享到:
评论

相关推荐

    达内java学习笔记-总最全

    在Java中,面向对象编程(OOP)是核心概念,这在"达内java学习笔记-总最全"中得到了强调。面向对象的主要特点是将现实世界中的实体抽象为对象,这些对象包含了属性(描述对象的状态)和方法(描述对象的行为)。...

    Java JDK 7学习笔记(国内第一本Java 7,前期版本累计销量5万册)

     《java jdk 7学习笔记》是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习java时遇到的概念、操作、应用或认证考试等问题及解决方案。  《java jdk 7学习笔记》针对java se 7新功能全面改版,无论是...

    scjp考试学习笔记

    SCJP(Sun Certified Programmer for the Java 2 Platform, ...以上是SCJP学习笔记中涉及的一些核心概念,理解并熟练掌握这些知识点对于准备SCJP考试至关重要。深入理解和实践这些内容将有助于提升Java编程能力。

    Java计算机二级学习笔记看完必过.pdf

    总结来说,Java计算机二级考试的学习笔记主要涵盖了内部类的使用、面向对象编程的概念和优势,以及类和对象的基本定义。深入理解和掌握这些知识点对于通过Java二级考试至关重要。在实际编程中,灵活运用这些概念可以...

    java学习笔记

    总之,Java学习笔记中的内容涵盖了面向对象编程的基本概念,包括类、对象、属性、方法、构造方法、对象引用以及方法的重载和覆盖。理解和掌握这些知识点是深入学习Java编程的基础。通过实践和不断学习,可以逐步提升...

    蜗牛学院-第一阶段java开发班笔记.zip

    【标题】:“蜗牛学院-第一阶段java开发班笔记”是一个包含了蜗牛学院第一阶段Java开发课程学习笔记的压缩文件。这个文件很可能是由学员在完成该阶段学习后整理的,旨在帮助回顾和巩固所学知识。 【描述】:描述中...

    JAVA认证-SCJP阅读笔记

    【JAVA认证-SCJP阅读笔记】是一份详细整理的备考...这份笔记全面地覆盖了SCJP考试的关键知识点,是备考者了解和复习Java基础的重要参考资料。通过深入学习这些内容,可以提升编程能力,为通过SCJP认证打下坚实基础。

    Java学习笔记(必看经典)

    例如,一个学生对象可能有姓名、年龄等属性,以及学习、考试等方法。 使用面向对象的主要原因在于其符合人类认知事物的方式,并且能有效地隐藏实现细节,提高代码的封装性。对象的方法提供了接口,使得外界只能通过...

    SCJP 310-065考试笔记.doc

    SCJP(Sun Certified Programmer for...以上就是文档中涉及的SCJP 310-065考试的关键知识点,涵盖了Java的语法、异常处理、循环控制、内部类和抽象类的特性等核心概念。学习和理解这些知识点对于准备SCJP考试至关重要。

    Java计算机二级学习笔记(看完必过!)[参照].pdf

    Java计算机二级考试主要涵盖Java语言的基础知识和编程技巧,其中包括内部类这一重要概念。内部类在Java中可以分为成员内部类、局部内部类、匿名内部类等类型,它们的一个显著特点是能够直接访问外部类的私有成员变量...

    SCJP学习笔记.doc文档

    SCJP,全称为Sun Certified Programmer for the ...这些是SCJP学习笔记中涵盖的一些核心概念,理解并掌握它们对于准备SCJP考试和日常Java编程非常重要。在实际应用中,还需要结合更多实践和深入学习来巩固这些知识。

    Java学习笔记(必看经典).

    Java是一种广泛使用的面向...学习Java时,理解和掌握面向对象的概念、类和对象、继承、封装、多态等核心概念至关重要。通过不断地实践和应用,可以逐渐精通Java的面向对象编程,从而能够构建高效、可扩展的软件系统。

    达内内部java试题

    根据给定的文件信息,我们可以总结出以下关于达内内部Java试题的知识点: ### 考试背景与目的 这份试题是由达内IT培训集团设计的,旨在评估和提升Java初学者的技术水平。通过这份试题,新手们可以检验自己对JavaSE...

    Java学习笔记(必看经典)[整理].docx

    Java是一种面向对象的编程语言,它的核心理念...总的来说,Java学习笔记中强调了面向对象编程的核心概念,包括对象、类、构造方法、属性、方法、重载和覆盖。理解并熟练运用这些概念是成为一名合格的Java开发者的基础。

    java学习,scjp试题,笔记,非常的全面,corejava工程

    Java是一种广泛使用的面向对象...通过学习笔记,可以了解作者在实践中遇到的问题和解决方法;通过SCJP试题,可以评估自己的学习进度,找出需要加强的部分。无论你是自学还是参加培训,这都是一个非常有价值的参考资料。

    计算机组成原理笔记-课后答案-唐朔飞

    - **计算机软件**:指计算机运行所需的程序及相关资料,分为系统软件和应用软件两大类。 - **硬件与软件的重要性**:两者相辅相成,缺一不可。硬件提供了计算能力的基础,而软件则决定了计算机的功能和用途。 2. ...

    SCJP学习笔记

    以下是从提供的学习笔记中提取的一些关键知识点: 1. **八进制和十六进制表示**: - 八进制数以0开头,如010表示的是十进制的8。 - 十六进制数以0x或0X开头,例如0x10表示十进制的16。 2. **参数传递**: - ...

    SCJC学习笔记(笔试 面试 精选)

    SCJC,全称为Sun Certified Java Programmer,是针对Java编程语言的一项认证考试,旨在评估考生对Java编程的基础知识和技能。...学习和理解这些知识点对于准备SCJP考试以及实际的Java开发工作至关重要。

Global site tag (gtag.js) - Google Analytics