8 Java 基础 内部类访问

 

内部类的访问规则

1内部类可以直接访问外部类中的成员 包括私有

之所以可以直接访问外部类中的成员是因为内部类中持有了一个外部类的引用格式  外部类名.this

2外部类要访问内部类 必须建立内部对象

 

访问格式

1 当内部类定义在外部类的成员位置上 而且非私有 可以在外部其他类中

可以直接建立内部类对象

格式

    外部类名.内部类名 变量名=外部类对象.内部类对象;

         Outer.Innerin =new Outer().new Inner();

2 当内部类在成员位置上 就可以被成员修饰符所修饰

    比如 private:将内部类在外部类中进行封装

         static:内部类就具备static特性

    当内部类被static修饰后 只能直接访问外部类中的static成员

         出现了访问局限性

 

在外部其他类中如何直接访问static内部类的非静态成员?

         newOuter.Inner().function();

 

在外部其他类中如何直接访问static内部类的静态成员?

                   Outer.Inner.function();

注意:当内部类中定义了静态成员该内部类必须是static的

   当外部类中的静态方法访问内部类时 内部类也必须是static的

 

 

当描述事物时事物的内部还有事物该事物用内部类来描述

因为内部事物在使用外部事物的内容


 

class Outer

{

         privatestatic int x=3;

 

 

  static class Inner//静态内部类 

         {

                   //intx=4;

                   staticvoid function()

                   {

                   //      int x=6;

                            System.out.println("inner:"+x);//内部有不去外面找

                   //      System.out.println("inner:"+this.x);//内部

                   //      System.out.println("inner:"+Outer.this.x);//外部

                   }

         }

         /*voidmethod()

         {

                   //System.out.println(x);

                   Innerin =new Inner();

                   in.function();

         }*/

static class Inner2

{

         voidshow()

         {

                   System.out.println("inner2show");

         }

}

public static void method()

         {

                  

                   Inner.function();

                   newInner2().show();

         }

 

}

 

class Innerclassdemo

{

         publicstatic void main(String[] args)

         {

                   Outer.method();

                   //newOuter.Inner().function();//静态

                   //Outer.Inner.function();

                   //Outerout=new Outer();

                   //out.method();

                   //直接访问内部类中的成员

                   //Outer.Innerin =new Outer().new Inner();

                   //in.function();

 

         }

}

 

 

 

/*

内部类定义在局部时

1 不可以被成员修饰符修饰

2可以直接访问外部类中的成员,因为还持有外部类中的引用

  但是不可以访问他所在的局部中的变量。只能访问被final修饰的局部变量

 

 

 

*/

class Outer

{

         intx=3;

         voidmethod(final int a)

         {

                   finalint y=4;

                   classInner

                   {

                            voidfunction()

                            {

                                     System.out.println(a);

                            }

                           

                   }

                   newInner().function();

         }

}

 

class Innerclassdemo1

{

         publicstatic void main(String[] args)

         {

                   //newOuter().method(7);

                   Outerout=new Outer();

                   out.method(7);

                   out.method(6);

 

         }

}

 

 

/*

匿名内部类:

1匿名内部类其实就是内部类的简写格式

2定义匿名内部类的前提 内部类必须继承一个类或者实现接口

3匿名内部类的格式:new 父类或者接口(构造函数 参数){定义子类的内容}

4其实匿名内部类就是一个匿名子类对象  而且这个对象有点胖

可以理解为带内容的对象

5匿名内部类中定义的方法最好不要超过3个

*/

abstract class Absdemo

{

         abstractvoid show();

}

class Outer

{

         intx=3;

         /*

         classInner extends Absdemo

         {

                   voidshow()

                   {

                            System.out.println("show:"+x);

                   }

         }*/

         publicvoid function()

         {

 

                   //newInner().show();

         //      Inner in=new Inner();

         //      in.show();

         //      in.abc();

                   //把上句话中的Inner()用以下对象代替

 

                   newAbsdemo()//Absdemo这个类的子类对象  对class的简化写法

                   {

                            intnum=9;

                            voidshow()

                            {

                                     System.out.println("num="+num);

                            }

                            voidabc()

                            {

                                     System.out.println("Hah");

                            }

                   }.show();

 

                   /*newAbsdemo()

                   {

                            voidshow()

                            {

                                     System.out.println("x="+x);

                            }

                            voidabc();

                            {

                                     System.out.println("Hah");

                            }

                   }.abc();*/

                   Absdemod=new Absdemo()

                   {

                            voidshow()

                            {

                                     System.out.println("x="+x);

                            }

                            voidabc()

                            {

                                     System.out.println("Hah");

                            }

                   };

                   d.show();

                   //d.abc();//编译失败父类中没有定义

 

         }

}

