• Java对象数组练习


    • 定义数组存储三个商品对象,商品的属性:id,名字,价格,库存,创建三个商品对象,并把商品对象存入到数组中
    1. public class Goods {
    2. private String id;
    3. private String name;
    4. private double price;
    5. private int count;
    6. public Goods(){
    7. }
    8. public Goods(String id, String name, double price, int count) {
    9. this.id = id;
    10. this.name = name;
    11. this.price = price;
    12. this.count = count;
    13. }
    14. public String getId() {
    15. return id;
    16. }
    17. public void setId(String id) {
    18. this.id = id;
    19. }
    20. public String getName() {
    21. return name;
    22. }
    23. public void setName(String name) {
    24. this.name = name;
    25. }
    26. public double getPrice() {
    27. return price;
    28. }
    29. public void setPrice(double price) {
    30. this.price = price;
    31. }
    32. public int getCount() {
    33. return count;
    34. }
    35. public void setCount(int count) {
    36. this.count = count;
    37. }
    38. }
    1. public class GoodsTest {
    2. public static void main(String[] args) {
    3. //创建一个数组,动态初始化
    4. Goods[] arr = new Goods[3];
    5. //创建三个商品对象
    6. Goods g1 = new Goods("001","华为mate60",5999.0,100);
    7. Goods g2 = new Goods("002","保温杯",227.0,200);
    8. Goods g3 = new Goods("003","宁夏枸杞",12.7,70);
    9. //把商品添加到数组
    10. arr[0] = g1;
    11. arr[1] = g2;
    12. arr[2] = g3;
    13. for (int i = 0; i < arr.length; i++) {
    14. Goods goods = arr[i];
    15. System.out.println(goods.getId() + ", " + goods.getName() + ", " + goods.getPrice() + ", " + goods.getCount());
    16. }
    17. }
    18. }

    定义数组存储3部汽车对象,汽车的属性:品牌,价格,颜色。创建三个汽车对象,数据通过键盘录入而来,并把数据存入到数组中。

    • 键盘录入
      • Java里面有一个类叫Scanner,可以接收键盘输入的数字
      • 第一步:导包,找Scanner这个类
        • import java.util.Scanner;
        • 导包必须在类定义的上边(public class上面)
      • 第二步:创建对象,开始使用Scanner这个类
        • Scanner sc = new Scanner(System.in);
        • sc为变量名可以改变,其他不能改
      • 第三步:接收数据
        • int i = sc.nextInt();
        • i为变量名可以改变,其他不能改
      • 注意:这种方式只能接收整数,输入字符小数会报错
      • 第一套体系(遇到空格、制表符、回车就停止接收,后面的数据不接收)
        • nextInt(); 接收整数
        • nextDouble(); 接收小数
        • next(); 接收字符串
      • 第二套体系(可以接收空格、制表符,遇到回车才停止接收)
        • nextLine(); 接收字符串
      • 两套体系不能混用,容易出错
    1. public class Car {
    2. private String brand;
    3. private int price;
    4. private String color;
    5. public Car() {
    6. }
    7. public Car(String brand, int price, String color) {
    8. this.brand = brand;
    9. this.price = price;
    10. this.color = color;
    11. }
    12. public String getBrand() {
    13. return brand;
    14. }
    15. public void setBrand(String brand) {
    16. this.brand = brand;
    17. }
    18. public int getPrice() {
    19. return price;
    20. }
    21. public void setPrice(int price) {
    22. this.price = price;
    23. }
    24. public String getColor() {
    25. return color;
    26. }
    27. public void setColor(String color) {
    28. this.color = color;
    29. }
    30. }

     

    1. import java.util.Scanner;
    2. public class CarText {
    3. public static void main(String[] args) {
    4. //创建一个数组存储汽车对象
    5. Car[] arr = new Car[3];
    6. //创建汽车对象,数据来自键盘录入
    7. Scanner sc = new Scanner(System.in);
    8. for (int i = 0; i < arr.length; i++) {
    9. //创建汽车的对象
    10. Car c = new Car();
    11. //录入品牌
    12. System.out.println("请输入汽车的品牌");
    13. String brand = sc.next();
    14. c.setBrand(brand);
    15. //录入价格
    16. System.out.println("请输入汽车的价格");
    17. int price = sc.nextInt();
    18. c.setPrice(price);
    19. System.out.println("请输入汽车的颜色");
    20. String color = sc.next();
    21. c.setColor(color);
    22. //把汽车对象添加到数组中
    23. arr[i] = c;
    24. }
    25. //遍历数组
    26. for (int i = 0; i < arr.length; i++) {
    27. Car car = arr[i];
    28. System.out.println(car.getBrand() + ", " + car.getPrice() + ", " + car.getColor());
    29. }
    30. }
    31. }

  • 相关阅读:
    【算法题】2906. 构造乘积矩阵
    CUDA + Visual Studio 环境搭建
    element-ui :封装el-input 遇到的问题
    javaWeb过滤器Filter(二)
    Unable to connect to the server: x509: certificate is valid for问题解决
    C++算法 —— 动态规划(8)01背包问题
    内存中为什么分堆和栈,能否只用一种模型呢?为什么每个线程都有单独的栈
    中小银行传统数据仓库向大数据平台迁移探索
    MyBatis框架 注解的形式开发
    软件设计模式系列之三———工厂方法模式
  • 原文地址:https://blog.csdn.net/unravel_sky/article/details/133901666