[英]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.