我们如何将数据从MainActivity.java传递到android中的类?

时间:2016-09-12 08:16:11

标签: android

我想在MainActivity.java中声明一个整数变量,然后将其传递给一个类,以便将该整数变量用作该类的一个属性,我不知道该怎么做它

有可能吗?任何人都有任何想法?

这是课。我想从MainActivity.Java

设置M和N.
public class Determinant {

//Atributes
private int M = 2;
private int N = M;
private double FinalAnswers[];



//Constructors
public Determinant() {
}

//methods
public void solve(double[][] A, double[] B) {
    int N = B.length;
    for (int k = 0; k < N; k++) {
        /** find pivot row **/
        int max = k;
        for (int i = k + 1; i < N; i++)
            if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
                max = i;

        /** swap row in A matrix **/
        double[] temp = A[k];
        A[k] = A[max];
        A[max] = temp;

        /** swap corresponding values in constants matrix **/
        double t = B[k];
        B[k] = B[max];
        B[max] = t;

        /** pivot within A and B **/
        for (int i = k + 1; i < N; i++) {
            double factor = A[i][k] / A[k][k];
            B[i] -= factor * B[k];
            for (int j = k; j < N; j++)
                A[i][j] -= factor * A[k][j];
        }
    }
    /** back substitution **/
    double[] solution = new double[N];
    for (int i = N - 1; i >= 0; i--) {
        double sum = 0.0;
        for (int j = i + 1; j < N; j++)
            sum += A[i][j] * solution[j];
        solution[i] = (B[i] - sum) / A[i][i];
    }
    /** Print solution **/
    FinalAnswers = solution;
}


//setters


//getters
public double getAnswers(int i) {
    return FinalAnswers[i];
}

public int whatIsM() {
    return M;
}

public int whatIsN() {
    return N;
}
}

2 个答案:

答案 0 :(得分:0)

创建一个名为 common.class

的新类
import android.app.Application;

/**
 * Created by webcastman on 9/8/16.
 */

public class common_class extends Application
{
private static String gstring;
private static Integer gnumber;

public static  String webURL  = "http://localhost:81/";

public static String getgstring()
{
    return gstring;
}
public static void   putgstring(String str)
{
    gstring = str;
}

public static Integer getgnumber()
{
    return gnumber;
}
public static void putgnumber(Integer nor)
{
    gnumber = nor;
}
}

然后在主类中编写代码。

common_class.putgnumber(..your number..);

然后从任何其他java类

value = common_class_getgnumber()

答案 1 :(得分:0)

我找到了。

我们可以为该类添加一个setter方法,用于将值赋给属性。