现有若干图书信息(包含名称title、作者author、定价price)需要存储到set集合中,保证集合中无重复元素,并遍历查看。可以认为所有信息都相同的图书为重复数据。
时间: 2023-05-01 10:03:25 浏览: 108
这是一个关于图书信息的字节串,包括名称(title)、作者(author)、定价(price)三个元素,需要被存储到一个set集合中,以确保集合中没有重复元素,并可以进行历史查询。可以认为所有信息都相同的图书为重复数据。
相关问题
class Book { private: // 图书编号 int id; // 书名 string title; // 作者 string author; // 出版社 string publisher; // 出版日期 string date; // 价格 double price;帮我生成set和get方法
好的,根据你提供的类定义,这里给出一个set和get方法的例子:
```c++
class Book {
private:
// 图书编号
int id;
// 书名
string title;
// 作者
string author;
// 出版社
string publisher;
// 出版日期
string date;
// 价格
double price;
public:
// 构造函数
Book(int id, string title, string author, string publisher, string date, double price) {
this->id = id;
this->title = title;
this->author = author;
this->publisher = publisher;
this->date = date;
this->price = price;
}
// set方法
void setId(int id) {
this->id = id;
}
void setTitle(string title) {
this->title = title;
}
void setAuthor(string author) {
this->author = author;
}
void setPublisher(string publisher) {
this->publisher = publisher;
}
void setDate(string date) {
this->date = date;
}
void setPrice(double price) {
this->price = price;
}
// get方法
int getId() {
return id;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
string getPublisher() {
return publisher;
}
string getDate() {
return date;
}
double getPrice() {
return price;
}
};
```
这个类中包含了6个私有成员变量,对应的每个成员变量都有一个对应的set方法和get方法。set方法用来设置成员变量的值,get方法用来获取成员变量的值。这些方法分别在类的public部分声明,在类的外部可以通过调用这些方法来访问类的私有成员变量。
按照实体类创建规范创建图书类Book,包含四个属性(编号、书名、作者、价格)、无参构造器、有参构造器、重写toString方法,并在测试类中创建一个Book对象并将该对象存储到文件中(序列化)并写出代码
### 回答1:
好的,我可以回答这个问题。
以下是创建图书类Book的代码:
```java
import java.io.Serializable;
public class Book implements Serializable {
private int id;
private String name;
private String author;
private double price;
public Book() {}
public Book(int id, String name, String author, double price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
```
以下是测试类中创建Book对象并将该对象存储到文件中的代码:
```java
import java.io.*;
public class Test {
public static void main(String[] args) {
Book book = new Book(1, "Java编程思想", "Bruce Eckel", 99.0);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("book.dat"));
oos.writeObject(book);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上就是创建图书类Book并将该对象存储到文件中的代码。
### 回答2:
按照实体类创建规范创建图书类Book,包含四个属性(编号、书名、作者、价格)、无参构造器、有参构造器、重写toString方法,并在测试类中创建一个Book对象并将该对象存储到文件中(序列化)并写出代码。
下面是图书类Book的代码实现:
```java
import java.io.Serializable;
public class Book implements Serializable {
private int id;
private String title;
private String author;
private double price;
public Book() {
}
public Book(int id, String title, String author, double price) {
this.id = id;
this.title = title;
this.author = author;
this.price = price;
}
// Getter and Setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
```
下面是测试类的代码实现:
```java
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Test {
public static void main(String[] args) {
// 创建一个Book对象
Book book = new Book(1, "Java编程思想", "Bruce Eckel", 79.9);
// 序列化并保存到文件
try {
FileOutputStream fileOut = new FileOutputStream("book.ser");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(book);
objectOut.close();
fileOut.close();
System.out.println("Book对象已序列化并保存到文件book.ser");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
运行测试类中的main方法后,Book对象将被序列化并保存到名为"book.ser"的文件中。
### 回答3:
按照实体类创建规范创建图书类Book可以按照以下方式实现:
```
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 1L; // 序列化版本UID
private String number; // 编号
private String title; // 书名
private String author; // 作者
private double price; // 价格
public Book() {
// 无参构造器
}
public Book(String number, String title, String author, double price) {
this.number = number;
this.title = title;
this.author = author;
this.price = price;
}
// getter和setter方法省略
@Override
public String toString() {
return "Book{" +
"number='" + number + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
```
在测试类中创建一个Book对象并将该对象存储到文件中可以按照以下方式实现:
```
import java.io.*;
public class Main {
public static void main(String[] args) {
Book book = new Book("123", "Java入门", "张三", 49.99);
try {
FileOutputStream fileOutputStream = new FileOutputStream("book.ser");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(book);
objectOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码创建了一个Book对象并将其存储到名为"book.ser"的文件中,实现了序列化。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)