面向对象(继承-内部类)-第19天

面向对象(内部类访问规则)

内部类:将一个类定义在另一个类的里面,对里面的那个类就称为内部类(内置类,嵌套类)

内部类的访问规则:

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

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

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

例1:

public class PackageOne {
    public static void main(String[] args) {
        Outer out = new Outer();
        out.method();
    }
}

class Outer {

    private int x = 3;//私有化在本类中有效。

    class Inner//内部类

    {

        void function() {

            System.out.println("inner :" + x);//内部类直接访问外部类的私有成员

        }

    }

    void method() {

        Inner in = new Inner();//in创建内部类对象

        in.function();//in访问内部类

    }

}

//打印

inner :3

 

例2:

public class PackageOne {
    public static void main(String[] args) {
        Outer out = new Outer();
        out.method();
        System.out.println(">>>");
        Outer.Inner in = new Outer().new Inner();//外部类.内部类in=外部类.内部类
        in.function();
    }
}

class Outer {

    private int x = 3;//私有化在本类中有效。

    class Inner {
        int x = 4;

        void function() {
            int x = 6;
            //内部类直接访问外部类的私有成员.
            System.out.printf("inner :" + x);//x=6
            //用this强制访问内部类成员的x
            System.out.printf("inner :" + this.x);//x=4
            //用this指向了Outer类成员的x。
            System.out.printf("inner :" + Outer.this.x);//x=3
        }
    }

    void method() {

        Inner in = new Inner();//in创建内部类对象

        in.function();//in访问内部类
    }

}

结果:

inner :6inner :4inner :3>>>
inner :6inner :4inner :3

 

例1中的System.out.println("inner:"+x);//Outer.this.x省略成了x。再一次说明内部可以访外部类成员。

 

08_面向对象(静态内部类)

访问格式:

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

可以直接建立内部类对象

格式:

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

Outer.Inner   in =Outer.Inner();

 

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

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

static:内部类就具备了static的特性。

当内部类被static修饰后,只能访问外部类中的static成员,出现了访问局限

 

在外部其它类中,如何访问静态内部类的非静态成员。??

可以通过new Outer.Inner().function();

 

在外部其它类中,如何访问静态内部类的静态成员。??

可以通过new Outer.Inner.function();//是静态,直接类名调用

 

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

注意:当外部类中的静态方访问内部类时,内部类也必须是静态的。

public class PackageOne {
    public static void main(String[] args) {

//        Outer.Inner in = new Outer().new Inner();//外部类.内部类in=外部类.内部类
        //静态化内部类
        new Outer.Inner().function();//外部类.内部类in=外部类.内部类

    }
}

class Outer {

    private static int x = 3;//私有化在本类中有效。

    //静态化内部类
    static class Inner {
        //当内部类中定义了静态成员,该内部类必须是static的
        static void function() {
            //内部类直接访问外部类的成员.
            System.out.printf("inner :" + x);//x=3
        }
    }

    void method() {
        Inner in = new Inner();//in创建内部类对象
        in.function();//in访问内部类
    }

}

面向对象(内部类定义原则)

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

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

class Body

{

private class XinZang//私有化心脏。公开心脏是很伤心的。

{



}

public void show()//提供心脏给外部访问的方法。

{

new XinZang();

}

}

 

110_面向对象(匿名内部类)

class Outer

{

         int x=3;

         void method()//非静态,没对象的时候,不运行。

         {

                  class Inner//局部内部类不能修饰为静态

                  {

                          void function()

                          {

                                   System.out.println(Outer.this.x);

                          }

                  }

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

         //编译运行报错。        

         }

}

}

}
class Outer

{

         int x=3;

         void method()//非静态,没对象的时候,不运行。

         {      

                  new Inner().function();//编译的时候,新创建类Inner时,还没有加载function方法,

找不到function,编译错误。

                  class Inner//局部内部类不能修饰为静态

                  {

                          void function()

                          {

                                   System.out.println(Outer.this.x);

                          }

                  }

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method();//编译错误       

         }

}

对比:内部类创建完成后,才调用 

class Outer

{

         int x=3;

         void method()//非静态,没对象的时候,不运行。

