当前位置:   article > 正文

(匿名)内部类、引用类型作为方法参数和返回值_匿名内部类作为参数

匿名内部类作为参数
内部类

在一个类中,定义了另外第一分类,成为内部类(嵌套类)

public class Outer{
	public class Inner{
	}
}
  • 1
  • 2
  • 3
  • 4

Outer类,成为外部类,Inner类称为Outer类的内部类,或者是嵌套类
内部类可以使用外部类的成员,包括私有的
外部类如果使用内部类的成员,必须创建内部类的对象

成员内部类

一个类,定义在另一个类的成员位置,成员内部类,可以使用成员修饰符
内部类编译后会产生class文件:外部类名$内部类名.class

  • 内部类成员的调用格式
    在这里插入图片描述
外部类名.内部类名 变量名= new 外部类对象().new 内部类对象;
变量名.内部类成员
Outer.Inner oi=new Outer().new Inner();
oi.inner();
  • 1
  • 2
  • 3
  • 4
局部内部类

局部内部类,定义在了一个方法里面
局部内部类编译后,产生class文件 Outer$1Inner

public class Outer {
/*局部内部类的方法inner()访问方法out的变量a
为什么a必须是final的
final的神秘面纱:其实是给javac看的,没有修改过变量的值,编译成功
被final修饰,值固定的,终身不变,javac凡是遇到a的时候,全部编译为1
*/
public void out() {
   final int a = 1;
   class Inner{
   //内部类的方法,能否使用变量a
      public void inner() {
         System. out . println("=="+ a );
       }
     }
    new
    Inner(). inner();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
匿名内部类

必须是内部类,必须是局部位置,没有名字
使用匿名内部类,需要前提,要有类的继承或者接口实现
匿名内部类是一种代码上的简化写法:简化的是实现类实现接口,重写方法,创建对象
匿名内部类的书写格式

new 父类或者是接口(){
    抽象方法的重写
};
  • 1
  • 2
  • 3

匿名内部类格式应用

new MyInteface() {
   //抽象方法重写
   pub1ic void inter() {
      system. out. print1n("匠名内部类的实现"):
   }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

是一个综合体,综合了实现类,综合了方法重写,综合了创建对象
匿名内部类:接口实现类的匿名对象

引用类型作为方法参数和返回值
抽象类作为方法的参数和返回值
public abstract class Animal {
   public abstract void eat();
}
public class Cat extends Animal{
      public void eat() {
        System. out. print1n("猫吃猫粮");
      }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
public  interface Animal {
   public void eat();
}

public  interface Animal {
   public void eat();
}

public class Arguments01 {
   public static void main(String[] args) {
		setAnimal( new Cat() );
		Animal animal=getAnimal();
		animal.eat();
	}
 /* 
  * 定义方法,返回值是Animal
  * 方法中return 语句,必须放回Animal的子类对象
  */
 	public static Animal getAnimal() {
	 	return new Cat();
	 }
 	public static void setAnimal(Animal a) {
		 a.eat();
 	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
接口作为方法的参数和返回值
public interface MyInterface {
	pub1ic abstract void interO);
}
  • 1
  • 2
  • 3
public class MyInterfaceImp1 implements MyInterface{
  @override
   public void inter() {
    system. out. print1n("实现类重写方法");
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public static void main(String[] args) {
setMyInterface( new MyInterfaceImp1());
//调用方法,获取接口实现类对象
	MyInterface my = getMyInterface(); // new MyInterfaceImp1()
	my. inter();
}
/*
*定义方法,方法的返回值类型是接口类型
*方法中,return语句,返回接口实现类对象
*/
public static MyInterface getMyInterface() {
	return new MyInterfaceImp1();
}
/*
公
定义方法,方法的参数类型是接口类型
*调用方法,传递接口实现类对象
*
* MyInterface my = new MyInterfaceImp1()
*/
public static void setMyInterface (MyInterface my) {
	my. inter();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
引用类型作为方法的返回值另一种写法
public abstract class Animal {
	public abstract void eat();
/*
*抽象类是可以定义普通方法
*静态的方法,返回值是抽象类类型
*/
	public static Animal getAnima1() {
		return new Cat();
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号