第30天

一、存储手机对象

package Phone;

public class PhoneTest {
    public static void main(String[] args) {
        Phone[] arr = new Phone[3];
        Phone p1 = new Phone("小米", 2567, "幽芒");
        Phone p2 = new Phone("华为", 23451, "幻境");
        Phone p3 = new Phone("IQOO", 3675, "黑曜");
        arr[0] = p1;
        arr[1] = p2;
        arr[2] = p3;
        int  sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i].getPrice();
        }
        System.out.println("手机平均价格为:" + (1.0)*sum / 3);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("第" + i + "个手机名字为" + arr[i].getName() + ",它的颜色为:" + arr[i].getColor());
        }
    }
}
package Phone;

public class Phone {
    private String name;
    private double price;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Phone() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Phone(String name, double price, String color) {
        super();
        this.name = name;
        this.price = price;
        this.color = color;
    }
}
手机平均价格为:9897.666666666666
第0个手机名字为小米,它的颜色为:幽芒
第1个手机名字为华为,它的颜色为:幻境
第2个手机名字为IQOO,它的颜色为:黑曜

 

posted @ 2023-07-31 17:53  七安。  阅读(3)  评论(0编辑  收藏  举报