//请问Compute类内输入什么代码,会使最后程序结果显示ans=8
abstract class Math
{
int ans;
void show(){
System.out.println( "ans= "+ans);
}
abstract int add(int a, int b);
}
class Compute extends Math
{
//请完成这个部分的程序代码
}
public class app1
{
public static void main(String args[])
{
Compute cmp=new Compute();
cmp.add(3,5);
cmp.show(); //此行会响应 " ans=8 "字符串
}
}
------解决方案--------------------
}
class Compute extends Math
{
int add(int a, int b) {
return ans = a + b;
}
}
------解决方案--------------------
abstract class Math
{
int ans;
void show(){
System.out.println( "ans= "+ans);
}
abstract int add(int a, int b);
}
class Compute extends Math
{
int ans;
@Override
int add(int a, int b)
{
ans=a+b;
return ans;
}
@Override
void show()
{
System.out.println( "ans= "+ans);
}
}
public class app1
{
public static void main(String args[])
{
Compute cmp=new Compute();
cmp.add(3,5);
cmp.show();
}
}
------解决方案--------------------
public void add(int i,int j){super.ans=i+j;}