Java编写程序一个shu类,用于描述个人藏书,包括作者zuozhe、书名shuming、价 格jiage、出版社chubanshe、出版年份chubannianfen.共5个属性。然后编写 一个测试类cheshilei,对shu类进行测试,输出正确的属性值。
时间: 2024-02-22 07:55:55 浏览: 68
好的,我可以帮您解答这个问题。
首先,让我们来创建一个Shu类,可以使用以下代码:
```java
public class Shu {
private String zuozhe;
private String shuming;
private double jiage;
private String chubanshe;
private int chubannianfen;
// 构造函数
public Shu(String zuozhe, String shuming, double jiage, String chubanshe, int chubannianfen) {
this.zuozhe = zuozhe;
this.shuming = shuming;
this.jiage = jiage;
this.chubanshe = chubanshe;
this.chubannianfen = chubannianfen;
}
// Getter和Setter方法
public String getZuozhe() {
return zuozhe;
}
public void setZuozhe(String zuozhe) {
this.zuozhe = zuozhe;
}
public String getShuming() {
return shuming;
}
public void setShuming(String shuming) {
this.shuming = shuming;
}
public double getJiage() {
return jiage;
}
public void setJiage(double jiage) {
this.jiage = jiage;
}
public String getChubanshe() {
return chubanshe;
}
public void setChubanshe(String chubanshe) {
this.chubanshe = chubanshe;
}
public int getChubannianfen() {
return chubannianfen;
}
public void setChubannianfen(int chubannianfen) {
this.chubannianfen = chubannianfen;
}
}
```
接下来,我们可以创建一个测试类,用于测试Shu类。可以使用以下代码:
```java
public class Cheshilei {
public static void main(String[] args) {
// 创建一个Shu对象
Shu shu = new Shu("张三", "Java编程思想", 88.8, "机械工业出版社", 2020);
// 输出属性值
System.out.println("作者:" + shu.getZuozhe());
System.out.println("书名:" + shu.getShuming());
System.out.println("价格:" + shu.getJiage());
System.out.println("出版社:" + shu.getChubanshe());
System.out.println("出版年份:" + shu.getChubannianfen());
}
}
```
这样,当您运行测试类时,就会输出正确的属性值。
希望这个答案能够帮到您!
阅读全文