如何从包(java)中的另一个类访问主类中的变量?
我正在做作业,我很难找到问题所在。我写的程序没有编译。请帮忙。作业详情:
“使用以下字段创建一个名为Airport的类:标识符。由纬度和经度组成的坐标(不要分别创建两个!!)。纬度为正表示位于赤道以北,位于南半球则为负。经度为负表示为西,为正表示为格林威治中心以东。一种磁场变化,也表明西部为负,东部为正。没有磁场变化是可以的。海拔高度(英尺)
添加一个静态方法,该方法接受四个双精度,即两个偏移坐标(双lat1、双long1、双lat2、双long2),并使用实验室05中给出的公式返回以海里为单位的距离。例如,圣地亚哥机场的值ID:San,Lat:32.7335556,Long:-117.1896667,Var:14,Elev:16.8'(http://www.airnav.com/airport/SAN)。该类应该为每个字段都有一个访问器和mutator方法。"
我在这里做了大部分工作,但我想我需要在这里添加构造函数。请帮忙
主要类别:
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;
}
}
# 1 楼答案
我假定您询问的是访问main中声明的局部变量>&燃气轮机;方法<&书信电报;。简单的答案是你不能
但是您可以将变量的值作为方法或构造函数参数传递给另一个类
对。那是个好主意
提示:阅读您的课堂讲稿/教科书/关于如何编写构造函数的在线Java教程
# 2 楼答案
按照我理解问题的方式,静态方法应该提供数据,而不知道它是在计算机场之间的距离。如果目的是这样做,它将需要接收两个机场对象,而不是所述的4个双倍对象
注意:作为一般规则,避免使任何可变数据静态可用(缓存除外)
# 3 楼答案
下面是一个如何将3个double传递给
Airport
的示例: