【问题标题】:How do I access variables in the main class from another class in the package (java)?如何从包中的另一个类(java)访问主类中的变量?
【发布时间】:2017-02-24 13:10:33
【问题描述】:

我正在做作业,我很难在这里找到问题所在。我写的程序无法编译。请帮忙。作业详情:

“使用以下字段创建一个名为 Airport 的类:标识符。由纬度和经度组成的坐标(不要每个创建两个!!)。纬度为正表示位于赤道以北,为负表示位于赤道以北南半球。经度为负表示在西,经度为正表示在格林威治中线以东。磁变化也表示西为负,东为正。没有磁变化是可以的。海拔高度以英尺为单位。

添加一个静态方法,该方法接受四个双精度值,它们是两个偏移坐标(双纬度 1、双倍长 1、双纬度 2、双倍长 2),并使用实验室 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);
    }
}

还有 Airport 类 ....

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;
    }
}

【问题讨论】:

  • 我将问题解读为“它无法编译,我不会费心阅读错误消息或将其提供给您。我希望您为我完成所有工作”。跨度>
  • 为什么不能编译?当您尝试编译它时,具体会发生什么?有什么东西停止它的编译吗?那是什么东西?
  • 不要贴这么多代码。发布minimal reproducible example:发布最低限度的内容以显示您的问题。
  • 主类getDistance方法出现错误,提示:Cannot find variable firstAirport, secondAirport。创建两个 Airport 对象时,如何访问存储在 Airport 类中的变量?

标签: java class variables methods arguments


【解决方案1】:

这是一个如何将 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());
    }

}

【讨论】:

【解决方案2】:

我阅读问题的方式,静态方法应该提供数据并且不知道它正在计算机场之间的距离。如果打算这样做,则需要接收两个 Airport 对象,而不是描述的 4 个 double。

注意:作为一般规则,请避免使任何可变数据静态可用(缓存除外)。

【讨论】:

【解决方案3】:

如何从包中的另一个类(java)访问主类中的变量?

我想您是在询问有关访问在主 >>method

但是您可以将变量的作为方法或构造函数参数传递给另一个类。

我在这里完成了大部分工作,但我认为我需要在这里添加构造函数。

是的。这是个好主意。

请帮忙

提示:阅读您的讲义/教科书/有关如何编写构造函数的在线 Java 教程。

【讨论】:

  • 感谢斯蒂芬的帮助。
猜你喜欢
  • 2013-10-25
  • 2021-08-09
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2020-11-24
  • 2014-07-06
相关资源
最近更新 更多