         {

                  class Inner//局部内部类不能修饰为静态

                  {

                          void function()

                          {

                                   System.out.println(Outer.this.x);

                          }

                  }

                  new Inner().function();//放在后面就保障了function方法被加载过。

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method();  

         }

}

//打印:3
 

class Outer
{
         int x=3;
         void method()//非静态,没对象的时候,不运行。
         {
                  int y=4;
                  class Inner//局部内部类不能修饰为静态

                  {
                          void function()

                          {
                                   System.out.println(y);
                          }

                  }
                  new Inner().function();
         }
}

class InnerClassDemo
{
         publicstatic void main(String[] args)
         {
                  newOuter().method();  
         }

}

//编译错误:

从内部类中访问局部变量y:需要被声明为最终类型final。

 

内部类定义在局部

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

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

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

class Outer

{
         intx=3;
         void method()//非静态,没对象的时候,不运行。
         {
                  final int y=4;
                  class Inner//局部内部类不能修饰为静态
                  {
                          void function()
                          {
                                   System.out.println(y);
                          }
                  }
                  newInner().function();
         }
}

class InnerClassDemo
{
         publicstatic void main(String[] args)
         {
                  newOuter().method();  
         }
}

//打印:4

class Outer

{

         intx=3;

         void method(int a)//a是一个变化的局部变量。当调用时7。即变成int a=7;

         {

                  finalint y=4;

                  classInner//局部内部类不能修饰为静态

                  {

                          voidfunction()

                          {

                                   System.out.println(a);

                          }

                  }

                  newInner().function();

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method(7);

         }

}

//编译误。再次出现内部类访问局部变量

class Outer

{

         int x=3;

         void method(final int a)

         {

                  final int y=4;

                  classInner//局部内部类不能修饰为静态

                  {

                          void function()

                          {

                                   System.out.println(a);

                          }

                  }

                  new Inner().function();

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  new Outer().method(7);

         }

}

//打印:7

class Outer

{

         int x=3;

         void method(final int a)

         {

                  final int y=4;

                  class Inner//局部内部类不能修饰为静态

                  {

                          void function()

                          {

                                   System.out.println(a);

                          }

                  }

                  new Inner().function();

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  Outer out=new Outer();

                  out().method(7);//往里面传7,a不是就锁定为7了吗?new一次     

                  out().method(8);//怎么还可以往里面传8呢?再new一次

         }

}

//编译通过。这是为什么???

首先out().method(7);调用method方法,在栈内存中开辟空间,把7赋给a,并锁定。注意,执行完后,就要出栈,释放内存。

然后out().method(8);再调用method方法,进栈,出栈。

 

当method方法,出现a++,就有问题了。。。

void method(final int a)

         {

                  a++;//a是固定的,不能改变。

                  final int y=4;

                  class Inner//局部内部类不能修饰为静态

                  {

                          void function()

                          {

                                   System.out.println(a);

                          }

                  }

                  new Inner().function();

         }

//编译误。

 

匿名内部类:

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

2,定义匿名内部类的前提:

内部类必须是继承一个类或实现接口。

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

4,其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖。可以理解为带内容的对象。

5,匿名内部类中最好不要超过3个。

 

//定义一个父类(公共类)

class AbsDemo

{

         abstract void show();//抽象

}



class Outer

{

         int x=3;

         class Inner extends AbsDemo//Inner继承了AbsDemo

         {

         void show()

                  {

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

                  }

         }

         public void function()

         {

                  new Inner().show();

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  new Outer().function();

         }

}

//打印:

show3

 

简化成匿名内部类。

class AbsDemo

{

         avstract void show();

}



class Outer

{

         int x=3;

         public void function()

         {

                  //newAbsDemo();这里代表了创建了AbsDemo对象。而抽象类不可以被new对象创建???它里面有抽样方法。。。

                  new AbsDemo()//创建AbsDemo对象,后面还带着内容。就相当于一个对象体,一个很胖的对象。

                  {//这个AbsDemo的子类对象。因为它要复写AbsDemo的父类对象的内容。

                          void show()

                          {

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

                          }

                  };

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  new Outer().function();

         }

}

再变化成更奇葩的!!!

class AbsDemo

{

         avstractvoid show();

}



class Outer

{

         int x=3;

         public void function()

