java 定义一个水果类 Fruit,包含了三个变量,分别为name (水果名)、area(产地)和price (单价),定义setXxx()方法和getXxx()方法读写变量的值。另外定义一个测试类FruitTest,建了个三水果类的对象,设置和输出了三种不同水果的信息。
时间: 2024-02-20 14:57:39 浏览: 135
Fruit-Dataset水果数据集+水果分类识别训练代码(支持googlenet, resnet, inception_v3,
5星 · 资源好评率100%
好的,以下是 Java 代码实现:
```java
public class Fruit {
private String name;
private String area;
private double price;
public Fruit() {
}
public Fruit(String name, String area, double price) {
this.name = name;
this.area = area;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class FruitTest {
public static void main(String[] args) {
Fruit apple = new Fruit();
apple.setName("苹果");
apple.setArea("山东");
apple.setPrice(5.0);
Fruit banana = new Fruit();
banana.setName("香蕉");
banana.setArea("海南");
banana.setPrice(3.0);
Fruit orange = new Fruit("橙子", "江西", 4.0);
System.out.println(apple.getName() + " 产自 " + apple.getArea() + " ,单价为 " + apple.getPrice() + " 元");
System.out.println(banana.getName() + " 产自 " + banana.getArea() + " ,单价为 " + banana.getPrice() + " 元");
System.out.println(orange.getName() + " 产自 " + orange.getArea() + " ,单价为 " + orange.getPrice() + " 元");
}
}
```
运行结果:
```
苹果 产自 山东 ,单价为 5.0 元
香蕉 产自 海南 ,单价为 3.0 元
橙子 产自 江西 ,单价为 4.0 元
```
阅读全文