預訂答案:您無法在靜態方法中調用非靜態方法(除非您創建一個對象作爲非靜態方法的調用對象)。我沒有得到括號中的部分,我試圖在靜態方法中創建對象,並且eclipse說「無法訪問的代碼」。有人可以解釋爲什麼,也可以舉一個例子。由於 (語言是Java)你可以在靜態方法中調用非靜態方法嗎?
public class RoundStuff {
public static final double PI = 3.141459;
public static double area(double radius){//Area of circle
return(PI*radius*radius);
}
public static double volume(double radius){//Volume of sphere
return((4.0/3.0)*PI*radius*radius*radius);
}
public void print(){
System.out.print("I am not parasyte");
}
}
import java.util.Scanner;
public class RoundStuffDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter radius");
double radius = keyboard.nextDouble();
RoundStuff round = new RoundStuff();
System.out.println("A circle of radius " + radius + " inches");
System.out.println("has an area of " + round.area(5.5) + " square inches.");
System.out.println("A sphere of radius " + radius + " inches");
System.out.println("has a volume of " + RoundStuff.volume(radius) + " cubic inches.");
}
}
發佈提供問題的代碼,有人可以嘗試解釋 –
'round.area(5.5)'是對實例的靜態調用。日食給你的確切消息是什麼,你不明白哪一部分? –