Domanda

I'm using the following model in a listview:

public class ListItem {
    private int _Id;
    private String Name;


    public ListItem() {
        this._Id = 0;
        this.Name = "";
    }

    public ListItem(int Id, String Name) {
        this._Id = Id;
        this.Name = Name;
    }
        public int getId() {
        return this._Id;
    }

    public void setId(int c) {
        this._Id = c;
    }

    public String getName() {
        return this.Name;
    }

    public void setName(String c) {
        this.Name = c;
    }
}

I have some extra data that will be displaying once they click on the item they want to view. Since I'm pulling this info from a database and I'm not limiting the number of items they put in the database, I was wondering if I could extend my model in another model. Something like (you can correct me if this is the wrong way to write an extend model, if extending models is not a big deal):

public class ItemDetails extends ListItem {
    private String itemType, Manufacturer, Qty, Notes;
    public ItemDetails () {
        this._Id = 0;
        this.Name = "";
        this.itemType = "";
        this.Manufacturer = "";
        this.Qty = "";
        this.Notes = "";
    }
    public Ammo(int _Id, String name, String itemType,
            String manufacturer, String qty, String notes) {
        this._Id = _Id;
        this.Name = name;
        this.itemType = itemType;
        this.Manufacturer = manufacturer;
        this.Qty = qty;
        this.Notes = notes;
    }

    // Get Variables //
    public int get_Id() {
        return this._Id;
    }

    public String getName() {
        return this.Name;
    }

    public String getItemType() {
        return itemType;
    }

    public String getManufacturer() {
        return Manufacturer;
    }

    public String getQty() {
        return Qty;
    }

    public String getNotes() {
        return Notes;
    }

    // Set Variables //
    public void set_Id(int c) {
        this._Id(c);
    }

    public void setName(int c) {
        this.Name = c;
    }

    public void setItemType(String c) {
        itemType = c;
    }

    public void setManufacturer(String c) {
        Manufacturer = c;
    }

    public void setQty(String c) {
        Qty = c;
    }

    public void setNotes(String c) {
        Notes = c;
    }

}

Would this be a bad idea? Would I be better off just having to different models or one model and only returning the data I need from the List then getting the rest of the data later? I'm trying to write clean code with this, and have as little duplicate code as possible. I am also wanting my code to be efficient and perform well , but I'm not sure if this might be a good idea.

È stato utile?

Soluzione

It looks like you're not using most of the code (variables and methods) from the class you are extending from. If you don't explicitly need to get an object of ListItem when you create an object of ItemDetails (like ListItem item = new ItemDetails();) you have actually no reason to extend that class. In that case your code is definitely cleaner, if you make it not to extend.

Altri suggerimenti

If you want to write clean code you should understand that the constructor calls the ListItem constructor and already initializes fields "_Id" and "Name" and overriding methods with no need to change them is not recommended.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top