Easiest way to return double from another class

2019-09-17 15:55发布

I wish to return a double from another class depending on input doubles. Any good links/examples to very simple project structure examples would be appreciated. Am I missing something in manifest. ie. activity 1

import com.example.app.utils.getRhumbLineBearingUtil;


double tbearing =             getRhumbLineBearing(alata,alona,alatb,alonb);

double recipbearing=getRhumbLineBearing(alatb, alonb, alata, alona);

Activity 2 import com.example.app.utils.getRhumbLineBearingUtil;

 double lattocbearing = getRhumbLineBearing(lat,lon,alatc,alonc);

 double bcbearing=getRhumbLineBearing(alatb, alonb, alatc, alonc);

(I do this over many activities with 15 different returns with some inputs ie gps points changing rapidly)

Class package com.example.app.utils;

public class getRhumbLineBearingUtil
{


public double getRhumbLineBearing(double $lat1, double $lon1, double $lat2, double $lon2)
  {Yada=trueRhumb  ;
double Bearing=yada;
Return (bearing);
      }
}

I am not interested in bearingTo which is the initial bearing hearing.

2条回答
神经病院院长
2楼-- · 2019-09-17 16:18

You should first learn a bit of Java programming and then move-up to android. You cannot do that that way. Try this:

Class GetRhumbLineBearingUtil {    

    public static double getRhumbLineBearing(double lat1, double lon1, double lat2, double lon2) {
        return lat1 + lon1;
    }
}

And then in another class, after import and whatever you wanna use that method for, just call it like this:

double lattocbearing = GetRhumbLineBearingUtil.getRhumbLineBearing(alatb, alonb, alata, alona);

Some notes here: static methods can be accessed via ClassName.methodName

This was what i understood from the question. Now if you want to exchange data between activities, then that is another story, you can use intents.

Some notes tho:

  • Class names start with upperCase
  • Classes, methods and method arguments should have a proper name/meaning to make it easier to understand what is being done.
  • Variable names start lowerCase
  • There is no $variable in Java, everything is passed by value
  • If you want to return some value stored in a variable in class GetRhumbLineBearingUtil you need to make that variables static as well.
查看更多
可以哭但决不认输i
3楼-- · 2019-09-17 16:24

If what you mean is how to exchange data between two activities and you would like to pass some double values, you should use the method putExtra(String name, double value) or putExtra(String name, double[] value) and get them from the second activity onCreate(Bundle savedInstanceState) method.

Otherwise use a static method that contains your method code.

public class Utils {

public static double getRhumbLineBearing(double latA, double lonA, double latB, double lonB) {
    [your code]
}
}
查看更多
登录 后发表回答