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

时间:2016-10-15 02:41:57

标签: java class variables methods arguments

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

“使用以下字段创建一个名为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)该类应具有每个字段的访问器和mutator方法。“

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

主要课程:

window.onload = function() {

  // append next `track` element to `video` in 1 seconds
  setTimeout(function() {
    // pass `src`, `label` to set at `track` element
    addTrack("/path/to/resource2", "track 1") 
  }, 1000);

  function foo(track, src) {  
    track.addEventListener("load", function() {
      console.log("this loads");
    });
    video.appendChild(track);
    track.src = src;
  };

  function addTrack(src, tracklabel) {
    var track = document.createElement("track");
    track.kind = "captions";
    track.srclang = "en";
    track.label = tracklabel;
    // set `default` attribute at `track` element
    track.setAttribute("default", "default");
    // pass `track` element, `src` to set at `track` to `foo`
    foo(track, src);
  }

  addTrack("/path/to/resource1", "track 1");

}

和机场班......

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

3 个答案:

答案 0 :(得分:0)

  

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

我假设您要求访问在主>>方法<<中声明的局部变量。简单的答案就是你不能。

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

  

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

是。那将是一个好主意。

  

请帮忙

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

以下是如何将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());
    }

}
相关问题