java 关于接口

来源:百度知道 编辑:UC知道 时间:2024/06/22 11:59:14
帮我改改这程序吧。
报错是:System.out.println("The Sum is : " + s.disp());
System.out.println("The Pro is : " + p.disp());
此处不允许用VOID。
import java.io.*;
class test_24
{
static int n;
interface OneToN
{
public void disp();

}
static class Sum implements OneToN
{
public void disp()
{
long result=0;
for(int i=1; i<=n; i++)
{
result +=i;
}
}
}
static class Pro implements OneToN
{
public void disp()
{
long result=0;
for(int i=1; i<=n; i++)
{
result *=i;
}
}
}
public static void main(String[] args) throws IOException
{
InputStreamReader reader=new InputStreamReader<

import java.io.*;

class test_24 {
static int n;

interface OneToN{
// 你原来定义的方法是void的类型
// 那么他就不会返回任何东西了
// 而你的主方法中是想通过他的
// 返回值来进行操作的,这样
// 就会产生矛盾了导致你的程序无法
// 运行,所以要修正这个错误就是要
// 修改这个方法的发挥类型,使它反
// 回一个long型,同时实现了该接口的
// 类也要做相同的修改
public long disp();
}
static class Sum implements OneToN{
public long disp(){
long result=0;
for(int i=1; i<=n; i++)
{
result +=i;
}
return result;
}
}
static class Pro implements OneToN{
public long disp(){
long result=0;
for(int i=1; i<=n; i++)
{
result *=i;
}
return result;
}
}
public static void main(String[] args) throws IOException{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
Sy