package innerclasspackage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class Person
{
String name = "Josh";
void foo()
{
Print p = new toFile();
Print q = new toConsole();
Display(p);
Display(q);
}
private void Display(Print p) {
p.invoke();
}
interface Print {
void invoke();
}
class toFile implements Print
{
@Override
public void invoke() {
File f = new File("c:/josh/delegate.txt");
try{
PrintWriter fileOut =
new PrintWriter(new FileOutputStream(f));
fileOut.write(name);
fileOut.flush();
fileOut.close();
}catch(IOException ioe) {}
}
}
class toConsole implements Print
{
@Override
public void invoke() {
System.out.print(name+" ");
}
}
public static void main(String[] args)
{
Person p = new Person();
p.foo();
}
}
Monday, August 2, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment