2.25.静态变量

静态变量
尽量不要使用 Magic Number

VIP 的折扣作为一个成员变量, hin浪费啊

看例程:学习静态变量(也叫做类变量)

public class MerchandiseV2DescAppMain {
    public static void main(String[] args) {
        MerchandiseV2WithStaticVariable merchandise = new MerchandiseV2WithStaticVariable
            ("书桌", "DESK9527", 40, 999.9, 500);

        merchandise.describe();

        // >> TODO 使用import static来引入一个静态变量,就可以直接用静态变量名访问了
        //    TODO import static也可以使用通配符*来引入一个类里所有静态变量
        System.out.println(DISCOUNT_FOR_VIP);
    }
}
public class RunLittleSupperMarketAppMain {
    public static void main(String[] args) {
        // 创建一个小超市类
        LittleSuperMarket littleSuperMarket = new LittleSuperMarket(
            "有家小超市", "浦东新区世纪大道666号",
            100, 200, 200);

        System.out.println("下面请利润最高的商品自我介绍:");
        MerchandiseV2WithStaticVariable bigProfit = littleSuperMarket.getBiggestProfitMerchandise();
        bigProfit.describe();
        double cost1 = bigProfit.buy(10, true);
        System.out.println("VIP 购买10个" + bigProfit.getName() + "的花费为" + cost1);

        // >> TODO 使用别的类的静态变量的时候,需要使用完整形态:类名.静态变量名字
        MerchandiseV2WithStaticVariable.DISCOUNT_FOR_VIP = 0.5;
        bigProfit.describe();
        double cost2 = bigProfit.buy(10, true);
        System.out.println("VIP 购买10个" + bigProfit.getName() + "的花费为" + cost2);

        // >> TODO 静态变量在整个Java程序中只有一个(对比实例变量,是每个实例有一份
        //    TODO 所以静态变量一旦变化,所有使用这个静态变量的地方的值都会变
        MerchandiseV2WithStaticVariable m0 = littleSuperMarket.getMerchandiseOf(0);
        m0.describe();
        double cost3 = m0.buy(10, true);
        System.out.println("VIP 购买10个" + m0.getName() + "的花费为" + cost3);



    }
}
public class LittleSuperMarket {
    public String superMarketName;
    public String address;
    public int parkingCount;
    public double incomingSum;
    public MerchandiseV2WithStaticVariable[] merchandises;
    public int[] merchandiseSold;

    /**
     * 初始化小超市
     *
     * @param superMarketName
     * @param address
     * @param parkingCount
     * @param merchandiseCount 商品种类数
     * @param count            每种商品缺省库存
     */
    public LittleSuperMarket(String superMarketName, String address, int parkingCount,
                             int merchandiseCount, int count) {
        this.superMarketName = superMarketName;
        this.address = address;
        this.parkingCount = parkingCount;

        merchandises = new MerchandiseV2WithStaticVariable[merchandiseCount];
        for (int i = 0; i < merchandises.length; i++) {
            double purchasePrice = Math.random() * 200;
            // 创建并给商品的属性赋值
            MerchandiseV2WithStaticVariable m = new MerchandiseV2WithStaticVariable(
                "商品" + i,
                "ID" + i,
                count,
                purchasePrice * (1 + Math.random()),
                purchasePrice
            );
            // 用创建的商品,给商品数组的第i个引用赋值,all和小超市的商品数组引用指向的是同一个数组对象
            merchandises[i] = m;
        }
        merchandiseSold = new int[merchandises.length];
    }

    // 简单的访问成员变量

    public String getSuperMarketName() {
        return superMarketName;
    }

    public String getAddress() {
        return address;
    }

    public int getParkingCount() {
        return parkingCount;
    }

    public double getIncomingSum() {
        return incomingSum;
    }

    public void setSuperMarketName(String superMarketName) {
        this.superMarketName = superMarketName;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setParkingCount(int parkingCount) {
        this.parkingCount = parkingCount;
    }

    public void setIncomingSum(double incomingSum) {
        this.incomingSum = incomingSum;
    }

    public void setMerchandises(MerchandiseV2WithStaticVariable[] merchandises) {
        this.merchandises = merchandises;
    }

    public void setMerchandiseSold(int[] merchandiseSold) {
        this.merchandiseSold = merchandiseSold;
    }

    // 一些特殊的逻辑

    /**
     * 得到利润最高的商品
     *
     * @return
     */
    public MerchandiseV2WithStaticVariable getBiggestProfitMerchandise() {
        MerchandiseV2WithStaticVariable curr = null;
        for (int i = 0; i < merchandises.length; i++) {
            MerchandiseV2WithStaticVariable m = merchandises[i];
            if (curr == null || curr.calculateProfit() < m.calculateProfit()) {
                curr = m;
            }
        }
        return curr;
    }

    /**
     * 根据索引获取商品
     *
     * @param merchandiseIndex
     * @return
     */
    public MerchandiseV2WithStaticVariable getMerchandiseOf(int merchandiseIndex) {
        if (merchandiseIndex < 0 || merchandiseIndex >= merchandises.length) {
            return null;
        }
        return merchandises[merchandiseIndex];
    }

    /**
     * 赚钱
     *
     * @param toBeAdded
     */
    public void addIncomingSum(double toBeAdded) {
        this.incomingSum += toBeAdded;
    }

    /**
     * 花钱
     *
     * @param toBeSpent
     * @return
     */
    public boolean spendMoney(double toBeSpent) {
        if (toBeSpent > incomingSum) {
            return false;
        }
        incomingSum -= toBeSpent;
        return true;
    }

}
public class MerchandiseV2 {

