面向对象(继承02)-第19天

来源:互联网 发布:python远程监控服务器 编辑:程序博客网 时间:2024/05/22 15:33

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

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

内部类的访问规则:

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

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

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

例1:

class Outer

{

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

class Inner//内部类

{

void function()

{

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

}

}

void method()

{

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

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

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer out=new Outer();

out.method();

}

}

//打印

inner :3

 

例2:

class Outer

{

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

class Inner//内部类

{

int x=4;

void function()

{

int x=6;

System.out.println("inner:"+x);//先找内部类function方法内的x,x=6

}

}

void method()

{

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

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

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer out=new Outer();

out.method();

}

}

//打印

inner :6

 

例3:

class Outer

{

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

class Inner//内部类

{

int x=4;

void function()

{

int x=6;

System.out.println("inner :"+this.x);//用this强制访问内部类成员的x。x=4

}

}

void method()

{

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

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

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer out=new Outer();

out.method();

}

}

//打印

inner :4

 

例4:

class Outer

{

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

class Inner//内部类

{

int x=4;

void function()

{

int x=6;

System.out.println("inner :"+Outer.this.x);//用this指向了Outer类成员的x。x=3

}

}

void method()

{

Inner in=new Inner();

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

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer out=new Outer();

out.method();

}

}

//打印

inner :3

 

总结:从例1和例4的结果相同可以看出,

例1中的

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

 

如果直接访问内部类的成员。下面这种是错误的:

class Outer

{

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

class Inner//内部类

{

void function()

{

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

}

}

class Outer2//另外定义一个外部类不同,而内部类相同。

{

class Inner//内部类

{

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Inner in=new Inner();//错误!!!这时,主函数就不知道调用那个内部类了。

in.function();

}

}

 

面试:

那么继续改进。。。

class Outer//这里绝对不能private class Outer

{

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

private class Inner//内部类!!!当内部类外部类成员位置上时,可以被私有化修饰!!!

{

void function()

{

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

}

}

void method()

{

Inner in=new Inner();

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

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer.Inner in=new Outer().new Inner();//外部类.内部类in=外部类.内部类

in.function();

}

}

 

 

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

访问格式:

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

可以直接建立内部类对象

格式:

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

Outer.Inner    in =Outer.Inner();

 

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

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

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

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

 

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

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

 

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

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

 

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

 

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

 

class Outer//这里绝对不能private class Outer

{

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

static class Inner//静态化内部类。

{

void function()

{

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

}

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

//编译错误。从静态内部类中引用非静态变量x

}

}

 

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

可以通过newOuter.Inner().function();

 

class Outer

{

private static int x=3;//私有化,静态化x

static class Inner//静态化内部类。

{

void function()//function()是非静态

{

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

}

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

new Outer.Inner().function();//内部类对象调用非静态function方法。

}

}

 

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

可以通过newOuter.Inner.function();

class Outer

{

private static int x=3;//私有化,静态化x

static class Inner//静态化内部类。

{

Void staticfunction()//function()是静态的

{

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

}

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

new Outer.Inner.function();//加载Outer,加载Inner,加载function。

}

}

 

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

class Outer

{

private static int x=3;//私有化,静态化x

class Inner//非静态内部类。

{

static void function()//function()是静态

{

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

}

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

//编译错误,内部类中不能有静态声明。

}

}

 

class Outer

{

private staticint x=3;//私有化,静态化x

static class Inner//静态内部类。

{

static void function()//function()是静态的

{

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

}

}

class Inner2

{

void show()

{

System.out.println("inner2show");

}

}

public static void method()

{

Inner.function();//静态内部类中的静态成员

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer.method();//静态成员,访问静态变量。都不需要new!!!

}

}

//打印:

inner :3

 

class Outer

{

private static int x=3;//私有化,静态化x

static class Inner//静态内部类。

{

static void function()//function()是静态的

{

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

}

}

class Inner2

{

void show()

{

System.out.println("inner2show");

}

}

public static void method()

{

new Inner2().show();//创建new

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer.method();//编译错误静态成员,访问非静态方法。

}

}

 

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

class Outer

{

private static int x=3;//私有化,静态化x

static class Inner//静态内部类。

{

static void function()//function()是静态的

{

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

}

}

static class Inner2

{

void show()

{

System.out.println("inner2show");

}

}

public staticvoid method()

{

new Inner2().show();//访问静态内部类的非静态成员

}

}

class InnerClassDemo

{

public static void main(String[] args)

{

Outer.method();

}

}

//打印:

inner2 show

 

 

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

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

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

 

class Body

{

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

{

 

}

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

{

new XinZang();

}

}

 

 

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

class Outer

{

         intx=3;

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

         {

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

                  {

                          voidfunction()

                          {

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

                          }

                  }

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

         //编译运行报错。        

         }

}

}

}

 

class Outer

{

         intx=3;

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

         {      

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

找不到function,编译错误。

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

                  {

                          voidfunction()

                          {

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

                          }

                  }

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

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

         }

}

 

class Outer

{

