深入探索Ruby编程语言

需积分: 10 4 下载量 86 浏览量 更新于2024-07-20 收藏 2.93MB PDF 举报
"The Book of Ruby" 《The Book of Ruby》是由Huw Collingbourne编著的一本关于Ruby编程语言的专业书籍。这本书详细介绍了Ruby语言的各种核心概念、语法和特性,旨在帮助读者深入理解和掌握Ruby编程。 Ruby是一种面向对象的、动态的、强类型的编程语言,由日本的松本行弘(Yukihiro Matsumoto)于1995年创建。Ruby的设计哲学强调简洁和生产力,其语法清晰,易于阅读和编写,特别适合快速开发。在《The Book of Ruby》中,作者Huw Collingbourne不仅讲解了基础语法,如变量、数据类型、控制结构、函数和类,还涵盖了高级主题,如模块、元编程、闭包和异常处理。 书中可能包含了以下关键知识点: 1. **变量和数据类型**:Ruby支持多种数据类型,包括整数、浮点数、字符串、布尔值、数组、哈希表等。变量的标识符分为四种类型:局部变量、实例变量、类变量和全局变量,每种都有特定的命名规则。 2. **面向对象编程**:Ruby是纯面向对象的语言,所有数据都是对象,每个对象都属于一个类。书中会介绍如何定义类、创建对象以及继承和多态的概念。 3. **控制结构**:包括条件语句(if-else,case)、循环(while,for,until),以及Ruby特有的块(blocks)和迭代器。 4. **函数和方法**:Ruby中的函数和方法是紧密相关的,函数可以作为方法调用,而方法也可以独立存在。书中会讲解如何定义和调用方法,以及方法的参数传递机制。 5. **模块(Module)**:模块用于封装代码,实现代码重用,并可以用来实现多重继承的效果。 6. **元编程**:Ruby允许在运行时修改自身,这是其元编程能力的体现。书中会介绍如何利用这个特性动态地创建或修改类和方法。 7. **闭包(Closures)和块**:Ruby的块是闭包的一种形式,它们可以捕获并保存当前作用域的变量,即使在离开该作用域后仍然有效。 8. **异常处理**:Ruby提供了try-catch-like结构来处理程序运行时的错误,使用`begin-rescue-end`语句进行异常捕获。 9. **文件和I/O操作**:书中会涵盖如何读写文件、处理标准输入输出,以及与系统进行交互的相关内容。 10. **正则表达式**:Ruby内置了强大的正则表达式引擎,用于文本匹配和处理。 Huw Collingbourne作为SapphireSteelSoftware的技术总监,他的著作通常结合实际开发经验,具有很高的实践价值。对于想要学习或提升Ruby技能的开发者,《The Book of Ruby》无疑是一本宝贵的资源。此外,作者还可能分享了一些关于Ruby在实际项目中的应用案例,以及与其他编程语言比较的观点,有助于读者更全面地理解Ruby的魅力和适用场景。

continue to use Java language, Add a class Borrower that extends User. The constructor of the Borrower class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor must throw a NotALenderException with the message “A new borrower cannot lend books.”. The borrower class does not have any instance variable. The moreBook method of the Borrower class increases the number of books borrowed by the borrower by the number of books given as argument to the method (so the books borrowed by the borrower becomes more positive!) For example, if a borrower currently borrows 10 books and moreBook(2) is called then the borrower borrows 12 books. It is fine for the moreBook method to be given a negative value as argument, which means the borrower then just returned some books. For example, if a borrower currently borrows 10 books and moreBook(-2) is called then the borrower borrows 8 books. However, a borrower cannot lend books, so the number of books borrowed by the borrower must always be positive or zero, never negative. If the argument given to the moreBook method is too negative and would change the book variable into a negative value, then the number of books borrowed by the borrower must not change and the moreBook method must throw a NotALenderException with the message “A borrower cannot lend XXX book(s).”, where XXX is replaced with the result of -(book + number). For example, if a borrower currently borrows 10 books and moreBook(-12) is called then the borrower still borrows 10 books and the method throws a NotALenderException with the message “A borrower cannot lend 2 book(s).”. Note: to simplify the project, do not worry about the setBook method. Change other classes and interfaces as necessary

2023-05-25 上传

Action 4: increasing the number of books of a given user. When the user of the software specifies action 4, your program must ask the user to type the name of a user, and a number of books, and the program then uses that number to increase the number of books lent or borrowed by the user. Then the program goes back to the main menu. For example: Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Anna Anna borrows -5 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Anna Enter the number of books: 2 Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Anna Anna borrows -7 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 10 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Bob Enter the number of books: 2 Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 12 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): If the name of the user is wrong, then an UnknownUserException exception will be thrown by the Library object. The code of the main method of your CLI class must catch this exception, print the error message from the exception object, and then it just goes back to printing the menu of actions (by just going back to the beginning of the while loop). For example (where 4, aaaa, and 2 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: aaaa Enter the number of books: 2 User aaaa unknown. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): Note that, even if a consumer is a borrower, the readPosInt method prevents the typed number of books from being negative. This means a borrower will never throw a NotALenderException. Nevertheless the code of the main method of your CLI class must handle this exception by printing the error message "BUG! This must never happen!" and immediately terminating the program using System.exit(1). For example (where 3, Bob, 4, Bob, and -15 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 12 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Bob Enter the number of books: -15 Positive integers only! Enter the number of books:

2023-05-24 上传

use java language ,In this project you need to write a book lending system for a Library. The system has different roles for registered users. There are two types of user roles: borrower and lender. Write an IUser interface for library users, with the following UML specification: +----------------------------------+ | <<interface>> | | IUser | +----------------------------------+ | + getName(): String | | + getBook(): int | | + moreBook(int number): void | +----------------------------------+ and a User class that implements IUser and has the following UML specification: +-----------------------------------+ | User | +-----------------------------------+ | - name: String | | - book: int | +-----------------------------------+ | + User(String name, int book) | | + getName(): String | | + getBook(): int | | # setBook(int book): void | | + moreBook(int number): void | | + testUser(): void | +-----------------------------------+ The name instance variable indicates the user name. The book instance variable indicates the number of books borrowed by the user. The setBook method changes the number of books borrowed by the user. The setBook method is protected, not public. This means that only subclasses of the User class can use the setBook method. All the other classes in the system cannot use the setBook method, so they cannot change the number of books borrowed by a user. The purpose of the moreBook method is to increase the number of books borrowed or lent by the user (depending on what kind of user it is) by the number given as argument to the method. The moreBook method of the User class is abstract, since we do not know what kind of role the user is (a borrower borrows books from other users and a lender lend books to other users). Also add to your program a Test class to test your User class. public class Test { public static void main(String[] args) { User.testUser(); } }

2023-05-25 上传