一、不使用匿名内部类
①继承
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("打篮球");
}
};
}
}