簡體   English   中英

如何要求子類具有可以使用超類方法檢索的已定義變量?

[英]How can I require a subclass to have a defined variable that can be retrieved with a superclass method?

我正在嘗試為其他人創建一個簡單的編程體驗來擴展我正在開發的應用程序的類結構。 我目前正在做實習生,但我工作的公司希望最終的應用程序能夠讓他們輕松添加小的更新(簡單的添加,不需要完整的重新雇佣。這不是他們不想要再雇用我。)

我有一個抽象類:

abstract class ProductEntry {
    protected String title;
    protected String subtitle;
    protected int[] imageArray;
    protected String downloadLink;
    protected String description;

    public ProductEntry() {

    }

    /*
      ~Getters and Setters~
    */
}

這是一個擴展它的示例類:

public class GenericEntry extends ProductEntry {
    private String newTitle = "This Title";
    private String newSubtitle = "That Subtitle";
    private int[] newImageArray = new int[]{R.drawable.picture1, R.drawable.picture2};
    private String newDownloadLink = "www.google.com";
    private String newDescription = "This is where my description would go, if I had one!";

    public GenericEntry() {

    }

    @Override
    public void setTitle(String title) {
        super.setTitle(newTitle);
    }

    @Override
    public void setDownloadLink(String downloadLink) {
        super.setDownloadLink(newDownloadLink);
    }

    @Override
    public void setImageArray(int[] imageArray) {
        super.setImageArray(newImageArray);
    }

    @Override
    public void setSubtitle(String subtitle) {
        super.setSubtitle(newSubtitle);
    }

    @Override
    public void setDescription(String description) {
        super.setDescription(newDescription);
    }
}

我這樣做是因為我想編寫我的應用程序將所有子類放入一個數組中:

ProductEntry allEntries = new ProductEntry[numOfEntries]

然后,我將使用所有條目根據列表中的選擇填充通用布局。

TextView title = (TextView) findViewById(R.id.txtTitle);
title.setText(allEntries[position].getTitle());

// Repeat for subtitle and description's TextView, 
// populate ImageViews with pictures from the imageArray, 
// and update the URL link of a button.

我選擇這樣做的原因是,因為將來可能希望更新應用程序的人不會是程序員並且不是非常精通技術,我可以向他們展示GenericEntry,向他們展示如何復制類,更改類名,然后告訴他們替換變量。

這樣,他們不需要復制布局並被迫創建一個全新的活動並將其分配給ListView。 我認為通常我會為每個條目創建一個“獨特”(個人)布局,但由於他們希望稍后用新條目擴展它,我正在嘗試創建一個后端,所以他們只需要填寫 - 空白。

這也應該允許我輕松地在應用程序中填充有關其產品的信息。

問題是,正如我可以肯定的那樣,我正在嘗試將變量傳遞給子類,而這些子類在Java中是無法做到的,因為這不是繼承的工作方式。 (當我開始這樣做時,我有一段時間沒有完成Java,所以我沒有立刻意識到這個錯誤。)

我的問題是如何在保留其目的的同時改進這個系統?

我應該試圖放棄抽象類嗎? 我是android dev的新手,所以我不確定是否有更好的方法將這種信息傳遞給一個活動。

如果要求子類提供信息,可以使用構造函數(在抽象類中):

abstract class ProductEntry {
    protected String title;
    protected String subtitle;
    protected int[] imageArray;
    protected String downloadLink;
    protected String description;

    ProductEntry(String title, String subtitle, int[] imageArray, String downloadLink, String description) {
        this.title = title;
        this.subtitle = subtitle;
        this.imageArray = imageArray;
        this.downloadLink = downloadLink;
        this.description = description;
    }

    /*
     * ~Getters and Setters~
     */
}

這會強制子類提供信息,如下所示:

public class GenericEntry extends ProductEntry {
    private static String newTitle = "This Title";
    private static String newSubtitle = "That Subtitle";
    private static int[] newImageArray = new int[]{R.drawable.picture1, R.drawable.picture2};
    private static String newDownloadLink = "www.google.com";
    private static String newDescription = "This is where my description would go, if I had one!";

    public GenericEntry() {
        super(newTitle, newSubtitle, newImageArray, newDownloadLink,newDescription);
    }
}

暫無
暫無

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

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