定义一个实体类Product类
时间: 2024-06-21 20:03:27 浏览: 6
在面向对象编程中,创建一个实体类(如Product)通常是为了表示一个现实世界中的抽象概念,例如商品。下面是一个基本的Java示例,展示如何定义一个名为Product的类:
```java
// 定义一个名为Product的类
public class Product {
// 数据成员(属性)
private String productId;
private String productName;
private double price;
private int quantity;
// 构造函数(用于初始化对象)
public Product(String productId, String productName, double price, int quantity) {
this.productId = productId;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
// 成员方法(操作)
// get方法获取属性值
public String getProductId() { return productId; }
public String getProductName() { return productName; }
public double getPrice() { return price; }
public int getQuantity() { return quantity; }
// set方法设置属性值
public void setProductId(String productId) { this.productId = productId; }
public void setProductName(String productName) { this.productName = productName; }
public void setPrice(double price) { this.price = price; }
public void setQuantity(int quantity) { this.quantity = quantity; }
// 其他可能的方法,如描述、折扣计算等
public String getDescription() {
// 这里是产品描述的逻辑
}
}
```