简体   繁体   English

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

[英]How do I access variables in the main class from another class in the package (java)?

I'm doing my homework and I have a hard time finding the problem here.我正在做作业,我很难在这里找到问题。 The program I wrote doesn't compile.我写的程序无法编译。 Please help.请帮忙。 Homework details:作业详情:

"Create a class called Airport with the following fields: Identifier. Coordinates which consist of Latitude and Longitude (Do not create two of each !!). Latitude is positive to indicate is North of the Equator and negative when it's located in the southern hemisphere. Longitude is negative to indicate is West and positive to indicate it's East of the Greenwich median. A magnetic variation which also is indicated negative for West and positive for East. It is OK to have no magnetic variation. Elevation above sea level in feet. “创建一个名为 Airport 的类,其中包含以下字段:标识符。由纬度和经度组成的坐标(不要分别创建两个!!)。纬度为正,表示赤道以北,负则位于南半球. 经度为负表示是西,正表示它在格林威治中位数的东边。磁变化也表示西为负,东为正。没有磁变化是可以的。海拔高度以英尺为单位。

Add a static method that accepts four doubles which are two set off coordinates (double lat1, double long1, double lat2, double long2) and returns the distance in nautical miles using the formula giving in Lab 05. For example San Diego airport has the values ID: SAN, Lat: 32.7335556, Long: -117.1896667, Var: 14, Elev: 16.8' ( http://www.airnav.com/airport/SAN ) The class should have an accessor and mutator methods for each field."添加一个静态方法,该方法接受四个双精度值,即两个偏移坐标(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 ) 该类应该有每个字段的访问器和修改器方法。”

I did most of the work here but I think I need to add constructors here.我在这里做了大部分工作,但我想我需要在这里添加构造函数。 Please help.请帮忙。

Main class:主要类:

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

and the Airport class ....和机场课....

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

How do I access variables in the main class from another class in the package (java)?如何从包(java)中的另一个类访问主类中的变量?

I presume you are asking about accessing local variables declared in the main >>method<<.我想您是在询问访问主 >> 方法<< 中声明的局部变量。 The simple answer is that you CAN'T.简单的答案是你不能。

But you can pass the value of a variable to another class as a method or constructor argument.但是您可以将变量的作为方法或构造函数参数传递给另一个类。

I did most of the work here but I think I need to add constructors here.我在这里做了大部分工作,但我想我需要在这里添加构造函数。

Yes.是的。 That would be a good idea.那将是一个好主意。

Please help请帮忙

Hint: read your lecture notes / text book / an online Java tutorial on how to write a constructor.提示:阅读有关如何编写构造函数的讲义/教科书/在线 Java 教程。

The way I read the question, the static method should be provided with the data and not know that it is calculating the distances between airports.我阅读问题的方式是,应该为静态方法提供数据,并且不知道它正在计算机场之间的距离。 If the intent was to do that, it would need to take in two Airport objects rather than 4 doubles as described.如果打算这样做,则需要接收两个 Airport 对象,而不是如所述的 4 个双打对象。

Note: As a general rule, avoid making any mutable data statically available (except for caching).注意:作为一般规则,避免使任何可变数据静态可用(缓存除外)。

Here is an example of how to pass 3 doubles to Airport :以下是如何将 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM