1.创建一个名为Book的类,包含title(书名)、author(作者)、price(价格)等属性,并且包含一个方法display,用于打印书籍信息。
时间: 2024-12-14 10:21:55 浏览: 2
创建一个名为Book的类可以按照以下Python语言的语法来设计:
```python
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def display(self):
print(f"书名: {self.title}, 作者: {self.author}, 价格: {self.price}")
```
在这个类中,`__init__`方法是一个特殊的方法,也称为构造函数,用于初始化新创建的对象。`display`方法则是为了展示书籍的信息,当需要打印书名、作者和价格时,可以直接调用这个方法。
如果你想在其他地方使用这个Book类,你可以这样做:
```python
book1 = Book("哈利·波特", "J.K.罗琳", 50)
book1.display() # 输出: 书名: 哈利·波特, 作者: J.K.罗琳, 价格: 50
```
相关问题
用python20.创建一个名为Student的类,包含姓名、学号和年级属性。从文件中读取数据并创建Student对象列表。将这些对象按学号排序并将结果写入另一个文件。 21.创建一个名为Book的类,包含书名、作者和价格属性。从文件中读取数据并创建Book对象列表。找出价格最高的书并输出书名和作者。 22.创建一个名为Employee的类,包含名字、工号和薪水属性。从文件中读取数据并创建Employee对象列表。将这些对象按薪水排序并将结果写入另一个文件。 23.创建一个名为Person的类,包含姓名、年龄和性别属性。从文件中读取数据并创建Person对象列表。将这些对象按年龄从大到小排序并将结果写入另一个文件。 创建一个名为Rectangle的类,包含长和宽属性。从文件中读取数据并创建Rectangle对象列表。计算并输出所有矩形对象的周长和面积之和。
好的,我已经了解您的要求了,下面是代码:
20.
```python
class Student:
def __init__(self, name, student_id, grade):
self.name = name
self.student_id = student_id
self.grade = grade
def read_students(file_name):
student_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
student_list.append(Student(data[0], data[1], data[2]))
return student_list
def write_students(student_list, file_name):
student_list.sort(key=lambda x: x.student_id)
with open(file_name, 'w') as f:
for student in student_list:
f.write(f'{student.name},{student.student_id},{student.grade}\n')
students = read_students('students.txt')
write_students(students, 'sorted_students.txt')
```
21.
```python
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def read_books(file_name):
book_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
book_list.append(Book(data[0], data[1], float(data[2])))
return book_list
def find_highest_priced_book(book_list):
highest_priced_book = book_list[0]
for book in book_list:
if book.price > highest_priced_book.price:
highest_priced_book = book
return highest_priced_book
books = read_books('books.txt')
highest_priced_book = find_highest_priced_book(books)
print(f'The highest priced book is {highest_priced_book.title} by {highest_priced_book.author}.')
```
22.
```python
class Employee:
def __init__(self, name, employee_id, salary):
self.name = name
self.employee_id = employee_id
self.salary = salary
def read_employees(file_name):
employee_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
employee_list.append(Employee(data[0], data[1], int(data[2])))
return employee_list
def write_employees(employee_list, file_name):
employee_list.sort(key=lambda x: x.salary)
with open(file_name, 'w') as f:
for employee in employee_list:
f.write(f'{employee.name},{employee.employee_id},{employee.salary}\n')
employees = read_employees('employees.txt')
write_employees(employees, 'sorted_employees.txt')
```
23.
```python
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def read_people(file_name):
person_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
person_list.append(Person(data[0], int(data[1]), data[2]))
return person_list
def write_people(person_list, file_name):
person_list.sort(key=lambda x: x.age, reverse=True)
with open(file_name, 'w') as f:
for person in person_list:
f.write(f'{person.name},{person.age},{person.gender}\n')
people = read_people('people.txt')
write_people(people, 'sorted_people.txt')
```
24.
```python
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
def read_rectangles(file_name):
rectangle_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
rectangle_list.append(Rectangle(int(data[0]), int(data[1])))
return rectangle_list
def calculate_total_area_and_perimeter(rectangle_list):
total_area = 0
total_perimeter = 0
for rectangle in rectangle_list:
total_area += rectangle.area()
total_perimeter += rectangle.perimeter()
return total_area, total_perimeter
rectangles = read_rectangles('rectangles.txt')
total_area, total_perimeter = calculate_total_area_and_perimeter(rectangles)
print(f'Total area: {total_area} sq. units')
print(f'Total perimeter: {total_perimeter} units')
```
编写book类,该类定义了三个属性,分别是title表示书名,author表示作者,price表示价格,三个set方法分别用来设置书名、作者和价格的值,三个get方法分别用来获取书名,作者和价格。编写两个类测试类test,用来测试book类,创建book对象并输出其属性的值。
A:
```java
// Book类
public class Book {
private String title; // 书名
private String author; // 作者
private double price; // 价格
// 构造方法
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
// set方法
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
this.price = price;
}
// get方法
public String getTitle() {
return this.title;
}
public String getAuthor() {
return this.author;
}
public double getPrice() {
return this.price;
}
}
// test1类
public class test1 {
public static void main(String[] args) {
Book book = new Book("Java从入门到精通", "张三", 58.5);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
book.setTitle("Java编程思想");
book.setAuthor("Bruce Eckel");
book.setPrice(89.0);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
}
}
// test2类
public class test2 {
public static void main(String[] args) {
Book book = new Book("JavaWeb开发技术", "李四", 78.0);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
book.setPrice(99.9);
System.out.println("书名:" + book.getTitle() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice());
}
}
```
阅读全文