         {

                  //newAbsDemo();这里代表了创建了AbsDemo对象。而抽象类不可以被new对象创建???它里面有抽象方法。。。

                  new AbsDemo()//创建AbsDemo对象,后面还带着内容。就相当于一个对象体,一个很胖的对象。

                  {//这个AbsDemo的子类对象。因为它要复写AbsDemo的父类对象的内容。

                          void show()

                          {

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

                          }

                  }.show();//相当于 new Inner().show();即AbsDemo的匿名子类对象.show();

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  new Outer().function();

         }

}

//优点:简化代码

缺点:初步一看,阅读性差。

 

在AbsDemo的子类对象,创建其它的方法。比如abc()

class AbsDemo

{

         abstract void show();

}



class Outer

{

         int x=3;

         public void function()

         {

                  new AbsDemo()

                  {

                          void show()

                          {

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

                          }

                          void abc()//定义了子类特有的方法,是正常的。

                          {

                                   System.out.println("haha");

                          }

                  }.abc();//故调用abc也是可以的。

//匿名对象只能对方法调用一次。再次调用又要重新写一次。。。

                  new AbsDemo()

                  {

                          void show()

                          {

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

                          }

                          void abc()//定义了子类特有的方法,是正常的。

                          {

                                   System.out.println("haha");

                          }

                  }.show();

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  new Outer().function();

         }

}

//打印:

haha

x===3

class AbsDemo

{

         abstract void show();

}



class Outer

{

         int x=3;

         public void function()

         {                 

                  AbsDemo d=new AbsDemo()//父类引用子类。相当与AbsDemo a=new Inner();

                  {

                          void show()//子类复写

                          {

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

                          }

                          void abc()

                          {

                                   System.out.println("haha");

                          }

                  };

                  d.show();//当d.abc();编译错误。父类中没有方法abc()

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  new Outer().function();

         }

}

//打印:

x===3

 

父类AbsDemo中有多个show()方法。

class AbsDemo

{

         avstract void show();

         avstract void show1();

         avstract void show2();

         avstract void show3();

}



class Outer

{

         int x=3;

         public void function()

         {//这样阅读性很差

                  new AbsDemo()//匿名变量中,最多不超过3个方法。

                  {

                          public void show()

                          {}

                          public void show1()

                          {

                          }

                          public void show2()

                          {

                          }

                          public void show3()

                          {

                          }

                  };

                 

                  AbsDemo d=new AbsDemo()//父类引用子类。相当与AbsDemo a=new Inner();

                  {

                          void show()//子类复写

                          {

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

                          }

                          void abc()

                          {

                                   System.out.println("haha");

                          }

                  };

                  d.show();//当d.abc();编译错误。父类中没有方法abc()

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  newOuter().function();

         }

}
class AbsDemo

{

         abstract void show();

}



class Outer

{

         intx=3;

         public void function()

         {

                 

                  AbsDemo d=new AbsDemo()

                  {//此时AbsDemo相当于类

                          int num=9;//num相当于类中的成员变量

                          void show()

                          {

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

                          }

                          void abc()

                          {

                                   System.out.println("haha");

                          }

                  };

                  d.show();

         }

}

class InnerClassDemo

{

         public static void main(String[] args)

         {

                  newOuter().function();

         }

}

//打印:

num===9

 

 

练习:

interface Inter

{

         void method();//在实现接口中,method是抽象非静态的。在接口中,抽象abstract可以隐藏不写。

}

class Test

{

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

         static class Inner implements Inter

         {

                  public void method()

                  {

                          System.out.println("methodrun");

                  }

         }

         static function()//定义静态function

         {

                  return new Inner();       

         }

}

class InnerClassTest

{

         public static void main(String[] args)

         {

                  Test.function().method();//上面已经new过了,这里就不用new

         }

}

//Test.function().method();可以看出function是静态的。而method是非静态的。

因为它是抽象的。

 

改成匿名的:

interface Inter

{

         void method();

}

class Test

{

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



         static function()

         {

                  return new Inter()

                  {

                          public void method()

                          {

                                   System.out.println("methodrun");                

                          }

                  };   

         }

}

class InnerClassTest

{

         public static void main(String[] args)

         {

                  Test.function().method();//上面的返回值,在匿名内部类中已经new创建了,这里不用new

}}

。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值