继承的方法不起作用

时间:2015-10-15 11:22:10

标签: java inheritance methods

我有一种方法可以为制造商补充库存零件。我尝试过使用super.Method()但它不起作用。如果需要,还有一个名为Part的类。

主类

package main;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestAssembledPart {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    List<AssembledPart> aparts = new ArrayList<AssembledPart>();
    aparts.add(new AssembledPart("a200", "Crank & Pedal", 10, 3.5, "Crank", "Pedal"));

     System.out.println("part before stock level change - start");
     System.out.println(AssembledPart.toAssembledString(aparts));

     for (AssembledPart apart: aparts){
         if(apart!=null){
             System.out.println("Please enter replenish quantity");
             aparts.replenish(sc.nextInt());
         }
     }
     System.out.println("part before stock level change - end");
     System.out.println(AssembledPart.toAssembledString(aparts));
}

}

AssembledPart Class

package main;
import java.util.*;

public class AssembledPart extends Part {

private String basica;
private String basicb;
private int assembledstocklevel;

public AssembledPart(String id, String name, int stocklevel, double unitprice, 
                    String basica, String basicb) {

    super(id, name, stocklevel, unitprice);

    this.basica = basica;
    this.basicb = basicb;
}

public void replenish(int qty){
    super.replenish(qty);
    //super.stocklevel = super.stocklevel + qty;
}

public String toAssembledString() {
    return super.toString() + " | " + basica + " | " + basicb;
}   
public static String toAssembledString(Collection<AssembledPart> aparts){
    String s = "";
    for (AssembledPart apart: aparts){
        s += apart.toAssembledString() + "\n";
    }
    return s;
}
}

PartClass

package main;
import java.util.*;

public class Part {

private String id;
private String name;
protected int stocklevel;
private double unitprice;

private int qty = 6000;

public Part(String id, String name, int stocklevel, double unitprice){
    this.id = id;
    this.name = name;
    this.stocklevel = stocklevel;
    this.unitprice = unitprice;
}


String partsAvailable()
{
    //String newLine = System.getProperty("line.separator");
    return (id + "\t" + name + "\t    " + stocklevel + "\t\t   " + unitprice);
}


public String getID() {
    return id;
}

public void setID(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public int getStockLevel(){
    return stocklevel - qty;
}

public void setStockLevel(int stocklevel){
    this.stocklevel = stocklevel;
}

public double getUnitPrice(){
    return unitprice;
}

public void setUnitPrice(double unitprice){
    this.unitprice = unitprice;
}

public void replenish(int qty){
    this.stocklevel = stocklevel + qty;
}

public double supply(int qty){
    return unitprice * qty;
}

public String toString() {
    return id + " | " + name + " | " + stocklevel + " | " + unitprice;
}


public static String toString(Collection<Part> parts){
    String s = "";
    for (Part part: parts){
        s += part + "\n";
    }
    return s;
}


}

1 个答案:

答案 0 :(得分:1)

替换此行:

         aparts.replenish(sc.nextInt());

使用以下

         apart.replenish(sc.nextInt());

您实际上是在包含对象而不是对象本身的集合上调用方法。