感谢你花时间看这个,我已经尝试了我所知道的所有内容,并且我继续在主要的第7,10,13和16行(类测试)上得到同样的错误
public class Test
{
public static void main (String[] args)
{
int res;
res = new Calculadora.doAdd();
System.out.println("The addition is: "+ res);
res = new Calculadora.doMul ();
System.out.println("\n The multiplication is: "+ res);
res = new Calculadora.doDiv ();
System.out.println("\n The division is: "+ res);
res = new Calculadora.doSub ();
System.out.println("The substraction is: "+ res);
Calculadora obj1 = new Calculadora ();
Calculadora obj2 = new Calculadora ();
Calculadora.contar();
Calculadora.contar();
System.out.println("Obj1: "+obj1.cont + " \nObj2: "+ obj2.cont);
}
}//fin del main
第二个程序:
public class Calculadora
{
public Addition add;
public Substraction sub;
public Multiplication mul;
public Division div;
static int cont = 0;
static void contar()
{
cont++;
}
public Calculadora ()
{
add = new Addition();
sub = new Substraction();
mul = new Multiplication();
div = new Division();
}
public int doAdd ()
{
return add.doAdd ();
}
public int doSub ()
{
return sub.doSub ();
}
public int doMul ()
{
return mul.doMul ();
}
public int doDiv ()
{
return div.doDiv ();
}
}
具有操作属性的操作程序:
public class Opperation
{
public int a, b;
public Opperation ()
{
a = 1;
b = 2;
}
}
最后,操作相当容易。
public class Substraction extends Opperation //Substraction
{
public int doSub ()
{
return a-b;
}
}//substraction
public class Multiplication extends Opperation //Multiplication
{
public int doMul ()
{
return a*b;
}
}//Multiplication
public class Division extends Opperation //Division
{
public int doDiv ()
{
return a/b;
}
}//Division
public class Addition extends Opperation//Addition
{
public int doAdd ()
{
return a+b;
}
}//Addition
我知道有一种更简单的方法来做一个计算器,但老师用七个类问了它,我继续得到同样的错误,指向新的
中的点答案 0 :(得分:1)
您实例化Calculadora
的方式是错误的
res = new Calculadora.doAdd(); // wrong.
应该改为
res = new Calculadora().doAdd(); //correct
或
Calculadora cal = new Calculadora();
res = cal.doAdd();