内部类与外部类 使用 及 instanceof

来源:互联网 发布:猫头鹰安全座椅 知乎 编辑:程序博客网 时间:2024/06/07 01:57



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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 老鼠跑到车里了怎么办 我的小车有老鼠怎么办 车里面进了老鼠怎么办 街电充电宝丢了怎么办 充电宝充不了电怎么办 脚裸扭伤肿了怎么办 大货车电瓶亏电怎么办 货车电瓶被偷了怎么办 小乌龟尾巴断了怎么办 长青春痘怎么办简单小妙招 一关灯就有蚊子怎么办 狗狗误食蟑螂药怎么办 泰迪误食蟑螂药怎么办 猫咪吃了蟑螂药怎么办 狗把蟑螂药吃了怎么办 猫吃了蟑螂诱饵怎么办 猫吃了蟑螂中毒怎么办 孕妇胃烧的难受怎么办 6个月孕妇胃难受怎么办 飞机杯吸盘不上怎么办 我的车位被占用怎么办 占别人车位的车怎么办 头受凉受风了疼怎么办 看电脑时间长了眼睛疼怎么办 电脑玩久了头疼怎么办 屋里有死老鼠味怎么办 老鼠死在车里面怎么办 手机锁屏怎么办求解锁 玩lol左键失灵怎么办 小米鼠标没电了怎么办 电脑键盘鼠标没反应怎么办 win10玩游戏很卡怎么办 鞋子夹脚怎么办小妙招 新鞋两边夹脚怎么办 新鞋子两边夹脚怎么办 鞋子瘦两边夹脚怎么办 鞋子小脚趾磨脚怎么办 鞋子买小了顶脚怎么办 鼠标不亮了怎么办呢 坐便水箱不上水怎么办 2个shift键失灵怎么办