Java-ElectronicProduct

发布时间:2024年01月12日

编写一个电子产品类ElectronicProduct的Java程序。

a定义电子产品类的3个数据成员,分别是品牌 brand,型号model,价格price,这些属性通过get和set方法进行封装。

b 定义电子产品类有参构造方法ElectronicProduct(String brand,String model,double price),在有参构造方法中加入System.out.println("constructor");

c完成无参构造方法ElectronicProduct(),要求在无参构造方法中使用this调用有参构造方法给品牌,型号和价值赋值(品牌赋值”Apple”,型号赋值”iphone”,价格赋值为6599)

d 电子产品类中定义 public String getDescription()方法可以返回其描述。描述包括品牌,型号和价格。

在Main类中先生成一个电子产品类对象,这个电子产品类的品牌,型号和价格通过键盘读入,调用getDescription方法求出描述后,输出描述。

然后再次定义一个电子产品类对象,调用无参构造方法,调用getDescription方法求出描述后,输出描述。

import java.util.Scanner;

public class ElectronicProduct {
    private String brand;
    private String model;
    private double price;

    public ElectronicProduct(String brand, String model, double price) {
        this.brand = brand;
        this.model = model;
        this.price = price;
        System.out.println("constructor");
    }

    public ElectronicProduct() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please input the brand of the electronic product: ");
        this.brand = scanner.nextLine().trim();

        System.out.print("Please input the model of the electronic product: ");
        this.model = scanner.nextLine().trim();

        System.out.print("Please input the price of the electronic product: ");
        this.price = scanner.nextDouble();

        System.out.println("constructor");

    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

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

    public double getPrice() {
        return price;
    }

    public String getDescription() {
        return "Brand: " + brand + ", Model: " + model + ", Price: " + price;
    }
}
package com.nishuixun.learn;

import java.util.Scanner;

public class useElectronicproduct {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please input the brand of the electronic product: ");
        String brand = scanner.nextLine().trim();

        System.out.print("Please input the model of the electronic product: ");
        String model = scanner.nextLine().trim();

        System.out.print("Please input the price of the electronic product: ");
        double price = scanner.nextDouble();
        scanner.nextLine(); // 读取输入缓冲区中的回车符

        ElectronicProduct electronicProduct1 = new ElectronicProduct(brand, model, price);
        System.out.println(electronicProduct1.getDescription());

        ElectronicProduct electronicProduct2 = new ElectronicProduct();
        System.out.println(electronicProduct2.getDescription());
    }
}

文章来源:https://blog.csdn.net/weixin_73617818/article/details/131118085
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。