         intx=3;

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

         {

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

                  {

                          voidfunction()

                          {

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

                          }

                  }

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

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method();  

         }

}

//打印:

3

 

class Outer

{

         intx=3;

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

         {

                  inty=4;

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

                  {

                          voidfunction()

                          {

                                   System.out.println(y);

                          }

                  }

                  newInner().function();

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method();  

         }

}//编译错误

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

 

内部类定义在局部时,

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

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

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

class Outer

{

         intx=3;

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

         {

                  final int y=4;

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

                  {

                          voidfunction()

                          {

                                   System.out.println(y);

                          }

                  }

                  newInner().function();

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method();  

         }

}//打印:

4

 

class Outer

{

         intx=3;

         voidmethod(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

{

         intx=3;

         voidmethod(final int a)

         {

                  finalint y=4;

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

                  {

                          voidfunction()

                          {

                                   System.out.println(a);

                          }

                  }

                  newInner().function();

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().method(7);

         }

}

//打印:

7

 

class Outer

{

         intx=3;

         voidmethod(final int a)

         {

                  final int y=4;

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

                  {

                          voidfunction()

                          {

                                   System.out.println(a);

                          }

                  }

                  newInner().function();

         }

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  Outerout=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(finalint a)

         {

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

                  finalint y=4;

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

                  {

                          voidfunction()

                          {

                                   System.out.println(a);

                          }

                  }

                  newInner().function();

         }

//编译误。

 

匿名内部类:

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

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

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

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

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

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

 

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

class AbsDemo

{

         abstract void show();//抽象

}

 

class Outer

{

         intx=3;

         classInner extends AbsDemo//Inner继承了AbsDemo

         {

         voidshow()

                  {

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

                  }

         }

         publicvoid function()

         {

                  newInner().show();

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

//打印:

show3

 

简化成匿名内部类。

class AbsDemo

{

         avstract voidshow();

}

 

class Outer

{

         intx=3;

         publicvoid function()

         {

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

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

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

                          voidshow()

                          {

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

                          }

                  };

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

 

再变化成更奇葩的!!!

class AbsDemo

{

         avstractvoid show();

}

 

class Outer

{

         intx=3;

         publicvoid function()

         {

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

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

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

                          voidshow()

                          {

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

                          }

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

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

//优点:简化代码

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

 

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

class AbsDemo

{

         abstract void show();

}

 

class Outer

{

         intx=3;

         publicvoid function()

         {

                  new AbsDemo()

                  {

                          voidshow()

                          {

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

                          }

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

                          {

                                   System.out.println("haha");

                          }

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

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

                  new AbsDemo()

                  {

                          voidshow()

                          {

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

                          }

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

                          {

                                   System.out.println("haha");

                          }

                  }.show();

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

//打印:

haha

x===3

 

class AbsDemo

{

         abstractvoid show();

}

 

class Outer

{

         intx=3;

         publicvoid function()

         {

                 

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

                  {

                          voidshow()//子类复写

                          {

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

                          }

                          voidabc()

                          {

                                   System.out.println("haha");

                          }

                  };

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

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

//打印:

x===3

 

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

class AbsDemo

{

         avstractvoid show();

         avstractvoid show1();

         avstractvoid show2();

         avstractvoid show3();

}

 

class Outer

{

         intx=3;

         publicvoid function()

         {//这样阅读性很差

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

                  {

                          publicvoid show()

                          {

                          publicvoid show1()

                          {

                          }

                          publicvoid show2()

                          {

                          }

                          publicvoid show3()

                          {

                          }

                  };

                 

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

                  {

                          voidshow()//子类复写

                          {

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

                          }

                          voidabc()

                          {

                                   System.out.println("haha");

                          }

                  };

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

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

 

class AbsDemo

{

         abstract void show();

}

 

class Outer

{

         intx=3;

         publicvoid function()

         {

                 

                  AbsDemod=new AbsDemo()

                  {//此时AbsDemo相当于类

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

                          voidshow()

                          {

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

                          }

                          voidabc()

                          {

                                   System.out.println("haha");

                          }

                  };

                  d.show();

         }

}

class InnerClassDemo

{

         publicstatic void main(String[] args)

         {

                  newOuter().function();

         }

}

//打印:

num===9

 

 

练习:

interface Inter

{

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

}

class Test

{

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

         static class InnerimplementsInter

         {

                  publicvoid method()

                  {

                          System.out.println("methodrun");

                  }

         }

         static function()//定义静态function

         {

                  returnnew Inner();       

         }

}

class InnerClassTest

{

         publicstatic void main(String[] args)

         {

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

         }

}

 

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

因为它是抽象的。

 

改成匿名的:

interface Inter

{

         voidmethod();

}

class Test

{

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

 

         staticfunction()

         {

                  returnnewInter()

                  {

                          publicvoid method()

                          {

                                   System.out.println("methodrun");                

                          }

                  };   

         }

}

class InnerClassTest

{

         publicstatic void main(String[] args)

         {

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

 

0 0
原创粉丝点击