黑马程序员技术交流社区
标题:
内部类
[打印本页]
作者:
絮缘小默
时间:
2014-3-5 22:09
标题:
内部类
学习了匿名内部类,但是我自己写不出来,所以想请同学们给我写一下内部类的样式
作者:
不冬眠的蚂蚁
时间:
2014-3-5 22:30
interface A{
public void fun1();
}
class B{
int i = 10;
public void get(A a)// 声明了一个方法get,接受一个类型为A的参数
{
a.fun1();
}
public void test(){
this.get(new A()//这里就是匿名的内部类,实现了A这个接口的类,只是是匿名的,编译的时候会自动生成一个
//类名为B$1的类,这一点你可以用cmd手动编译一下,就能看见B$1.class这样一个文件,相当于你有一个B$1
//的类实现了A这个接口,然后作为get的参数传了进去
{
public void fun1() {
System.out.println(i);
}
});
}
}
class TestNonameDemo{
public static void main(String args[]){
B b = new B();
b.test();
}
}
作者:
冯鸿昌
时间:
2014-3-10 01:01
package com.uc;
/**
* <p>What's the nested class ?</p>
* <p>define a class within another class</p>
* <p>Why to use nested class ?</p>
* <li>It is a way of logically grouping classes that are only used in one place.</li>
* <li>It increases encapsulation.</li>
* <li>Nested classes can lead to more readable and maintainable code.</li>
*
*
*/
public class NestClassT {
public static void main(String[] args) {
//test();
test1();
}
/**
* 测试instance nested class的用法
*/
private static void test1() {
// TODO Auto-generated method stub
Outer outerObj = new Outer();
Outer.InnerClass obj = outerObj.new InnerClass();
obj.showInfo(" Called by Create context");
//查看inner class 是否可修改value of enclosing class variable
System.out.println(outerObj.getInstanceVal());
}
/**
* 测试static nested class 与 static variable of enclosing class 的访问关系
*/
private static void test() {
// TODO Auto-generated method stub
Outer.StaticInner obj = new Outer.StaticInner();
//可以访问static variable of enclosing class
obj.showInfo();
//测试 static method of static nested class 对enclosing class的访问性
Outer.StaticInner.staticMethod();
//从上面的测试可以看出 static nested class 与 static method of enclosing class
//的访问性一致
//还有一点需要注意的是,the modifiers of static nested class
//{public, protected, private and packet private}
}
}
class Outer {
/**
* static nested class modifiers can be
* {public protected private and packet private}
*/
public static class StaticInner {
public void showInfo () {
//查看static nested class 是否可访问Enclosing class
//static variable
System.out.println(info + " in instance method of static nested class");
System.out.println(instanceVal + " in instance method of static nested class");
System.out.println(outerInfo);
//variable shadow
//我们可以通过完整的名称引用来解决命名冲突的问题
System.out.println(Outer.info);
//调用static private method of enclosing class
System.out.println("Call static private method of enclosing class: ");
System.out.println(staticMethodOfOuter(" called by instance method of static nested class call "));
}
public static void staticMethod() {
//访问外部变量,实际上是编译器在中间做了手脚,这个可以通过 javap命令查看
System.out.println(outerInfo);
System.out.println(info + " in static method of static nested class");
//variable shadow
//我们可以通过完整的名称引用来解决命名冲突的问题
System.out.println(Outer.info);
//calling static private method of enclosing class
System.out.println("Calling static private method of enclosing class: ");
System.out.println(staticMethodOfOuter(" called by static method of static nested class call "));
}
//the static variable of static nested class
private static String info = "static val";
//the instance variable of static nested class
private String instanceVal = "instanceVal";
}
//test the variable shadow
//there is a 'info' variable in the static nested class
private static String info = "private static info of enclosing class";
private static String outerInfo = "static val of enclosing class";
private static String staticMethodOfOuter(String val) {
return "I am static private method of enclosing class" + val;
}
//////////////////////////////////////////////////////////////////////
//下面测试instance nested class 对 enclosing class 的访问性
//
//As with instance methods and variables,
//an inner class is associated with an instance of its enclosing class
//and has direct access to that object's methods and fields.
//
//Also, because an inner class is associated with an instance,
//it cannot define any static members itself.
//
//To instantiate an inner class, you must first instantiate the outer class.
//Then, create the inner object within the outer object with this syntax:
// OuterClass.InnerClass innerObject = outerObject.new InnerClass();
//Additionally, there are two special kinds of inner classes:
//local classes and anonymous classes.
//////////////////////////////////////////////////////////////////////
private String instanceOuterInfo = "I am Instance variable of enclosing class";
private String instanceVal = "I am instance variable of enclosing method";
public String getInstanceVal() {
return this.instanceOuterInfo;
}
private String instanceMethod(String val) {
return "I am instance method of enclosing class " + val;
}
class InnerClass {
//The field info cannot be declared static in a non-static inner type,
//unless initialized with a constant expression
static final String info = "test";
private String instanceVal = "I am instance varaible of instance nested method";
/**
* The method staticMethod cannot be declared static;
* static methods can only be declared in a static or top level type
*/
// public static void staticMethod() {
//
// }
public void showInfo(String instanceVal) {
//visit private variable of enclosing class
System.out.println("visit the private static variable of enclosing class: ");
System.out.println(Outer.info);
System.out.println("visit the private instance variable of enclosing class: ");
System.out.println(instanceOuterInfo);
//visit private method of enclosing class
System.out.println(staticMethodOfOuter(
" Called by private instance method of instance nested class") );
System.out.println(instanceMethod(
" Called by private instance method of instance nested class") );
//命名冲突解决方案
System.out.println("Checking name conflict: ");
System.out.println("The parameter of method: " + instanceVal);
System.out.println("The instance variable of inner class: " + this.instanceVal);
System.out.println("The instance variable of enclosing class:" + Outer.this.instanceVal);
//try to modify the the value of enclosing class variable
instanceOuterInfo = "haha";
System.out.println(instanceOuterInfo);
}
}
}
复制代码
作者:
冯鸿昌
时间:
2014-3-10 01:04
作者:
向阳泪无痕
时间:
2014-3-10 11:44
外部类{
内部类{}
}
就是这样
作者:
清风木扬
时间:
2014-3-27 19:45
new InterfaceClass()
{
//代码块
// InterfaceClass是接口,抽象类时,实现抽象方法,Override这个类的其它方法
//InterfaceClass是普通类时,Override这个类的方法
}
enum Lamp {
//生成一个匿名类
YELLEW {
@Override
public void sayColor() {
System.out.println("");
}
},
//再生成一个匿名类
RED
{
public void sayColor() {
System.out.println("");
}
}
;
//抽象方法
public abstract void sayColor();
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2