import java.lang.Math;
public class FiguraProba
{
public abstract class Figura {
public abstract double pole();
public abstract double obwod();
}
public abstract class Czworokat extends Figura {
public double obwod(double bok1, double bok2, double bok3, double bok4)
{
return (bok1+bok2+bok3+bok4);
}
public abstract double pole();
}
public class Kwadrat extends Czworokat {
double bok1;
public Kwadrat(double bokPodany)
{
bok1=bokPodany;
}
public double pole(double bok1) {
return bok1*bok1;
}
}
public class Prostokat extends Czworokat {
double bok1, bok2;
public Prostokat(double bokPodany1, double bokPodany2)
{
bok1=bokPodany1;
bok2=bokPodany2;
}
public double pole(double bok1, double bok2) {
return bok1*bok2;
}
}
public static void main(String args[] )
{
//System.out.println(Math.sin(90*(Math.PI/180)));
}
}
我为我的英语道歉并使用英文变量名称。我的目标是:我有对象f.e squareExample with side 5.我想调用函数从类square计算区域并从类quadrange调用函数周长来计算周长。可能吗? Compilator说:" FiguraProba.Prostokat不是抽象的,并且不会在FiguraProba.Czworokat中抽象抽象方法pole()。"我理解错误,但我不知道如何解决错误以实现目标。
很抱歉使用外语代码变量和类。以下是所有单词的含义: figura - 图 kwadrat - square czworokat - 四边形 prostokat - 矩形 杆区 obwod - 周长 bok - side
答案 0 :(得分:1)
解决方案如下:
import java.lang.Math;
public class FiguraProba
{
public abstract class Figura {
public abstract double pole();
public abstract double obwod();
}
public abstract class Czworokat extends Figura {
public double obwod(double bok1, double bok2, double bok3, double bok4)
{
return (bok1+bok2+bok3+bok4);
}
public abstract double pole();
}
public class Kwadrat extends Czworokat {
double bok1;
public Kwadrat(double bokPodany)
{
bok1=bokPodany;
}
// Method without any arguments like the Figura class
// When you extend an abstract class, you have to
// implement the exact method with the exact number of arguments
// Of course you are obligated only if you can create an instance
// of that object. The perfect example when you are not obligated to
// implement a method is class Czworokat. Czworokat is not
// obligated, because you can not create an instance out of it.
public double pole() {
return bok1*bok1;
}
}
public class Prostokat extends Czworokat {
double bok1, bok2;
public Prostokat(double bokPodany1, double bokPodany2)
{
bok1=bokPodany1;
bok2=bokPodany2;
}
// Same logic applies here.
// When you extend an abstract class, you have to
// implement the exact method with the exact number of arguments
// Of course you are obligated only if you can create an instance
// of that object. The perfect example when you are not obligated to
// implement a method is class Czworokat. Czworokat is not
// obligated, because you can not create an instance out of it.
public double pole() {
return bok1*bok2;
}
}
public static void main(String args[] )
{
//System.out.println(Math.sin(90*(Math.PI/180)));
}
}
如果您只使用类属性并代表它返回值,那么这就是您想要的。如果你确实希望pole
方法带有一个和两个参数(就像你在代码中那样),那么你仍然需要删除抽象的pole
方法,或者你需要实现那个确切的方法ZERO论点。
你不能通过不实施没有参数的pole
来违反合同。如果您需要更深入地了解abstract
类,那么我建议您查看官方Java documentation。
答案 1 :(得分:0)
如果要覆盖抽象类中的方法,则必须在界面中将它看起来像它。所以你的方法应该有相同的名称和相同的参数。
在其他世界中,你有一个带有签名双极(double bok1)的方法,但你仍然需要覆盖方法dobule pole();