内部类与外部类 使用 及 instanceof



class A{
    void printA(){
       
    }
}

class B extends A{
    void printB(){
    }
}

class C extends A{
    void printC(){
    }
}

public class TestStringBuilder {

    /**
     * @param args
     */
    public static void main(String[] args){
        // TODO Auto-generated method stub
        //begin------StringBuilder use---------
        String str = "";
        long startTime =System.currentTimeMillis();
        for (inti=0;i<10000;i++){
            str+=i;
        }
        long endTime =System.currentTimeMillis();
        long Time = endTime -startTime;
        System.out.println("totaltime is
?" + Time);
       
        StringBuilder builder = newStringBuilder("");
        startTime =System.currentTimeMillis();
        for (inti=0;i<10000;i++){
            builder.append(i);
        }
        endTime =System.currentTimeMillis();
        Time = endTime - startTime;
        System.out.println("stringbuildertotal time: " + Time);
        //end------StringBuilder end---------
       
        //---------sort arraybegin----------
        int[] testNum ={10,40,1,50,88,100};
        for (inti=0;i<testNum.length;i++){
            int maxIndex = 0;
            int max = testNum[maxIndex];
            for (intj=1;j<testNum.length-i;j++){
                if (max >testNum[j]){
                    testNum[maxIndex] =testNum[j];
                    testNum[j]= max;
                    maxIndex = j;
                }else{
                    maxIndex = j;
                    max =testNum[j];
                }
            }
        }
        for (int i=0;i<testNum.length;i++){
            System.out.println(testNum[i]);
        }
        //---------sort arrayend----------
       
       
        //---------begin instanceof usedfor downcast -------
        //A a = new B(); // a is instaceofB
        A a = new A();   //a is not instanceof B
        B b = new B();
       
        if(a instanceof B){
            b =(B)a;
            System.out.println("a isinstance of B");
        }
        //---------end instanceof used fordowncast -------
       
       
        //--begin final array values canbe changed

        final int[] arr = {10,42,1};
        int[] arr_2 = {2,7,9};
        final int[] f_arr = arr;
        for (int i =0;i<arr.length;i++){
            f_arr[i] = i + 1;
            arr[i] = i + 2;
            System.out.println(":"+ arr[i] + ":" + f_arr[i]);
        }
        //--end final array values can bechanged
       
       
        //--begin ----------inner and outer class----

        Outer out = new Outer();
        Outer.Inner inner = out.newInner();
        Outer.Inner in = out.fun();
        //--end ----------inner and outerclass----
       
        //begin inner class
        OuerInterface bb = newOuterClass().doInterface();
        OuerInterface aa = newMyClass().doInterface();
        aa.fun();
        bb.fun();
        //end inner class
    }
}

//1:inner class instance associated to outer class instance
//2:inner class can access outer class's private funs and vars
//3:outer class's function can instant inner class as a var and can be accessby other inner class
//4:outer class can not access inner class's var,but can access by a instanceof inner class in case 3:
//5:inner class can only be instant by instance of outer class

class Outer {
    private int outterVar = 1;
    Inner in = new Inner();
   
    Inner fun(){
        in.innerVar = 4;//can access
        //innerVar = 5; //can notaccess
        return new Inner();
    }
    class Inner {
        int innerVar = 1;
    }
}

//1:inner class can be private or protected but outer class can not
//2:inner class implements interface but as a private class, so can not bedowncast as InnerClass
//3:only a interface and a outer class can be access so it very well for hidefor fun implements

interface OuerInterface{
    void fun();
}

class OuterClass {
    private class InnerClass implementsOuerInterface {
        public void fun(){
        }
    }
       
    public OuerInterfacedoInterface(){
        return new InnerClass();
    }
}

class MyClass extends OuterClass{
   
}


内部类与外部类 使用  instanceof 

内部类实例依附于外部类的实例,内部类可以访问外部类的private 变量与方法,外部类只能通过在外部类中实例(new)一个内部类或者在非静态方法中实例一个内部类返回来访问内部类域与方法

Outclass outer =new outclass

Inner inner =outer.new inner();

Inner inner =outer.fun();




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值