JavaSE:11、内部类
学习 资源1
学习资源 2
1、成员内部类
import com.test.*;public class Main {public static void main(String [] argv){Person person=new Person();//Person构造函数Person.Woman woman=person.new Woman();//woman构造函数}
}
package com.test;public class Person {public Person(){System.out.println("Person构造函数");}public class Woman{public Woman(){System.out.println("woman构造函数");}}
}
内部类能访问外部成员,外部类不能访问内部类成员
package com.test;public class Person {public int age;public Person(){System.out.println("Person构造函数");//name="aaa";ERROR 不能使用内部类的成员}public class Woman{public String name;public Woman(){System.out.println("woman构造函数");age=18; //true 内部类能访问外部成员}}
}
同名的3种访问方式
import com.test.*;public class Main {public static void main(String [] argv){Person person=new Person();//Person构造函数Person.Woman woman=person.new Woman();//woman构造函数woman.test(20);// 方法参数age : 20// 内部类的age : 19// 外部类的age : 18}
}
package com.test;public class Person {public int age=18;public class Woman{public int age=19;public void test(int age){System.out.println("方法参数age : "+age); //参数System.out.println("内部类的age : "+this.age); //内部类成员System.out.println("外部类的age : "+Person.this.age); //外部成员}}
}
2、静态内部类
内部类为static,注意外部类不能为static
import com.test.*;public class Main {public static void main(String [] argv){Person.Woman.p();//我是静态内部类的静态方法Person.Woman woman=new Person.Woman();woman.test(20);// 方法参数age : 20// 内部类的age : 19// 外部类的age : 18}
}
package com.test;public class Person {public static int age=18;//静态内部类只能访问到外部静态内容public static class Woman{public int age=19;public void test(int age){System.out.println("方法参数age : "+age); //参数System.out.println("内部类的age : "+this.age); //内部类成员System.out.println("外部类的age : "+Person.age); //外部成员}public static void p(){System.out.println("我是静态内部类的静态方法");}}
}
3、局部内部类
在方法中定义一个类
package com.test;public class Person {public int age;public void test(){class T{public void p(){System.out.println("我是局部内部类");}}T t=new T();t.p();}//T t=new T();t.p(); 只能在方法里面用,外部用不了
}
import com.test.*;public class Main {public static void main(String [] argv){Person person=new Person();person.test();//我是局部内部类}
}
4、匿名内部类
匿名内部类是唯一一种没有构造方法的类,它就像是直接通过 new 关键字创建出来的一个对象
一般我们可以在抽象类的对象实现中使用匿名内部类
package com.test;public abstract class Person {public int age;public void say(){}}
import com.test.*;public class Main {public static void main(String [] argv){Person person=new Person(){@Overridepublic void say() {System.out.println("匿名内部类");}};person.say();}
}
去掉Person的abstra关键字变为普通类依然可以使用匿名内部类
区别在于抽象类new时必须写匿名内部类,而普通类不强制。
5、lambda表达式
- 标准格式为:
([参数类型 参数名称,]...) ‐> { 代码语句,包括返回值 }
- 和匿名内部类不同,Lambda仅支持接口,不支持抽象类
- 接口内部必须有且仅有一个抽象方法(可以有多个方法,但是必须保证其他方法有默认实现,必须留一个抽象方法出来)
import com.test.*;public class Main {public static void main(String [] argv){Study study=()->System.out.println("我是study");//重写了接口里study抽象方法study.study();}
}
package com.test;public interface Study {void study();static void a(){}default void b(){}public static final int d=0;
}
6、方法引用
方法引用就是将一个已实现的方法,直接作为接口中抽象方法的实现
import com.test.*;public class Main {public static void main(String [] argv){Study study=Integer::sum;System.out.println(study.study(3,5));//8}
}
package com.test;public interface Study {int study(int a,int b);}