gpt4 book ai didi

java - 在 Java 中使用扩展类时的小问题

转载 作者:行者123 更新时间:2023-11-30 11:13:15 25 4
gpt4 key购买 nike

编辑:这完全是我的错误,可能是因为凌晨 4 点的匆忙。代码在技术上是合理的并且工作正常,但感谢评论我已经删除了一些冗余以使事情变得更聪明。

我正在处理一个处理 Java 父类(super class)/子类和继承的类项目。虽然 90% 的代码工作正常,但有一件小事似乎不想工作,我有点困惑为什么。基本上,我们正在创建一个名为 CompactDisc 的新类,其中包含产品代码、描述、价格和艺术家等信息。当用户输入适当的代码(sgtp)时,控制台会输出信息。不幸的是,尽管提供了数据,但 Artist 行始终是空白的。虽然这个项目还涉及其他类(class),但我只会发布我认为重要的类(class)。

ProductApp.java:

import java.util.Scanner;

public class ProductApp
{
public static void main(String args[])
{
// display a weclome message
System.out.println("Welcome to the Product Selector\n");

// perform 1 or more selections
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.print("Enter product code: ");
String productCode = sc.next(); // read the product code
sc.nextLine(); // discard any other data entered on the line

// get the Product object
Product p = ProductDB.getProduct(productCode);

// display the output
System.out.println();
if (p != null)
System.out.println(p.toString());
else
System.out.println("No product matches this product code.\n");

System.out.println("Product count: " + Product.getCount() + "\n");

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}

产品.java:

import java.text.NumberFormat;

public class Product
{
private String code;
private String description;
private String artist;
private double price;
protected static int count = 0;

public Product()
{
code = "";
description = "";
price = 0;

}

public void setCode(String code)
{
this.code = code;
}

public String getCode(){
return code;
}

public void setArtist(String artist)
{
this.artist = artist;
}

public String getArtist(){
return artist;
}

public void setDescription(String description)
{
this.description = description;
}

public String getDescription()
{
return description;
}

public void setPrice(double price)
{
this.price = price;
}

public double getPrice()
{
return price;
}

public String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}

@Override
public String toString()
{
return "Code: " + code + "\n" +
"Description: " + description + "\n" +
"Price: " + this.getFormattedPrice() + "\n";
}

public static int getCount()
{
return count;
}
}

光盘.java:

public class CompactDisc extends Product
{
private String artist;

public CompactDisc()
{
super();
artist = "";
count++;
}

@Override
public void setArtist(String artist)
{
this.artist = artist;
}

@Override
public String getArtist(){
return artist;
}

@Override
public String toString()
{
return super.toString() +
"Artist: " + artist + "\n";
}
}

产品数据库.java:

public class ProductDB
{
public static Product getProduct(String productCode)
{
// In a more realistic application, this code would
// get the data for the product from a file or database
// For now, this code just uses if/else statements
// to return the correct product data

Product p = null;

if (productCode.equalsIgnoreCase("java") ||
productCode.equalsIgnoreCase("jsps") ||
productCode.equalsIgnoreCase("mcb2"))
{
Book b = new Book();
if (productCode.equalsIgnoreCase("java"))
{
b.setCode(productCode);
b.setDescription("Murach's Beginning Java");
b.setPrice(49.50);
b.setAuthor("Andrea Steelman");
}
else if (productCode.equalsIgnoreCase("jsps"))
{
b.setCode(productCode);
b.setDescription("Murach's Java Servlets and JSP");
b.setPrice(49.50);
b.setAuthor("Andrea Steelman");
}
else if (productCode.equalsIgnoreCase("mcb2"))
{
b.setCode(productCode);
b.setDescription("Murach's Mainframe COBOL");
b.setPrice(59.50);
b.setAuthor("Mike Murach");
}
p = b; // set Product object equal to the Book object
}
else if (productCode.equalsIgnoreCase("txtp"))
{
Software s = new Software();
s.setCode("txtp");
s.setDescription("TextPad");
s.setPrice(27.00);
s.setVersion("4.7.3");
p = s; // set Product object equal to the Software object
}
else if (productCode.equalsIgnoreCase("sgtp"))
{
CompactDisc c = new CompactDisc();
c.setCode("sgtp");
c.setDescription("Sgt. Pepper's Lonely Hearts Club Band");
c.setPrice(15.00);
c.setArtist("The Beatles");
p = c; // set Product object equal to the CompactDisc object
}
return p;
}

和输出。如您所见,艺术家是空的:

Welcome to the Product Selector

Enter product code: sgtp

Code: sgtp
Description: Sgt. Pepper's Lonely Hearts Club Band
Price: $15.00
Artist:

Product count: 1

Continue? (y/n):

我有一种预感,我在 ProductDB.java 中做错了什么,但我可能是错的。提前致谢。

最佳答案

我无法重现您的错误。我得到结果:

Welcome to the Product Selector

Enter product code: sgtp

Code: sgtp
Description: Sgt. Pepper's Lonely Hearts Club Band
Price: 15,00 €
Artist: The Beatles

Product count: 1

Continue? (y/n):

Product 的艺术家字段被CompactDisk 隐藏。你真的应该把它改成一个合理的设计。但我不明白为什么这不起作用。

关于java - 在 Java 中使用扩展类时的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26502346/

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