求大神帮写一下这两道题目的JAVA代码。在线等。必须是代码。

如图所示,实训任务的任务1和任务2.
2025-05-11 03:56:10
推荐回答(1个)
回答(1):



abstract class Goods
{
protected double unitPrice;

protected int account;

public double totalPrice()
{
return unitPrice * account;
}
}

class Apple extends Goods
{
private String name;

public Apple(String name, double unitPrice, int account)
{
this.name = name;
this.unitPrice = unitPrice;
this.account = account;
}

public void show()
{
System.out.printf("%s,%.2f元/斤,%d斤,共%.2f元", name, unitPrice, account, totalPrice());
}
}

interface VipPrice
{
double DISCOUNT = 0.8;

double reducedPrice();
}

class Clothing extends Goods implements VipPrice
{
private String style;

public Clothing(String style, double unitPrice, int account)
{
this.style = style;
this.unitPrice = unitPrice;
this.account = account;
}

@Override
public double reducedPrice()
{
return this.unitPrice *= DISCOUNT;
}

public void show()
{
System.out.printf("单价:%.2f 元,数量:%d 件,样式:%s,VIP价格信息:%.2f 元", unitPrice, account, style, reducedPrice());
}
}

public class Main
{
public static void main(String[] args)
{
Apple apple = new Apple("红富士", 10, 2);
apple.show();
System.out.println();
Clothing clothing = new Clothing("女装", 300, 2);
clothing.show();
}
}