Java函数重写
假设我有类A和类B。那么,主要来说,有可能得到类A的返回值吗?我现在正在做作业,但似乎做不好,所以这里是我想问的例子Java函数重写,java,Java,假设我有类A和类B。那么,主要来说,有可能得到类A的返回值吗?我现在正在做作业,但似乎做不好,所以这里是我想问的例子 public class A { private double total=10; public double test() { return total; } } public class B extends A { private double totalB=20; public double test()
public class A
{
private double total=10;
public double test()
{
return total;
}
}
public class B extends A
{
private double totalB=20;
public double test()
{
return (super.test()+10);
}
}
public class C
{
public static void main(String [] args)
{
double Sum;
B FTest = new B();
System.out.println("I want to get back return value of class A for calculate\n"+FTest.test()/2);
//So the answer i want to get is suppose to be 5
System.out.println("Again i want to use return value of class A\n"+FTest.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
我得到的答案
I want to get back return value of class A for calculate
30.0
Again i want to use return value of class A
300.0
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
我想要的答案
I want to get back return value of class A for calculate
5
Again i want to use return value of class A
100
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
所以我要问的直接问题是,我是否能够使用
B反对。如果可以,怎么做 由于您已经创建了类B的对象,您可以获取在类A中初始化的变量的值,除非您修改了类B的测试方法super关键字允许您访问超类的属性和方法
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
由于total在A中是私有的,所以您需要通过A的测试方法获取它
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
因此,B应成为:
public class B extends A
{
private total=20;
public double test()
{
return super.test()+10;
}
}
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
下面是我可以写的代码..更多关于类和变量的定义,在你提交的代码中有很多混合器。。请参考您的资料
class A
{
int total=10;
public double test()
{
return total;
}
}
class B extends A
{
private int total=20;
public double test()
{
return total+10;
}
}
public class C
{
public static void main(String [] args)
{
A class1 = new A();
System.out.println("I want to get back return value of class A for calculate");
//So the answer i want to get is suppose to be 5
//If override occur only the second time i invoke the method, then how about i want to use it back again? For calculation.....
System.out.println("Again i want to use return value of class A"+class1.test()*10);
//Here the value i want to get is 10*10 = 100
}
}
你能把这个Java修改成实际编译的东西吗?还有,既然这是家庭作业,你能把要问的实际问题包括进去吗;不清楚您是否理解了问题的要求。@Dancrumb,代码编辑,至于家庭作业问题,问题实际上不同,但需要使用覆盖。所以现在我只是想知道,我是否能够使用main中声明的对象B在类A中而不是类B中获取test方法的返回值来进行计算。好的,对不起,我编辑了代码~有没有办法使用main中声明的对象B调用类A中的test方法?比如FTest.super.test;