/**
* 匿名类必须实现一个接口或者继承一个父类
* 如果将接口注释掉,会提示不能识别Common
*
*/
package project.java.thinker.ten;
/**
* @author Administrator
*
*/
public class InnerClassOne {
/**
* @return
*/
public Common getCommone(){
return new Common (){
public void commonMethod(){
System.out.println("匿名内部类必须实现一个接口或者继承一个父类");
}
};
}
/**
* 即使传递的参数是基本类型,在匿名类中使用时也要用final限制
* 匿名类没有构造函数,因为匿名类是没有名字的
* 新建对象作为结果返回时,会向上转型的
* @param i
* @return
*/
public Base getBase(final int i){
return new Base(i){
@Override
public void baseMethod() {
System.out.println("INNERCLASS:" + i);
}
};
}
public static class Other{
// private String name = null;
/*
* 普通内部类中不能定义static变量
* 是内存分配导致的,静态成员是所有类对象共享的, 在加载时就分配了内存。
* 我们知道普通内部类是需要创建了外部类时才可以创建对象的,
* 所以普通内部类在加载类时是没有内存可以分配的,
* 自然也就没法为他的静态成员分配内存,所以普通内部类不能有静态成员
*/
public static int i = 0;
}
public Other getStatic(){
return new Other();
}
/**
* @param args
*/
public static void main(String[] args) {
new InnerClassOne().getCommone().commonMethod();
}
}
interface Common {
public void commonMethod();
}
class Base{
private int i = 0;
/**
* @param i
*/
public Base(int i){
this.i = i;
System.out.println("父类构造函数");
}
/**
*
*/
public void baseMethod(){
System.out.println("BASE:" + i);
}
}
* 匿名类必须实现一个接口或者继承一个父类
* 如果将接口注释掉,会提示不能识别Common
*
*/
package project.java.thinker.ten;
/**
* @author Administrator
*
*/
public class InnerClassOne {
/**
* @return
*/
public Common getCommone(){
return new Common (){
public void commonMethod(){
System.out.println("匿名内部类必须实现一个接口或者继承一个父类");
}
};
}
/**
* 即使传递的参数是基本类型,在匿名类中使用时也要用final限制
* 匿名类没有构造函数,因为匿名类是没有名字的
* 新建对象作为结果返回时,会向上转型的
* @param i
* @return
*/
public Base getBase(final int i){
return new Base(i){
@Override
public void baseMethod() {
System.out.println("INNERCLASS:" + i);
}
};
}
public static class Other{
// private String name = null;
/*
* 普通内部类中不能定义static变量
* 是内存分配导致的,静态成员是所有类对象共享的, 在加载时就分配了内存。
* 我们知道普通内部类是需要创建了外部类时才可以创建对象的,
* 所以普通内部类在加载类时是没有内存可以分配的,
* 自然也就没法为他的静态成员分配内存,所以普通内部类不能有静态成员
*/
public static int i = 0;
}
public Other getStatic(){
return new Other();
}
/**
* @param args
*/
public static void main(String[] args) {
new InnerClassOne().getCommone().commonMethod();
}
}
interface Common {
public void commonMethod();
}
class Base{
private int i = 0;
/**
* @param i
*/
public Base(int i){
this.i = i;
System.out.println("父类构造函数");
}
/**
*
*/
public void baseMethod(){
System.out.println("BASE:" + i);
}
}
1868

被折叠的 条评论
为什么被折叠?



