package chapter5;
abstract class Goods {
private double unitPrice;
private int account;
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getAccount() {
return account;
}
public void setAccount(int account) {
this.account = account;
}
public Goods() {}
public Goods(double unitPrice, int account) {
this.unitPrice = unitPrice;
this.account = account;
}
public double totalPrice(){
return unitPrice*account;
}
}
//VIP价格接口
interface VipPrice {
double DISCOUNT=0.8;
double reducedPrice();
}
//服装类
class Clothing extends Goods implements VipPrice {
private String style;
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public double reducedPrice() {
return VipPrice.DISCOUNT*totalPrice();
}
public Clothing(){}
public Clothing(String style,double unitPrice, int account) {
super(unitPrice, account);
this.style=style;
}
public void showInfo(){
System.out.println("单价:"+getUnitPrice());
System.out.println("数量:"+getAccount());
System.out.println("样式:"+style);
System.out.println("VIP价格:"+reducedPrice());
}
}
//测试类
public class Test {
public static void main(String[] args) {
Clothing c=new Clothing("女装",300,2);
c.showInfo();
}
}
为了让JAVA代码看起来美观,可读性强,应该怎么调整代码呢?
具体问题就是我的代码中有好几个类,每个类中属性偏多,我应该把一个类中所有的方法写在一起吗?还有设置器和访问器,是声明一个属性就紧跟在后面写还是把所有的属性声明完之后,一起写设置器和访问器,怎么安排能让结构看起来清晰呢?一个类中一般是按什么顺序写属性、构造器、方法呢?就是如何安排顺序的问题。