内部类:一个类内部包含另一个类。
分类:
1、成员内部类
2、局部内部类(包含匿名内部类)
成员内部类:
public class Demo1 {
public class InnerClassName{
}
}
注意:内用外,随意访问,外用内,需要内部类对象。
如何使用成员内部类:
1、间接方式:在外部类的方法中,使用内部类,然后main只是调用外部类的方法。
2、直接方式:外部类名称.内部类名称 name = new 外部类名称().new 内部类名称();
public class Demo1 {
Demo1() { }
void method() {
System.out.println("外部类方法调用内部类方法");
InnerClassName innerClassName = new InnerClassName();
innerClassName.innerMethod();
System.out.println("===========");
}
public class InnerClassName {
void innerMethod() {
System.out.println("内部类的方法");
}
}
}
public class test1 {
public static void main(String[] args) {
//间接
Demo1 demo1 = new Demo1();
demo1.method();
//直接
Demo1.InnerClassName innerClassName = new Demo1().new InnerClassName();
innerClassName.innerMethod();
}
}
内部类的同名变量访问
public class Outer {
int number = 1111;
public class Inner{
int number = 2222;
void innerMethod(){
int number = 3333;
System.out.println(number);
System.out.println(this.number);
System.out.println(Outer.this.number);
}
}
}
public class test1 {
public static void main(String[] args) {
Outer.Inner inner = new Outer().new Inner();
inner.innerMethod();
}
}
局部内部类
如果一个类是定义在一个方法内部的,那么这就是一个局部内部类
只有在当前所属的方法才能使用这个类,出了这个方法就不能使用。
需要在方法内部进行使用。
public class Outer {
public void outerMethod(){
class Inner {
public void innerMethod(){
System.out.println("局部内部类的方法");
}
}
Inner inner = new Inner();
inner.innerMethod();
}
}
public class test1 {
public static void main(String[] args) {
Outer outer = new Outer();
outer.outerMethod();
}
}
1、外部内:public/(default)修饰
2、成员内部类:public / protected / (default) / private
3、局部内部类:什么都不能写效果和default是不一样的
局部内部类的final问题
局部内部类,如果希望访问所在方法的局部变量,那么这个局部变量必须是有效final的。
public class Outer {
public void outerMethod(){
int number = 11111;
// number = 2222; // 错误
class Inner {
public void innerMethod(){
System.out.println(number);
}
}
Inner inner = new Inner();
inner.innerMethod();
}
}
匿名内部类
如果接口的实现类(或者是父类的子类)只需要使用唯一的一次,那么这种情况下就可以省略掉该类的定义,而使用匿名内部类。
public class test1 {
public static void main(String[] args) {
Interface anInterface = new Interface(){
@Override
public void method() {
System.out.println("匿名内部类使用,最后的分号不能掉");
}
};
anInterface.method();
}
}
注意问题:
1、匿名内部类,在创建对象的时候,只能使用唯一一次;如果希望多次使用时,需要创建接口实现类。
2、匿名内部类只是省略了类名称,没有省略对象名称。
3、匿名内部类是省略了实现类或者子类名称,但是匿名对象是省略了对象名称。
public class test1 {
public static void main(String[] args) {
Interface anInterface = new Interface(){
@Override
public void method() {
System.out.println("匿名内部类使用,最后的分号不能掉");
}
};
anInterface.method();
new Interface(){
@Override
public void method() {
System.out.println("匿名内部类,匿名对象使用,最后的分号不能掉");
}
}.method();
}
}
类作为成员变量类型
String name; // String就是一个类
接口作为成员变量类型
public class InterfaceMemberVariable {
private String name;
private Interface anInterface;
public InterfaceMemberVariable() {
}
public InterfaceMemberVariable(String name, Interface anInterface) {
this.name = name;
this.anInterface = anInterface;
}
public void methodUse(){
System.out.println("我叫" + name );
anInterface.method();
System.out.println("接口成员变量的使用!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Interface getAnInterface() {
return anInterface;
}
public void setAnInterface(Interface anInterface) {
this.anInterface = anInterface;
}
}
public interface Interface {
void method();
}
public class Realize {
public static void main(String[] args) {
InterfaceMemberVariable interfaceMemberVariable = new InterfaceMemberVariable();
interfaceMemberVariable.setName("接口成员变量");
interfaceMemberVariable.setAnInterface(new Interface() {
@Override
public void method() {
System.out.println("匿名对象,匿名内部类的使用");
}
});
interfaceMemberVariable.methodUse();
}
}
只要涉及类型的时候,任何类型都可以使用。
接口做方法的参数和返回值
import java.util.ArrayList;
import java.util.List;
public class Realize {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
List<String> result = add(list);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
}
public static List<String> add(List<String> list){
list.add("盖伦");
list.add("亚索");
list.add("小炮");
return list;
}
}
原文:https://www.cnblogs.com/saonian450/p/12416815.html