[英]How do I access variables in the main class from another class in the package (java)?
我正在做作業,我很難在這里找到問題。 我寫的程序無法編譯。 請幫忙。 作業詳情:
“創建一個名為 Airport 的類,其中包含以下字段:標識符。由緯度和經度組成的坐標(不要分別創建兩個!!)。緯度為正,表示赤道以北,負則位於南半球. 經度為負表示是西,正表示它在格林威治中位數的東邊。磁變化也表示西為負,東為正。沒有磁變化是可以的。海拔高度以英尺為單位。
添加一個靜態方法,該方法接受四個雙精度值,即兩個偏移坐標(double lat1、double long1、double lat2、double long2)並使用 Lab 05 中給出的公式返回以海里為單位的距離。例如,聖地亞哥機場具有這些值ID: SAN, Lat: 32.7335556, Long: -117.1896667, Var: 14, Elev: 16.8' ( http://www.airnav.com/airport/SAN ) 該類應該有每個字段的訪問器和修改器方法。”
我在這里做了大部分工作,但我想我需要在這里添加構造函數。 請幫忙。
主要類:
package lab06;
import javax.swing.JOptionPane;
public class Lab06
{
public static void main(String[] args) {
double number; // To hold the number
String input; // To hold user input
//Create two Airport objects.
Airport firstAirport = new Airport();
Airport secondAirport = new Airport();
// Get and store the coordinates for firstAirport.
input = JOptionPane.showInputDialog("Enter the first Latitude: ");
number = Double.parseDouble(input);
firstAirport.setLatitude(number);
input = JOptionPane.showInputDialog("Enter the first Longitude: ");
number = Double.parseDouble(input);
firstAirport.setLongitude(number);
input = JOptionPane.showInputDialog("Enter the first Elevation: ");
number = Double.parseDouble(input);
firstAirport.setElevation(number);
// Get and store the coordinates for secondAirport.
input = JOptionPane.showInputDialog("Enter the second Latitude: ");
number = Double.parseDouble(input);
secondAirport.setLatitude(number);
input = JOptionPane.showInputDialog("Enter the second Longitude: ");
number = Double.parseDouble(input);
secondAirport.setLongitude(number);
input = JOptionPane.showInputDialog("Enter the second Elevation: ");
number = Double.parseDouble(input);
secondAirport.setElevation(number);
}
// The Distance method calculates the distance in nautical miles
public static void getDistance(String[] args)
{
double R = 3440;
double dist = Math.sin(firstAirport.getLatitude())
* Math.sin(secondAirport.getLatitude())
+ Math.cos(secondAirport.getLatitude())
* Math.cos(firstAirport.getLatitude())
* Math.cos(firstAirport.getLongitude()
- secondAirport.getLongitude());
dist = Math.acos(dist);
dist = dist * R;
// Display result in nautical miles.
JOptionPane.showMessageDialog(null,
"The distance in nautical miles is: %.1f\n" + dist);
System.exit(0);
}
}
和機場課....
package lab06;
public class Airport
{
public double latitude;
public double longitude;
public double elevation;
//The setLatitude method stores a value in the latitude field.
public void setLatitude(double latitude)
{
this.latitude = latitude;
}
//The setLongitude method stores a value in the longitude field.
public void setLongitude(double longitude)
{
this.longitude = longitude;
}
//The setElevation method stores a value in the elevation field.
public void setElevation (double elevation)
{
this.elevation = elevation;
}
//The getLatitude method returns an Airport object's latitude.
public double getLatitude()
{
return latitude;
}
//The getLongitude method returns an Airport object's longitude.
public double getLongitude()
{
return longitude;
}
//The getElevation method returns an Airport object's elevation.
public double getElevation()
{
return elevation;
}
}
如何從包(java)中的另一個類訪問主類中的變量?
我想您是在詢問訪問主 >> 方法<< 中聲明的局部變量。 簡單的答案是你不能。
但是您可以將變量的值作為方法或構造函數參數傳遞給另一個類。
我在這里做了大部分工作,但我想我需要在這里添加構造函數。
是的。 那將是一個好主意。
請幫忙
提示:閱讀有關如何編寫構造函數的講義/教科書/在線 Java 教程。
我閱讀問題的方式是,應該為靜態方法提供數據,並且不知道它正在計算機場之間的距離。 如果打算這樣做,則需要接收兩個 Airport 對象,而不是如所述的 4 個雙打對象。
注意:作為一般規則,避免使任何可變數據靜態可用(緩存除外)。
以下是如何將 3 個雙打傳遞給Airport
的示例:
public class Airport {
public double latitude;
public double longitude;
public double elevation;
public Airport(double latitude, double longitude, double elevation) {
this.latitude = latitude;
this.longitude = longitude;
this.elevation = elevation;
}
//if you need to access variables you add get methods like:
public double getLatitude(){
return latitude;
}
public static void main( String[] args) {
Airport ap = new Airport(30.34567, 27.6789, -140);
System.out.println("Airport latitude is "+ ap.getLatitude());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.