为什么赢得变量basica(Crank)和basicb(Pedal)会出现在我的arraylist显示中以及其他值?还有另一个名为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));
}
}
AssembledPart类
package main;
import java.util.*;
public class AssembledPart extends Part {
private String basica;
private String basicb;
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 String toAssembledString(String id, String name, int stocklevel, double unitprice) {
return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb;
}
public static String toAssembledString(Collection<AssembledPart> aparts){
String s = "";
for (AssembledPart apart: aparts){
s += apart + "\n";
}
return s;
}
}
Part Class
package main;
import java.util.*;
public class Part {
private String id;
private String name;
private 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;
}
}
答案 0 :(得分:3)
因为你没有调用assembledString()方法,所以你正在调用toString()方法,这是(我期望)在Part类中实现的。
取而代之的是:
for (AssembledPart apart: aparts){
s += apart.toAssembledString() + "\n";
这样做:
public String toAssembledString(String id, String name, int stocklevel, double unitprice) {
return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb;
}
然后,您应该避免实例方法中的参数:
public String toAssembledString() {
return super.toString() + " | " + basica + " | " + basicb;
}
按如下方式进行:
{{1}}
感谢@Parker_Halo的评论
答案 1 :(得分:1)
这是因为它调用Part.toString()而且部分没有变量basica和basicb。在打印之前,您必须先调用toAssembledString()
而不是简单的部件。
答案 2 :(得分:0)
因为您要在'toAssembledString'方法中为字符串指定空值;
String s = "";
无论你做什么,它总会返回1组合,因为你正在做s="";
将其声明为类变量,并将其附加到较旧的变量。
用户StringBuilder
,此处更好。
此外,如果需要清除字符串的数据以便可以将该静态字符串重新用于其他对象,则创建一个将s="";
执行的方法,否则它将在旧的数据中附加数据之一。
答案 3 :(得分:0)
如果您不想更改 <service android:name="com.abc.def.MyCustomService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
'
toString()
而不是编写自己的重载toAssembledString(String id, String name, int stocklevel, double unitprice)
toAssembledString(Collection<AssembledPart> aparts)
答案 4 :(得分:0)
好像你的零件类看起来像这样
public class Part {
String a, b;
int c;
double d;
public Part(String a, String b, int c, double d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public String toString() {
StringJoiner j = new StringJoiner(" | ");
j.add(a);
j.add(b);
j.add(Integer.toString(c));
j.add(Double.toString(d));
return j.toString();
}
}
通过调用s += apart + "\n";
,您可以自动调用类的toString
方法,该方法看起来像Part
类所覆盖。要获得AssembledPart
课程所需的输出,您应该覆盖toString
方法并添加此类附加信息。
@Override
public String toString() {
StringJoiner j = new StringJoiner(" | ");
j.add(super.toString());
j.add(basica);
j.add(basicb);
return j.toString();
}