Java 内部类

Posted 小海_macro

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 内部类相关的知识,希望对你有一定的参考价值。



public class TestInnerClass {
public static int field = 1;
protected int field1 = 2;
int field2 = 3;
private int field3 = 4;

//静态代码块
/*static {
System.out.println(field);
}*/

public TestInnerClass(){
NorInner norInner = new NorInner();
System.out.println(norInner.field1Inner);
System.out.println(norInner.field2Inner);
System.out.println(norInner.field3Inner);
System.out.println(norInner.fieldInner);
//System.out.println(fieldInner);
}

//普通内部类
public class NorInner{
public int fieldInner = 1;
protected int field1Inner = 2;
int field2Inner = 3;
private int field3Inner = 4;
int field = 3;

public NorInner(){
System.out.println("内部类field:"+field);
System.out.println("外部类field:"+TestInnerClass.this.field);
System.out.println(field1);
System.out.println(field2);
System.out.println(field3);
}
}

private void print(){
StaticInner staticInner = new StaticInner();
System.out.println(staticInner.field1Inner);
}

//静态内部类
static class StaticInner{
public int fieldInner = 1;
protected int field1Inner = 2;
int field2Inner = 3;
private int field3Inner = 4;
static int i=0;

public StaticInner(){
System.out.println(field);
System.out.println(i);
}
}

//自定义接口
interface OnclickListener{
void onClick(Object object);
static int i =1;
final int NUMB = 124;
int a = 0;

void print();
//void print();
/*static final void print1(){
System.out.println("hello");
}*/
}

//匿名内部类
private void clickInner(){
OnclickListener oc = new OnclickListener() {
@Override
public void onClick(Object object) {
System.out.println("对象"+object+"被点击");
}
public void print(){
System.out.println("匿名内部类打印");
System.out.println(i+NUMB+a);
}
};
oc.onClick(new Object(){
@Override
public String toString() {
return super.toString();
}
});
oc.onClick(2);
oc.print();
}

//局部内部类;一般采用匿名内部类
public static void main(String[] args) {
TestInnerClass tic = new TestInnerClass();
System.out.println("普通内部类:");
//TestInnerClass.NorInner norInner = new tic.NorInner();
TestInnerClass.NorInner norInner = tic.new NorInner();

System.out.println("匿名内部类:");

tic.clickInner();
System.out.println("静态内部类:");
StaticInner staticInner1 = new StaticInner();
System.out.println(field);

}
}

//作用域问题

public class testInnerVariable {
class NormalClass{
//普通内部类不能有静态变量、方法
int numb = 1;
public void print(){
System.out.println("普通内部类numb:"+numb);
}
//static void test(){}
}
static class StaticClass{
static int numb = 2;
public void print(){
System.out.println("静态内部类numb:"+numb);
}
static void test(){
System.out.println("hello");
}
}
interface Anonymity{
static int numb = 3;

public static void print() {
System.out.println("接口numb:"+numb);
}

}
private void print(){
Anonymity anonymity = new Anonymity() {
@Override
public String toString() {
System.out.println("匿名内部类变量numb:"+numb);
return super.toString();
}
};
anonymity.toString();
Anonymity.print();
//静态方法不支持对象访问
//anonymity.print();

//局部内部类
class LocalClass{
//不支持静态变量
int numb = 1;
public void print(){
System.out.println("局部内部类numb"+numb);
}
//不支持静态方法
/*public static void test(){
System.out.println("hello");
}*/
}
LocalClass localClass = new LocalClass();
localClass.print();
}

/*public staic void test(){

}*/
public static void main(String[] args) {

}

}

??接口的静态方法不支持对象访问



跟虚拟机有关:填坑!!高级应用

典型内存泄漏案例

public class MemoryLeakTest {

//抽象类,模拟组件类
static abstract class Component{
final void onCreat(){
creat();
}
final void onDestroy(){
destroy();
}

abstract void creat();
abstract void destroy();
}

//具体实现子类————组件
static class MyComponet extends Component{
Mywindow mywindow;
static OnclickListener onclickListener;

@Override
void creat() {
onclickListener = new OnclickListener() {
@Override
public void onClick(Object object) {
System.out.println("组件: "+object+"被点击!");
}
};
mywindow = new Mywindow();
mywindow.setOnclickListener(onclickListener);
}

@Override
void destroy() {
//this.mywindow.getOnclickListener();
mywindow.removeClickListener();
}
}

//可视化控件
static class Mywindow{
OnclickListener onclickListener;

public OnclickListener getOnclickListener(){
return onclickListener;
}
public void setOnclickListener(OnclickListener onclickListener){
this.onclickListener = onclickListener;
}
public void removeClickListener(){
this.onclickListener = null;
}
}


//监听器接口
interface OnclickListener{
void onClick(Object object);
}

public static void main(String[] args) {
MyComponet myComponet = new MyComponet();
myComponet.creat();
myComponet.destroy();
System.gc();
System.out.println("");


}
}

以上是关于Java 内部类的主要内容,如果未能解决你的问题,请参考以下文章

# Java 常用代码片段

# Java 常用代码片段

elasticsearch代码片段,及工具类SearchEsUtil.java

片段 - 全局视图变量与本地和内部类侦听器和内存泄漏

为啥片段类应该是公开的?

ForegroundService没有从片段开始?