interface Mylistener { public void fn(); }class C implements Mylistener { public void fn() { System.out.println("class C"); } } class A { Mylistener lsr = new C(); //获得接口的实例 public void fn() { System.out.println("class A"); } }class B implements Mylistener { A a1 = new A(); public void fn() { System.out.println("class B"); } public void print() { a1.fn(); this.fn(); a1.lsr.fn(); } public static void main(String[] args) { B b1 = new B(); b1.print();
} }
类A和类B啥关系? 如果是A继承B,就会出现这种情况
interface myf { void f(int c); } class A { public myf l;
public void g(myf t) { l=t;
} } public class interf1 implements myf{ private A a1;public static void main(String arg[]) {A a=new A(); a.g(this); a.l.f(3); } void f(int d){ System.out.println("f() in B"+d); } }这是我的想法。。但是出现错误,不知道为什么?interf1.java:30: interf1 中的 f(int) 无法实现 myf 中的 f(int);正在尝试指定更低的访问权限;为 public void f(int d){ ^ interf1.java:27: 无法从静态上下文中引用非静态 变量 this
我找到资料了。是回调问题 interface Callable { void call(); }class Listener { private Callable caller; Listener() { } Listener(Callable aCaller) { caller = aCaller; }void setListener(Callable a){caller=a; } void handle() { if (caller == null) return; caller.call(); } }public class CallBack implements Callable { public void call() { System.out.println("After Calling..."); } public void g(){ Listener l = new Listener(); l.setListener(this); l.handle(); } public static void main(String[] args) { // Listener l = new Listener(new CallBack()); CallBack cb=new CallBack(); cb.g(); } }
interface PCI { void start(); void stop(); } class NetCard implements PCI { public void start() { System.out.println("NetCard is working!!"); } public void stop() { System.out.println("NetCard is Stop!!!"); } } class SoundCard implements PCI { public void start() { System.out.println("SoundCard is working!!"); } public void stop() { System.out.println("SoundCard is Stop!!!!!"); } } class MainBoard { public void user(PCI p){ p.start(); p.stop(); } } class Admin { public static void main(String [] args) { MainBoard mb = new MainBoard(); NetCard nc = new NetCard(); SoundCard sc = new SoundCard(); mb.user(nc); mb.user(sc); if(nc instanceof PCI) System.out.println("True"); else System.out.println("False"); if(nc instanceof NetCard) System.out.println("True"); else System.out.println("False");
你的A又没有f()方法
A的实例怎么调用f()
应该是A的那个实现接口的成员变量
{
public void fn();
}class C implements Mylistener
{
public void fn()
{
System.out.println("class C");
}
}
class A
{
Mylistener lsr = new C(); //获得接口的实例
public void fn()
{
System.out.println("class A");
}
}class B implements Mylistener
{
A a1 = new A();
public void fn()
{
System.out.println("class B");
}
public void print()
{
a1.fn();
this.fn();
a1.lsr.fn();
}
public static void main(String[] args)
{
B b1 = new B();
b1.print();
}
}
如果是A继承B,就会出现这种情况
{
void f(int c);
} class A
{
public myf l;
public void g(myf t)
{
l=t;
}
}
public class interf1 implements myf{
private A a1;public static void main(String arg[])
{A a=new A();
a.g(this);
a.l.f(3);
}
void f(int d){
System.out.println("f() in B"+d);
}
}这是我的想法。。但是出现错误,不知道为什么?interf1.java:30: interf1 中的 f(int) 无法实现 myf 中的 f(int);正在尝试指定更低的访问权限;为 public
void f(int d){
^
interf1.java:27: 无法从静态上下文中引用非静态 变量 this
interface Callable { void call(); }class Listener {
private Callable caller;
Listener() { }
Listener(Callable aCaller) { caller = aCaller; }void setListener(Callable a){caller=a; }
void handle() {
if (caller == null) return;
caller.call();
}
}public class CallBack implements Callable {
public void call() {
System.out.println("After Calling...");
}
public void g(){
Listener l = new Listener();
l.setListener(this);
l.handle();
}
public static void main(String[] args) {
// Listener l = new Listener(new CallBack());
CallBack cb=new CallBack();
cb.g();
}
}
{
void start();
void stop();
}
class NetCard implements PCI
{
public void start()
{
System.out.println("NetCard is working!!");
}
public void stop()
{
System.out.println("NetCard is Stop!!!");
}
}
class SoundCard implements PCI
{
public void start()
{
System.out.println("SoundCard is working!!");
}
public void stop()
{
System.out.println("SoundCard is Stop!!!!!");
}
}
class MainBoard
{
public void user(PCI p){
p.start();
p.stop();
}
}
class Admin
{
public static void main(String [] args)
{
MainBoard mb = new MainBoard();
NetCard nc = new NetCard();
SoundCard sc = new SoundCard();
mb.user(nc);
mb.user(sc);
if(nc instanceof PCI)
System.out.println("True");
else
System.out.println("False");
if(nc instanceof NetCard)
System.out.println("True");
else
System.out.println("False");
}
}
这是我根据书上写的程序.你可以看一下.