class Innerclassdemo2

{

         publicstatic void main(String[] args)

         {

                   newOuter().function();

         }

}

 

 

小例程

 

interface Inter

{

         voidmethod();

}

class Test

{

         //补足代码 通过匿名内部类

         /*staticclass Inner implements Inter

         {

                   publicvoid method()

                   {

                            System.out.println("HelloWorld!");

                   }

         }*/

         staticInter function()

         {

                   returnnew Inter()

                   {

                            publicvoid method()

                      {

                            System.out.println("methodrun");

                      }

                   };

         }

 

}

 

class Innerclasstest

{

         publicstatic void main(String[] args)

         {

                   Test.function()//Test类中有一个静态的方法function

                   //.method():function这个方法运算的结果是一个对象 而且是Inter类型的对象

                   //只有Inter类型的对象 才能调用方法

                  

                   Test.function().method();

                   //Interin=Test.function();

                   //in.method();

                   show(newInner()

                   {

                            publicvoid method()

                            {

                                     System.out.println("methodshow run");

                            }

                   }  );

 

 

         }

         publicstatic void show(Inter in)

         {

                   in.method();

         }

}

class Innertest

{

         publicstatic void main(String[] args)

         {

                   newObject()

                   {

                            publicvoid function()

                            {

 

                            }

                   }

         }.function();

}

//Object();//Object对象

/*new Object()

                   {

                            publicvoid function()

                            {

 

                            }

                   }

         }.function();

 

         */

         //Object的子类对象



--------------------------------包

package pack;//包放第一行  包名所有字母小写

 

/*

为了简化类名的书写使用一个关键字 import

import导入的是包中的类  不包含子包

建议不要写通配符*。需要用到包中的哪个类,就导入哪个类

 

建议定义包名不要重复可以使用url来完成定义  url是唯一的

 

 

 

*/

 

 

//import packc.haha.hehe.heihei.*;

//import packa.*;

 

 

 

class Packagedemo

{

         publicstatic void main(String[] args)

         {

                   packa.demoaa=new packa.demoa();

                   a.show();

                   packb.demobb=new packb.demob();

                   b.method();

                   System.out.println("HelloWorld!");

         }

}

// javac -d . Packagedemo.java(指明存放路径"."表示当前目录)

//java pack.Packagedemo// 有了包名后 类名编程包名.类名

 

/*

总结:

 

1包与包之间进行访问 被访问的包中的类以及类中的成员 需要public修饰

2不同包中的子类还可以直接访问父类中被protected权限修饰的成员

3包与包之间可以使用的权限只有两种public protected

 

                            public    protected   default    private

 

同一个类中      OK          OK             OK             OK

同一个包中              OK                       OK             OK            

子类                            OK                       OK   

不同包中                   OK

 

 

*/

 

package packa;//包放第一行  包名所有字母小写

 

public class demoa

{

         publicvoid show()

         {

                   System.out.println("Helloshow");

         }

}

 

package packb;

 

public class demob

{

         protected/*保护*/ voidmethod()

         {

                   System.out.println("HelloWorld!bbbb");

         }

}

        

/*class democ//没有覆盖 父类有保护

{

          void method();

        

}

 

*/

/*

package packb;

 

public class demob

{

          void method()

         {

                   System.out.println("HelloWorld!");

         }

}

        

class democ

{

          void method(){}//覆盖父类

        

}

 

*/

/*

package packb;

 

public class demob

{

         protectedvoid method()

         {

                   System.out.println("HelloWorld!");

         }

}

        

class democ

{

         protected  void method()//覆盖父类

        

}

 

 

*/

/*

package packb;

 

public class demob

{

         protectedvoid method()

         {

                   System.out.println("HelloWorld!");

         }

}

        

class democ

{

         publicvoid method()//覆盖父类  权限够大就可以覆盖

        

}

 

 

*/

 

package packc.haha.hehe.heihei;//包放第一行  包名所有字母小写

 

public class democ

{

         publicvoid show()

         {

                   System.out.println("Helloshow");

         }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值