通过接口访问如何实现呢。
以前所做过的练习都是这样的,比如写一个类:
public class a{
public int add(int i){
return i*100;
}
}
在另外一个类如果要用这个add的方法,就这样。
public class b{
public staitc void main(String args[]){
a aa =new a();
int sum=aa.add(20);
System.out.println(sum);
}
}
组长要求我们要用面向接口的编程方式来实践:
不直接访问类里边的方法,而是通过接口来进行
相应的方法访问。。。。
那么该如何写呢。。
偶的思路漫乱的。。
哪个朋友能根据偶现在写的这个简单的来帮偶写
一个简单的呢。拜托了。
问题点数:20、回复次数:3Top
1 楼heihei425(呵呵)回复于 2005-06-01 19:23:18 得分 0
在线等,一直到问题解决为止。。Top
2 楼iceandfire(【咖啡沫】)回复于 2005-06-01 20:25:31 得分 20
是这意思吗?随便写了一个
public interface Amethod
{
public int add(int i);
}
public class A1 implements Amethod
{
public int add(int i)
{
return i*100;
}
}
public class A2 implements Amethod
{
public int add(int i)
{
return i*200;
}
}
public class Main
{
public static void main(String[] args)
{
Amethod a1 = new A1();
Amethod a2 = new A2();
int sum = a.add(20);
System.out.println(sum);
sum = a2.add(20);
System.out.println(sum);
}
}Top
3 楼heihei425(呵呵)回复于 2005-06-01 20:50:20 得分 0
不错。谢谢楼上的,要的就是这样的。。Top