    public String name;
    public String id;
    public int count;
    public double soldPrice;
    public double purchasePrice;


    public MerchandiseV2(String name, String id, int count, double soldPrice, double purchasePrice) {
        this.name = name;
        this.id = id;
        this.count = count;
        this.soldPrice = soldPrice;
        this.purchasePrice = purchasePrice;
        // soldPrice = 9/0;
    }
    public MerchandiseV2(String name, String id, int count, double soldPrice) {
        // double purPrice = soldPrice * 0.8;
        // this(name, id, count, soldPrice, purchasePrice);
        this(name, id, count, soldPrice, soldPrice * 0.8);
        // double purPrice = soldPrice * 0.8;
    }

    public MerchandiseV2() {
        this("无名", "000", 0, 1, 1.1);

    }

    public void describe() {
        System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
            + "。商品进价是" + purchasePrice + "。商品库存量是" + count +
            "。销售一个的毛利润是" + (soldPrice - purchasePrice));
    }

    public double calculateProfit() {
        double profit = soldPrice - purchasePrice;
//        if(profit <= 0){
//            return 0;
//        }
        return profit;
    }


    public double buy() {
        return buy(1);
    }

    public double buy(int count) {
        return buy(count, false);
    }

    double discount = 0.95;

    public double buy(int count, boolean isVIP) {
        if (this.count < count) {
            return -1;
        }
        this.count -= count;
        double totalCost = count * soldPrice;
        if (isVIP) {
            return totalCost * discount;
        } else {
            return totalCost;
        }
    }
}
public class MerchandiseV2WithStaticVariable {

    public String name;
    public String id;
    public int count;
    public double soldPrice;
    public double purchasePrice;

    // >> TODO 静态变量使用 static 修饰符
    // >> TODO 静态变量如果不赋值,Java也会给它赋以其类型的初始值
    // >> TODO 静态变量一般使用全大写字母加下划线分割。这是一个习惯用法
    // >> TODO 所有的代码都可以使用静态变量,只要根据防范控制符的规范,这个静态变量对其可见即可
    //    TODO 比如 public 的静态变量,所有的代码都可以使用它
    public static double DISCOUNT_FOR_VIP = 0.95;

    //    TODO 但是如果没有public修饰符,只能当前包的代码能使用它
    static int STATIC_VARIABLE_CURR_PACKAGE_ONLY = 100;


    public MerchandiseV2WithStaticVariable(String name, String id, int count, double soldPrice, double purchasePrice) {
        this.name = name;
        this.id = id;
        this.count = count;
        this.soldPrice = soldPrice;
        this.purchasePrice = purchasePrice;
    }

    public String getName() {
        return name;
    }

    public MerchandiseV2WithStaticVariable(String name, String id, int count, double soldPrice) {
        this(name, id, count, soldPrice, soldPrice * 0.8);
    }

    public MerchandiseV2WithStaticVariable() {
        this("无名", "000", 0, 1, 1.1);

    }

    public void describe() {
        System.out.println("商品名字叫做" + name + ",id是" + id + "。 商品售价是" + soldPrice
            + "。商品进价是" + purchasePrice + "。商品库存量是" + count +
            "。销售一个的毛利润是" + (soldPrice - purchasePrice) + "。折扣为" + DISCOUNT_FOR_VIP);
    }

    public double calculateProfit() {
        double profit = soldPrice - purchasePrice;
//        if(profit <= 0){
//            return 0;
//        }
        return profit;
    }


    public double buy() {
        return buy(1);
    }

    public double buy(int count) {
        return buy(count, false);
    }

    public double buy(int count, boolean isVIP) {
        if (this.count < count) {
            return -1;
        }
        this.count -= count;
        double totalCost = count * soldPrice;
        if (isVIP) {
            // >> TODO 使用自己的使用静态变量的时候,直接写静态变量名字。
            return totalCost * DISCOUNT_FOR_VIP;
        } else {
            return totalCost;
        }
    }
}

使用import static来引入一个静态变量,就可以直接用静态变量名访问了
import static也可以使用通配符*来引入一个类里所有静态变量

使用别的类的静态变量的时候,需要使用完整形态:类名.静态变量名字
静态变量在整个Java程序中只有一个(对比实例变量,是每个实例有一份

所以静态变量一旦变化,所有使用这个静态变量的地方的值都会变

静态变量使用 static 修饰符
静态变量如果不赋值,Java也会给它赋以其类型的初始值
静态变量一般使用全大写字母加下划线分割。这是一个习惯用法
所有的代码都可以使用静态变量,只要根据防范控制符的规范,这个静态变量对其可见即可
比如 public 的静态变量,所有的代码都可以使用它

但是如果没有public修饰符,只能当前包的代码能使用它

使用自己的使用静态变量的时候,直接写静态变量名字。

发布了57 篇原创文章 · 获赞 0 · 访问量 513

猜你喜欢

转载自blog.csdn.net/weixin_45471415/article/details/104798459
今日推荐