求解继承问题、、
程序代码:import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("已买到商品清单:");
while (sca.hasNext()) {
int n = sca.nextInt();
ShopCar ps = new ShopCar(n + 1);
for (int i = 1; i <= n; i++) {
String sh = sca.next();
char c = sh.charAt(0);
if (c == 'B') {
ps.add(new Book(sca.next(), sca.nextFloat(), sca.nextInt(),
sca.next(), sca.next()));
ps.print();
}
if (c == 'C') {
ps.add(new Cloths(sca.next(), sca.nextFloat(), sca
.nextInt(), sca.next(), sca.next()));
ps.print();
}
}
}
}
}
abstract class Goods {
private String name;
private float price;
private int count;
Goods(String name, float price, int count) {
this.setName(name);
this.setPrice(price);
this.setCount(count);
}
public void setName(String n) {
this.name = n;
}
public String getName() {
return this.name;
}
public void setPrice(float p) {
this.price = p;
}
public float getPrice() {
return this.price;
}
public void setCount(int n) {
this.count = n;
}
public int getCount() {
return this.count;
}
public abstract void getInfo();
}
class Book extends Goods {
private String publish;
private String author;
Book(String name, float price, int count, String au, String pu) {
super(name, price, count);
this.setAuthor(au);
this.setPublish(pu);
}
public void setAuthor(String n) {
this.author = n;
}
public String getAuthor() {
return this.author;
}
public void setPublish(String n) {
this.publish = n;
}
public String getPublish() {
return this.publish;
}
public void getInfo() {
System.out.println("书名:" + super.getName() + ",单价:" + super.getPrice()
+ "f,数量:" + super.getCount());
}
}
class Cloths extends Goods {
private String title;
private String style;
Cloths(String name, float price, int count, String au, String pu) {
super(name, price, count);
this.setTitle(au);
this.setStyle(pu);
}
public void setTitle(String n) {
this.title = n;
}
public String getTitle() {
return this.title;
}
public void setStyle(String n) {
this.style = n;
}
public String getStyle() {
return this.style;
}
public void getInfo() {
System.out.println("品名:" + super.getName() + ",单价:" + super.getPrice()
+ "f,数量:" + super.getCount());
}
}
class ShopCar {
private Goods[] goods;
private int foot;
public ShopCar(String n, float price, int count) {
}
public ShopCar(int len) {
if (len > 0) {
this.goods = new Goods[len];
} else {
this.goods = new Goods[1];
}
}
public boolean add(Goods goods) {
if (this.foot < this.goods.length) {
this.goods[this.foot] = goods;
return true;
} else {
return false;
}
}
public void print() {
for (int i = 0; i < goods.length; i++) {
if (goods[i] != null) {
goods[i].getInfo();// 这个调用的是子类中的信息吗??
}
}
}
}







