第27天
package 两个对象数组; public class Goods { private String id; private String name; private double price; private int count; public Goods(String id, String name, double price, int count) { super(); this.id = id; this.name = name; this.price = price; this.count = count; } public Goods() { super(); // TODO Auto-generated constructor stub } public String getId() { return id; } public void setId(String id) { this.id = id; } 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 int getCount() { return count; } public void setCount(int count) { this.count = count; } }
package 两个对象数组; public class GoodsTest { public static void main(String[] args) { Goods[] arr = new Goods[3]; Goods g1 = new Goods("001", "红米K50 Pro", 2567.00, 30); Goods g2 = new Goods("002", "红米K50", 2337.00, 31); Goods g3 = new Goods("003", "iPhone 15 ProMax 暗夜紫", 16032.00, 2); arr[0] = g1; arr[1] = g2; arr[2] = g3; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i].getId() + arr[i].getName() + arr[i].getPrice() + arr[i].getCount()); } } }
001红米K50 Pro2567.030 002红米K502337.031 003iPhone 15 ProMax 暗夜紫16032.02
package 汽车数组; public class cars { private String brand; private double price; private String color; public cars() { super(); // TODO Auto-generated constructor stub } public cars(String brand, double price, String color) { super(); this.brand = brand; this.price = price; this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } 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; } }
package 汽车数组; import java.util.Scanner; public class carsTest { public static void main(String[] args) { cars[] arr = new cars[3]; Scanner sc = new Scanner(System.in); for (int i = 0; i < arr.length; i++) { cars c = new cars(); String brand = sc.next(); c.setBrand(brand); double price = sc.nextDouble(); c.setPrice(price); String color = sc.next(); c.setColor(color); arr[i] = c; } for (int i = 0; i < arr.length; i++) { System.out.println( "汽车品牌为:" + arr[i].getBrand() + ",汽车颜色为:" + arr[i].getColor() + ",汽车价格为:" + arr[i].getPrice()); } } }
奥迪 230000 紫色 野马 3434556 黑色 非拉瑞 233432 红色 汽车品牌为:奥迪,汽车颜色为:紫色,汽车价格为:230000.0 汽车品牌为:野马,汽车颜色为:黑色,汽车价格为:3434556.0 汽车品牌为:非拉瑞,汽车颜色为:红色,汽车价格为:233432.0