2016-02-12 87 views
0
import java.awt.Rectangle; 
public class Rec { 

public static void main(String[] args) { 
    Rectangle r1; 
    r1 = new Rectangle(2, 5, 15, 15); 
    System.out.println(r1.getHeight); 
} 
} 

,我得到这样一个错误:Java的矩形找不到符号

System.out.println(r1.getHeight); 
         ^
symbol: variable getHeight 
location: variable r1 of type Rectangle 
1 error 

我不明白什么是错的这个代码,我是新用户,因此它可能很简单,但我不能“找不到问题:(

+1

'r1.getHeight()'这是一种方法 –

回答

1

调用Java方法(和Rectangle.getHeight()是一种方法),你需要一个括号,如:

System.out.println(r1.getHeight()); 

经t他的方式,对于接受参数的方法,你会放在括号之间的那些参数:

// This is a method declaration. It says it returns an integer 
// and it accepts two integers as its arguments. 
int addTwoNumbers(int a, int b) { 
    return a + b; 
} 

// Somewhere in the code, you could call this method by passing 
// two integers in there: 
int number1 = 1; 
int number2 = 10; 
int result = someobject.addTwoNumbers(a, b); 
// result is now 11. 
0

使用r1.getHeight()而不是r1.getHeight因为definded在Rectangle类getHeight();。用于获取此Rectangle的边界Rectangle。 Link