首页 > 代码库 > Java中的匿名内部类
Java中的匿名内部类
通常通过继承某个类或实现某个接口的方式来编写代码,但是有时候某一些代码只使用一次,就没有必要写专门写一个子类或实现类了,可以采用匿名内部类的写法。最常用的场景是线程方面的应用。
一、不使用匿名内部类
①继承
abstract class Player
{
public abstract void play();
}
public class FootBallPlayer extends Player
{
public void play()
{
System.out.println("踢足球");
}
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
Player p1 = new FootBallPlayer();
p1.play();
}
}
②接口
interface IPlayer
{
public void play();
}
public class IPlayFootballImpl implements IPlayer
{
public void play()
{
System.out.println("踢足球");
}
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
IPlayer ip1 = new IPlayFootballImpl();
ip1.play();
}
}
二、使用匿名内部类
①继承
abstract class Player
{
public abstract void play();
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
Player p2 = new Player() {
public void play()
{
System.out.println("打篮球");
}
};
p2.play();
}
}
②接口
interface IPlayer
{
public void play();
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
IPlayer ip2 = new IPlayer() {
public void play()
{
System.out.println("打篮球");
}
};
}
}
三、线程中的应用
实现线程的方法有两种:①继承Thread类 ②实现Runnable接口。给出用匿名类实现的例子:
public class ThreadTest
{
public static void main(String[] args)
{
// 继承Thread类
Thread thread = new Thread() {
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
System.out.println(this.getName());
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
};
thread.start();
// 实现Runnable接口
Thread thread2 = new Thread(new Runnable() {
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
});
thread2.start();
}
}
一、不使用匿名内部类
①继承
abstract class Player
{
public abstract void play();
}
public class FootBallPlayer extends Player
{
public void play()
{
System.out.println("踢足球");
}
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
Player p1 = new FootBallPlayer();
p1.play();
}
}
②接口
interface IPlayer
{
public void play();
}
public class IPlayFootballImpl implements IPlayer
{
public void play()
{
System.out.println("踢足球");
}
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
IPlayer ip1 = new IPlayFootballImpl();
ip1.play();
}
}
二、使用匿名内部类
①继承
abstract class Player
{
public abstract void play();
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
Player p2 = new Player() {
public void play()
{
System.out.println("打篮球");
}
};
p2.play();
}
}
②接口
interface IPlayer
{
public void play();
}
public class AnonymousInnerClassTest
{
public static void main(String[] args)
{
IPlayer ip2 = new IPlayer() {
public void play()
{
System.out.println("打篮球");
}
};
}
}
三、线程中的应用
实现线程的方法有两种:①继承Thread类 ②实现Runnable接口。给出用匿名类实现的例子:
public class ThreadTest
{
public static void main(String[] args)
{
// 继承Thread类
Thread thread = new Thread() {
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
System.out.println(this.getName());
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
};
thread.start();
// 实现Runnable接口
Thread thread2 = new Thread(new Runnable() {
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
}
});
thread2.start();
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。