编写程序一个shu类,用于描述个人藏书,包括作者zuozhe、书名 shuming、价格 jiage、出版社chubanshe、出版年份 chubannianfen共5个属性。然后编写一个测试类 cheshilei,对 shu类进行测试,输出正确的属性值。
时间: 2023-12-03 17:41:55 浏览: 200
以下是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;
}
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;
}
}
```
以下是测试类CheShiLei的代码实现:
```java
public class CheShiLei {
public static void main(String[] args) {
Shu shu1 = new Shu("李白", "将进酒", 20.5, "人民文学出版社", 2020);
System.out.println(shu1.getZuozhe());
System.out.println(shu1.getShuming());
System.out.println(shu1.getJiage());
System.out.println(shu1.getChubanshe());
System.out.println(shu1.getChubannianfen());
}
}
```
运行以上测试类,输出如下:
```
李白
将进酒
20.5
人民文学出版社
2020
```
可以看出,Shu类的属性值都被正确地赋值,并且通过测试类CheShiLei的调用,属性值也被正确地输出。
阅读全文