簡體   English   中英

同一類中的兩個構造函數具有不同的參數

[英]Two constructors at in the same class with different parameters

在第1課中,我創建了一個實例:

  db = new AdapterDB (cal, rcarbohydrate, rfat, rprotein, getApplicationContext());

在第2課中,我進行了一個實例:

db = new AdapterDB (Bits, Truncation, Crossover, Mutation, Chromosomes, Generation, getApplicationContext());

在AdapterDB類中,我構造了構造函數:

private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public AdapterDB(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper (context);
    }

    double Truncation;
    double Crossover;
    double Mutation;
    int Generation;

    private Context context;

    int indexOfChromosomes ;
    int indexOfGens;
    int gensNumber;
    int chromosomesNumber;

    String [][] population;
    double [] fitnesstotal;

    public AdapterDB(int Bits, double Truncation, double Crossover, double Mutation, int Chromosomes, int Generation, Context ctx)
    {
        this.indexOfGens = Bits;
        this.Truncation = Truncation;
        this.Crossover = Crossover;
        this.Mutation = Mutation;
        this.indexOfChromosomes = Chromosomes;
        this.Generation = Generation;
        this.context = ctx;
        DBHelper = new DatabaseHelper (context);

        population = new String[indexOfChromosomes][indexOfGens];
        fitnesstotal = new double [indexOfChromosomes];
    }

double cal;
    double rcarbohydrate;
    double rfat;
    double rprotein;

    public AdapterDB(double cal, double rcarbohydrate, double rfat, double rprotein, Context ctx)
    {
        this.cal = cal;
        this.rcarbohydrate = rcarbohydrate;
        this.rfat = rfat;
        this.rprotein = rprotein;
        this.context = ctx;
        DBHelper = new DatabaseHelper (context);
    }

在構造函數中該值仍然存在,每個變量上都有一個值,但是當我想在AdapterDB類的某些函數中使用它時,cal,rcarbohydrate,rfat和rprotein為零(0.0),為什么會發生這種情況? 以及如何解決? 謝謝。

任何新手程序員都應記住的一個好的經驗法則是,從一開始就擁有將帶來好處:

什么是一類? (來自Java教程)

您的AdapterDB類顯然試圖同時成為兩個不同的事物。 我猜想您的調用使用第一個構造函數創建了一個實例,但是隨后期望填充其他值(由第二個構造函數設置)。

您可能應該做的是使AdapterDB成為抽象類:

public abstract class AdapterDB {

    private Context context;
    private DBHelper dbHelper;

    public AdapterDB(Context context) {
        this.context = context;
        this.dbHelper = new DBHelper(context);
    }

    public Context getContext() {
        return context;
    }

    public DBHelper getDBHelper() {
        return dbHelper;
    }
}

然后,您可以為需要表示的各種數據類型創建單獨的類:

public class GeneticAdapter extends AdapterDB {

    int chromosomesNumber;
    // ... And others

    public GeneticAdapter(Context context, int numChromosomes, ...) {
        super(context);
        // Set your instance variables
    }
}

一個適合健身嗎?

public class FitnessAdapter extends AdapterDB {
    double cal;
    // ... And others

    public FitnessAdapter(Context context, double cal, ...) {
        super(context);
        // Set your instance variables
    }
}

我很抱歉,如果這不是您想要的,您的問題有點難以理解。 祝您好運,如果您有后續行動,請在下方評論(或編輯答案)。

聽起來您正在創建兩個單獨的實例,並嘗試通過給它們指定相同的名稱來合並它們。 這不是Java的工作方式。

第1類使用給定的參數制作一個adapterdb(稱為db)。

類2使用給定的參數制作另一個adapterdb。

如果DB是兩個類都可以訪問的某種全局變量,則引用DB指向NEW對象,而第一個對象是垃圾。 對構造函數的第二次調用不會將兩者合並在一起。

構造函數似乎屬於兩個不同的類:通常,構造函數會將所有實例變量設置為至少某個默認值。

如果您確實想“合並”這兩個,我將使用默認構造函數並將您擁有的兩個構造轉換為大型setter方法,如下所示:

public AdapterDB(Context context) {
    this.context = context;
    this.dbHelper = new DBHelper(context);
}

public setFitness(double cal, double rcarbohydrate, double rfat, double rprotein, Context ctx) {
    ...
}

public setGenetics(int Bits, double Truncation, double Crossover, double Mutation, int Chromosomes, int Generation, Context ctx) {
    ....
}

但是,由於參數是如此不同,因此我將對按原樣構造類保持警惕,並且我將看看craig提出的解決方案是否不是更合適的選擇。

您可以使用this關鍵字調用另一個構造函數。

class Animal {
    int weight;
    String name;

    Animal(int w) {
        weight = w;
    }

    Animal(String n) {
        this(10);  // set the weight to 10
        name = n;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM