[英]How to call a non-static method from main in Java?
我一直在為Comp Sci入門課程設計實驗室,但遇到了以下問題:
按照說明,我必須擁有回答特定問題的所有方法(例如:問題5.9,方法是fourPointNine(),並且必須在main中由另一個類(Assignment6.main)調用。我的問題是main是靜態的,我需要為該實驗室調用的最終方法是非靜態的,因此它將不允許我編譯和運行。我遇到的三個錯誤與multiSphere(x)有關。具體錯誤是“無法靜態引用類型Assignment6中的非靜態方法“ multiSphere(double)”。”
public class Assignment6
{
public Sphere newSphere = new Sphere();
public Assignment6 A6 = new Assignment6();
public static int x;
public static void main(String[] args)
{
x = 2 + (int)(Math.random() * ((10 - 2) + 1));
multiSphere(x);
x = 2 + (int)(Math.random() * ((10 - 2) + 1));
multiSphere(x);
x = 2 + (int)(Math.random() * ((10 - 2) + 1));
multiSphere(x);
}
public void multiSphere(double diameter)
{
Sphere.inputDiameter(diameter);
Sphere.volumeCalc(diameter);
Sphere.surfaceAreaCalc(diameter);
toString();
}
public String toString()
{
return "The diameter of this sphere is " + Sphere.inputDiameter(x) + ", the volume is " + Sphere.volumeCalc(x) + ", and the surface area is " + Sphere.surfaceAreaCalc(x) + ". ";
}
我的第二堂課叫做Sphere,看起來像這樣:
public class Sphere
{
public Assignment6 newA6 = new Assignment6();
public static double volume;
public static double surfaceArea;
public static double inputDiameter(double diameter)
{
return diameter;
}
public static double volumeCalc(double diameter)
{
// Volume = (4.0/3.0)(pi)(r)^3
volume = (4.0/3.0) * (Math.PI) * Math.pow((diameter/2.0), 3);
return volume;
}
public static double surfaceAreaCalc(double diameter)
{
// Surface area = (4)(pi)(r)^2
surfaceArea = (4) * (Math.PI) * (Math.pow((diameter/2.0), 2));
return surfaceArea;
}
}
我不確定如何在main方法中調用multiSphere(x)而不遇到我遇到的錯誤。 我感覺好像缺少了這么簡單的東西。
關於
如何在Java中從main調用非靜態方法?
簡短的答案:您不會,至少不會直接。 規范的答案是使用該方法創建該類的實例,然后在該實例上調用該方法,但是對於您而言,這是毫無用處的,因為該類除靜態字段和方法外別無其他。 這使您的toString方法在一個包含靜態所有內容的類中變得毫無意義。 要么使該方法成為靜態方法(並且它不是真正的toString()
重寫),要么使用非靜態字段和方法創建一個真實的類。
正確的方法是擺脫Sphere類和此處的關鍵類的所有靜態內容 ,然后為它提供一個體面的public String toString()
方法重寫。 在您發布的兩個類中,它是唯一一個toString()
方法有意義的類,但前提是它是帶有非靜態方法和字段的真正的面向對象類。
接下來,您需要提供有意義的Sphere getter和setter方法。 這個:
public static double inputDiameter(double diameter)
{
return diameter;
}
既不是魚也不是犯規。 擺脫它,創建真正的getter和setter,這些getter不帶參數但返回值,而setter則帶參數並聲明返回void。
接下來,在靜態main方法中,您將創建一個Sphere變量,並為其分配一個新的Sphere實例。 然后,您可以調用Sphere的適當方法,包括其toString()
方法。
簡而言之,請廢棄所有代碼並重新開始。 從最重要的一點開始-創建一個不錯的Sphere類。 然后創建您的Assignment類(帶有main方法的類),並創建您的Sphere實例。
如果您仍然感到困惑,請發布您的實際作業指導 ,而不是您對它們的解釋或措辭,這樣我們就可以准確地知道您應該做什么。
例如,使用與您相似但不相同的相關示例:
CircleTest.java
public class CircleTest {
public static void main(String[] args) {
Circle circle = new Circle(10.0);
System.out.println(circle); // calls toString()
}
}
Circle.java
public class Circle {
private double diameter;
public Circle(double diameter) {
this.diameter = diameter;
}
public double getDiameter() {
return diameter;
}
public double calculateArea() {
return Math.PI * diameter * diameter / 4.0;
}
public double calculateCircumference() {
return Math.PI * diameter;
}
@Override
public String toString() {
String result = String.format("Circle with diameter %.2f, area %.2f, "
+ "circumference %.2f",
diameter, calculateArea(), calculateCircumference());
return result;
}
}
這里有很多事情需要快速糾正-尤其是決賽即將到來。
首先,您需要了解Sphere
類應該如何工作。 Sphere
每個實例都是自己的東西,並且知道如何做。 Sphere
給定一個Sphere
的直徑(或半徑),就可以知道其自身所需的一切。 它可以計算其體積和表面積,並可以將其自身打印到toString
。
因此,您希望Sphere
看起來像這樣:
public class Sphere {
public double diameter;
public Sphere(double diameter) {
this.diameter = diameter;
}
public double volume() {
return (4.0 / 3.0) * (Math.PI) * Math.pow((diameter / 2.0), 3);
}
public double surfaceArea() {
return (4) * (Math.PI) * (Math.pow((diameter / 2.0), 2));
}
public String toString()
{
return "The diameter of this sphere is " + diameter + ", the volume is " + volume() + ", and the surface area is " + surfaceArea() + ". ";
}
}
注意Sphere
如何具有構造函數,這是我們如何創建Sphere
新實例。 我們只用直徑來做。 一旦有了它, Sphere
實例就可以完成Sphere
的所有工作。
注意也沒有static
。 我不知道你為什么如此愛上static
。
您應該知道, static
任何事物都意味着該類的每個實例都共享它。 這不適用於Sphere
因為每個實例都有自己的直徑。 這不像一堆Sphere
共享一個直徑。 有些直徑可能相等,但它們仍然是自己的。 就像您和我如何駕駛賓利汽車(一廂情願)一樣,在各個方面都一樣,但它們仍然是兩輛不同的汽車。 當然, static
事物不能訪問非static
事物,因為非static
事物需要成為可能不可用的對象實例的一部分。
最后,在主類Assignment6
,您只需執行以下操作:
Sphere s = new Sphere(5);
創建的實例Sphere
直徑5.現在s
可以告訴我們它的體積,表面積,並且通過它描述toString
。
我實際上不確定您的任務是什么,但希望能對您有所幫助。
您可以使用新實例來調用如下方法:
public static void main(String[] args)
{
Assignment6 assignment6 = new Assignment6();
....
assignment6.multiSphere(x);
....
assignment6.multiSphere(x);
....
assignment6.multiSphere(x);
}
但是程序有一些錯誤,請檢查一下。
首先,將變量(newSphere,A6和x)設為私有。 全局變量幾乎總是應該是私有的。
就您的問題而言,創建一個對象並對其進行調用,而不是使用main方法調用multiSphere。
public static void main(String[] args) {
...
Assignment6 newAssig = new Assignment6();
...
newAssig.multiSphere();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.