gpt4 book ai didi

java - 继承的方法不起作用

转载 作者:行者123 更新时间:2023-12-01 11:03:35 25 4
gpt4 key购买 nike

我有一种方法应该为制造商补充库存零件。我尝试过使用 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));
}

}

组装零件类

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;
}
}

零件类

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;
}


}

最佳答案

替换这一行:

         aparts.replenish(sc.nextInt());

具有以下内容

         apart.replenish(sc.nextInt());

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

关于java - 继承的方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33147199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com