编程实现一个Book类,包含title,author,price,publisher四个成员变量,其中title和author只能通过get()方法访问,price和publisher可以通过set()和get()访问,重写Book类的构造方法,该方法传入title和author两个参数。
很简单,考的是get、set和构造方法————很基础的东西要学会理解(private的变量,外界就不可随意访问)
/**
* @Description Book
* @Author hgg
* @Date 2021/12/23 11:51
* @Version 1.0
*/
public class Book {
private String title;
private String author;
private Double price;
private String publisher;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
/**
* @ClassName: Book
* @Description:
* @Author: 海晨忆
* @Date: 2021/12/23 11:53
*/
public class Book {
private String title;
private String author;
private double price;
private String publisher;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
这个考的就是publish跟private的含义,希望可以帮到你