实现内部类,验证那些可以在内部类存在,怎么调用内部类,内部类怎么访问外部类

public class StaticClass {
/**
* public class A{
public static String name=”zhangsan”;
public String phone=”18336913501”;
//静态内部类
public stiatic class AinStatic{
/* 静态内部类里面可以写:除了抽象方法都可以
非静态变量public String name=”zhangxiaosan”;而且与外部类不冲突
静态变量public statid int age=30;
常量
匿名代码{}
静态代码 static{}
构造器 public AinStatic(){}
普通方法 public void test(){}
静态方法 public static void test(){}
抽象方法 public abstract void gg();//抽象方法的类必须为抽象类
}
public static void main(String[] args){
构建静态内部类
外部类.内部类名 a=new 外部类.内部类名();
要拿静态内部类中的静态变量
外部.内部.静态变量;
}
}
public int aaa=10;
public static class S{
private int a;
public static String b;
public static int c;
public final int C=10;
static{
b=”chenyye”;
c++;
StaticClass sc=new StaticClass();
System.out.println(sc.aaa);

    }
    {
        a=10;
        b="cccc";
        c++;
    }
    public S(){}
    public S(int a){
    }
    public void Ss(){
        c++;
        System.out.println("fangfa "+c);
    }


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

    System.out.println("zhi "+StaticClass.S.c);
    StaticClass.S sssss=new StaticClass.S();
    sssss.Ss();
}

}


public class StaticIn {
public static int W;
public int w;
// static内部类
public void II() {
System.out.println(“外部类的非静态方法”);
}
public static void III() {
System.out.println(“外部类的静态方法”);
}
public static class S {
private int a;
public String b;
private static int c;
public final String NAME = “chenyeye”;
static {
int id = 1;
c = id;
}
{
b = “100”;
}
public S() {
}

    public S(int a, String b, int c) {
        this.c = c;
        this.a = a;
        this.b = b;
    }

    // 静态内部类可以声明静态内部类
    public static class BB {
    }

    // 静态内部类可以声明成员类
    // public class BBB{}
    public void test() {
        // 静态内部函数可以访问外部成员变量
        // 访问外部静态变量,直接访问,访问非静态变量,需要通过对象调用
        StaticIn st = new StaticIn();
        st.w = 10;
        // 静态内部类访问外部静态方法直接调用,调用非静态的方法需要借助对象访问
        st.II();
        III();
        W = 100;
        System.out.println("Static内部类" + c + st.w);
        StaticIn.C c = new StaticIn().new C();
        c.testz();
        // 静态内部类可以再次声明局部类
        class BBBB {
        }
    }

    public static void BBB() {
        System.out.println("静态内部类中的静态方法");
    }

    public void say() {
        System.out.println("静态内部类中的非静态方法");
    }
    /*
     * public static void main(String[] args) { S s=new S();
     * System.out.println(s.c); System.out.println(s.b); s.test(); }
     */

}

// ****************************************************************************************************
// ****************************************************************************************************
// 成员内部类
public class C {
    public int a;
    // 成员内部类不能声明static变量public static int aaaa;
    // 成员内部类不能声明static代码块static{}
    {
    }

    // public C(){}
    // 成员内部类不能声明非静态内部类public static class CC{}
    // 成员内部类可以声明成员内部类
    // public class CC{}
    public void testz() {
        StaticIn st = new StaticIn();
        a = 100;
        st.w++;
        System.out.println("成员内部类" + st.w + "110");
        StaticIn si = new StaticIn();
        si.II();
        // 成员内部类可以访问外部静态方法
        III();
        // 成员内部类可以声明局部内部类
        class CCC {
        }
    }
    // 成员内部类不能声明静态方法public static void test(){}
    // 因为成员内部类不能声明静态方法,所以main方法也不能声明public static void main(String[] args) {
    // }
}

// ****************************************************************************************************
// ****************************************************************************************************
// 成员内部类:成员内部类是声明在成员方法中
public void test() {
    int a = 10;
    int b = 50;

    class A {
        // 局部内部类不能声明static属性static代码块static{}
        {
        }

        // 不能声明构造器public AA(){}
        public void Atest2() {
            int a = 20;
            System.out.println(a);
            System.out.println("dadad" + b);
            III();
            // StaticIn stat=new StaticIn();
            II();
        }
    }
    // 成员内部类只能在成员对象里面调用,不能被成员方法以外的调用
    A ac = new A();
    ac.Atest2();
    System.out.println(a);

}

public static void main(String[] args) {
    // //静态内部类
    S in = new S();
    S.BBB();
    in.test();
    //
    // //成员内部类,因为成员内部类不能写static修饰的方法,所以这里不讨论外部类怎么访问内部类
    // //内部类不是静态的,所以访问内部类需要借助new 就需要StaticIn a=new StaticIn(); C c=a.new
    // C();
    // StaticIn a=new StaticIn();
    // C c=a.new C();
    // //或者用下面的方法
    // //StaticIn.C c=new StaticIn().new C();
    // c.testz();
    StaticIn si = new StaticIn();
    si.test();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值