Java面向对象编程:商品信息管理系统
// 定义Product类
class Product {
private String name; // 商品名称
private int id; // 商品编号
private double price; // 商品价格
private static int nextId = 1000; // 下一个商品编号
// 构造方法
public Product(String name, double price) {
this.name = name;
this.price = price;
this.id = nextId++;
}
// 输出商品信息
public void display() {
System.out.println('商品名称:' + name);
System.out.println('商品编号:' + id);
System.out.println('商品价格:' + price + '元');
}
}
// 主类
public class Prog1 {
public static void main(String[] args) {
// 创建商品对象并输出商品信息
Product product1 = new Product('手机', 3999.0);
product1.display();
Product product2 = new Product('电脑', 7999.0);
product2.display();
Product product3 = new Product('平板', 2999.0);
product3.display();
}
}
原文地址: https://www.cveoy.top/t/topic/pGRF 著作权归作者所有。请勿转载和采集!