学习JAVA第十三天

时间:2021-08-16
本文章向大家介绍学习JAVA第十三天,主要包括学习JAVA第十三天使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

内部类与匿名类

*内部类定义

将类的定义 class xxx{...}放入一个类的内部,编译器生成 xxx$xxx的class文件  //内部类不能与外部类同名

*内部类使用

封装它的类的内部使用内部类,与普通类的使用方式相同

在其他地方,类名前要冠以外部类的名字  在用new创建内部类时,也要在new前面冠以对象变量

外部对象名.new.内部类名(参数)

class TestInnerClass{
 public static void main( String[] args ){
  Parcel p = new Parcel();
  p.testShip();
  Parcel.Contents c = p.new Contents(33);
  Parcel.Destination d = p.new Destination( "Hawii" );
  p.setProperty( c, d );
  p.ship();
 }
}
class Parcel {
  private Contents c;
  private Destination d;
  class Contents {
    private int i;
 Contents( int i ){ this.i = i; }
    int value() { return i; }
  }
  class Destination {
    private String label;
    Destination(String whereTo) {label = whereTo;}
    String readLabel() { return label; }
  }
  void setProperty( Contents c, Destination d ){
 this.c =c; this.d = d;
  }
  void ship(){
 System.out.println( "move "+ c.value() +" to "+ d.readLabel() );
  }
  public void testShip() {
    c = new Contents(22);
    d = new Destination("Beijing");
    ship();
  }
}
*在内部类中使用外部类成员
可以直接访问外部类的字段及方法
如果内部类有与外部类同名的字段或方法,可用  外部类名.this.字段及方法
public class TestInnerThis
{   
 public static void main(String args[]){
     A a = new A();
     A.B b = a.new B();
     b.mb(333);
    }
}
class A
{
 private int s = 111;
 public class B {
     private int s = 222;
     public void mb(int s) {
         System.out.println(s);      // 局部变量s
         System.out.println(this.s);      // 内部类对象的属性s
         System.out.println(A.this.s);      //  外层类对象属性s
     }
    }
}
*内部类的修饰符
访问控制符 : public protected 默认 private   //外部类只能使用修饰或者默认  ,也可以用final,abstract等等
*static修饰符
用static修饰内部类,表面这个内部类实际是一种外部类
static使用时 : (1)实例化static类时,在new前面不需要用对象实例变量;(2)static类只能访问static成员
                         (3)static方法不能访问非static域及方法,也不能不带前缀new一个非static的内部类
class TestInnerStatic
{
 public static void main(String[] args)
 {
  A.B a_b = new A().new B();  // ok
  A a = new A();
  A.B ab =  a.new B();
  Outer.Inner oi = new Outer.Inner();
  //Outer.Inner oi2 = Outer.new Inner();  //错误
  //Outer.Inner oi3 = new Outer().new Inner(); //错误
 }
}
class A   
{
 private int x;
 void m(){
  new B();
 }
 static void sm(){
  //new B();  // error!!!!
 }
 class B
 {
  B(){ x=5; }
 }
}
class Outer  
{
 static class Inner
 {
 }
}
(2)局部类
在一个方法中丁玉磊,这种类称为局部类或者“方法中的内部类”
class TestInnerInMethod
{
 public static void main(String[] args)
 {
  Object obj = new Outer().makeTheInner(47);
  System.out.println("Hello World!" + obj.toString() );
 }
}
class Outer
{
 private int size = 5;
 public Object makeTheInner( int localVar )
 {
  final int finalLocalVar = 99;
  class Inner  {
   public String toString() {
    return ( " InnerSize: " + size +
     // " localVar: " + localVar +   // Error!
     " finalLocalVar: " + finalLocalVar
    );
   }
  }
  return new Inner();
 }
}
*使用局部类
可以访问外部类成员,不能访问该方法的局部变量,除非是final局部变量
(3)匿名类
特殊的内部类,没有名字,在定义类的同时生成该对象的一个实例,算“一次性使用”的类
class TestInnerAnonymous
{
 public static void main(String[] args)
 {
  Object obj = new Outer().makeTheInner(47);
  System.out.println("Hello World!" + obj.toString() );
 }
}
class Outer
{
 private int size = 5;
 public Object makeTheInner( int localVar )
 {
  final int finalLocalVar = 99;
  return new Object()  {
   public String toString() {
    return ( " InnerSize: " + size +
     " finalLocalVar: " + finalLocalVar
    );
   }
  };
 }
}
*匿名类的使用
不取名字,直接用父类或接口的名字
类定义同时创建实例,既类的定义前有一个new   new 类名或接口名(){...}
在构造对象时使用父类构造方法,如果new对象时,要带参数
2.lambda表达式
*基本写法
(参数) ->结果,如(string s)->s.length()
class LambdaRunnable  {
    public static void main(String argv[]) {
  Runnable doIt =  new Runnable(){
   public void run(){
    System.out.println("aaa");
   }
  };
  new Thread( doIt ).start();
  Runnable doIt2 = ()->System.out.println("bbb");
  new Thread( doIt2 ).start();
  new Thread( ()->System.out.println("ccc") ).start();
  
    }
}

原文地址:https://www.cnblogs.com/wangao666/p/15149952.html