首页 技术 正文
技术 2022年11月8日
0 收藏 378 点赞 1,364 浏览 1607 个字

  我们都知道Java中可以使用内部类,将一个类的定义放在另一个类的定义的内部,这就是内部类,但是匿名内部类往往使我们摸不着头脑,因为它并没有特定的名称,那么该如何使用它呢?

定义一个匿名内部类

public interface Contents
{
int value();
}public class Test1
{
private Contents contents()
{
return new Contents()
{
private int i = 11;
@Override
public int value()
{
return i;
}
};
} public static void main(String[] args)
{
Test1 test = new Test1();
Contents con = test.contents();
System.out.println(con.value());
}
}

  如上,我们就定义了一个匿名内部类,它实现了Contents接口,通过new表达式返回的引用被自动向上转型为对Contents的引用,输出结果就是11。这是一个最基本的匿名内部类,只是输出了一个结果,下面我们来看看使用外部对象的匿名内部类。

使用带参数的匿名内部类

public interface Destination
{
String readLabel();
}public class Test2
{
private Destination destination(final String dest)
{
return new Destination()
{
private String label = dest;
@Override
public String readLabel()
{
return label;
}
};
} public static void main(String[] args)
{
Test2 test2 = new Test2();
Destination d = test2.destination("Wu Han");
System.out.println(d.readLabel());
}
}

  上面的匿名内部类传入了一个参数,该匿名内部类使用了一个在外部定义的对象,那么编译器就要求其参数引用是final的。但是在JDK1.8中,就算没有设置为final也没有报错。

对匿名内部类初始化

public interface Destination
{
String readLabel();
}public class Test3
{
public Destination destination(final String dest, final float price)
{
return new Destination()
{
private int cost;
{
cost = Math.round(price);
if(cost > 100)
{
System.out.println("超出预支");
}
} private String label = dest;
@Override
public String readLabel()
{
return label;
}
};
} public static void main(String[] args)
{
Test3 test3 = new Test3();
Destination d3 = test3.destination("Wu Han", 101.1F);
System.out.println(d3.readLabel());
}
}

  如上,该匿名内部类中对cost进行了初始化,由于匿名内部类是没有名称的,所以初始化方法自然也是没有名称的了。

  在使用匿名内部类后,我们再使用工厂方法,该方法就可以变得十分简洁易用。

  在最后说一下为什么我们需要内部类,最吸引我们的一个原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。在上面我们也看到了,使用匿名内部类,匿名内部类会直接实现接口,这在某些条件下会给我们带来便捷性。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289