Javaオブジェクトに向かう匿名の内部クラス
1408 ワード
http://www.verejava.com/?id=16992872428362
/**
:
: , ,
:
1. :
1.1 (Moto), (Alarm), (Person)
2. :
2.1 Alarm -> Moto(1 1)
2.2 Alarm
3. :
3.1 Moto( , )
3.2 Alarm( )
3.3 Person( )
4. :
4.1 Moto{load(Alarm alarm)}
4.2 Person{touch(Moto moto)}
4.3 Alarm{beep()}
*/
public class TestMoto
{
public static void main(String[] args)
{
//
Moto moto=new Moto(" ");
//
moto.load(new SmallAlarm());
//
Person p=new Person(" ");
// ,
p.touch(moto);
}
}
interface Alarm
{
/**
*/
public void beep();
}
class SmallAlarm implements Alarm
{
/**
*/
public void beep()
{
System.out.println(" , ");
}
}
class Moto
{
private String brand;//
private Alarm alarm;//
public Moto(String brand)
{
this.brand=brand;
}
public Alarm getAlarm()
{
return this.alarm;
}
/**
*/
public void load(Alarm alarm)
{
this.alarm=alarm;
}
}
class Person
{
private String name;//
public Person(String name)
{
this.name=name;
}
/**
,
*/
public void touch(Moto moto)
{
moto.getAlarm().beep();
}
}
http://www.verejava.com/?id=16992872428362