2014-02-19 38 views
-2

简单地说,我试图使用一种方法来做一个计算并返回计算。它说明两个无法弄清楚为什么java抛出返回类型错误

Invalid method declaration;return type required 
Incompatible types;unexpected return value 

这是类:

package shapes; 

public class RectangleCalc { 
    private double length; 
    private double width; 
} 

public RectangleCalc(double length, double width){ 
    this.length = length; 
    this.width = width; 
} 

public getArea() { 
    return length * width; 
} 

基本方法getArea()是扔我上面列出的错误。我知道为什么。

+2

getArea()方法的返回类型是什么? – rgettman

+1

你在“私人双倍宽度”之后关闭了你的课程;还是它是一个错字? – injecteer

回答

0

您缺少返回类型。

public double getArea() { 
    return length * width; 
} 
1

你想要得到的计算面积,但你没有申报的getArea方法的返回类型。它应该是

//double is the return type of the method. 
//Java requires you declared a return type 
public double getArea() 

退房本教程的返回类型http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

+0

谢谢,知道这是愚蠢的。 – Eric

+0

@Eric我不会说这是愚蠢的,有些语言不需要声明方法或函数的返回类型(例如Javascript)。您可能是Java的新手。事先做一点研究可以解决你的大部分问题:) – Bren

0

1.Need为函数返回类型。

2.需要在类中包含整个函数和变量。

public class RectangleCalc { 
    private double length; 
    private double width; 


    public RectangleCalc(double length, double width){ 
     this.length = length; 
     this.width = width; 
    } 

    public double getArea() { 
     return length * width; 
    } 
} 
相关问题