Java构造器与初始化清理

需积分: 31 1 下载量 160 浏览量 更新于2024-07-27 收藏 321KB PDF 举报
"《Thinking in Java》是 Bruce Eckel 编著的一本深入学习 Java 语言的经典书籍,作者以其深厚的 Java 知识底蕴,帮助读者更轻松地掌握 Java 学习。本书着重讨论了初始化和清理这两个编程中常见的问题,这些问题在不安全的程序设计中经常导致错误和资源浪费。在 C++ 和 Java 中,构造函数(constructor)是解决初始化问题的关键,而 Java 还引入了垃圾回收机制(garbage collector),自动管理内存清理,以避免资源泄漏。本章将详细介绍如何利用构造函数确保对象初始化,并探讨构造函数命名的规则和逻辑。" 《Thinking in Java》一书指出,初始化问题在很多编程错误中占有重要地位,尤其是当程序员忘记初始化变量时,可能导致难以预料的程序行为。库组件的使用中,如果没有明确的初始化指南,也会给用户带来困扰。另一方面,清理问题往往被忽视,因为代码执行后,不再直接引用的对象可能仍然占用资源,这在内存管理中尤其重要。C++ 通过构造函数解决了初始化问题,而 Java 在此基础上添加了垃圾回收机制,确保不再使用的内存能够自动回收。 Java 中的构造函数是一种特殊的方法,它在创建对象时自动调用,确保了每个实例化对象的初始化。为了避免用户忘记初始化,构造函数的使用强制性地融入了类的生命周期。然而,如何命名构造函数成为一个挑战。为了解决命名冲突和让编译器能正确识别构造函数,Java 选择了让构造函数的名字与类名相同,这种做法在 C++ 中也有应用,因为它直观地表明了这个方法会在对象创建时被调用。 以下是一个简单的 Java 类,展示了构造函数的使用: ```java public class SimpleConstruct { private int value; // 构造函数 public SimpleConstruct(int val) { this.value = val; // 初始化成员变量 } // 其他方法... } ``` 在这个例子中,`SimpleConstruct` 类有一个带参数的构造函数,它接收一个整数值 `val` 并将其赋值给类的私有成员变量 `value`,确保了对象在创建时就被正确初始化。这就是 Java 如何利用构造函数保证初始化的过程,同时避免了像 C++ 中可能出现的忘记初始化的问题。 总结来说,《Thinking in Java》强调了初始化和清理的重要性,并详细介绍了 Java 中如何通过构造函数和垃圾回收机制来解决这些问题,从而提高代码的安全性和效率。对于想要深入理解 Java 的开发者来说,这本书提供了宝贵的指导和洞察。

Write java code: Copy the files, small_weapons.txt, and large_weapons.txt from the assignment folder on Blackboard and save them to your folder. For testing purposes, you should use the small file. Use the large file when you think the application works correctly. To see what is in the files use a text editor. Nilesh is currently enjoying the action RPG game Torchlight 2 which is an awesome game and totally blows Auction House Simulator 3, oh sorry, that should be Diablo 3, out of the water. He has got a file containing info on some of the unique weapons in the game. The transaction file contains the following information: Weapon Name (string) Weapon Type (string) Damage (int) Weapon Speed (double) … To tell if one weapon is better than another you need to know the Damage Per Second (DPS) the weapon does, since weapons have a different attack speed. DPS is calculated by taking the damage value and dividing it by the attack speed.a) You will write a program that will allow Nilesh to load the file and display the weapon info and DPS of every weapon in the input file. When the user chooses to open a weapon file, they are required to type in the filename. It will then read the 4 values about a particular weapon and then display the 4 values to the console window on one line neatly padded along with the DPS of the weapon. This is repeated until the end of the file. b) Modify your code from a) so that the weapon information written to the console window is also written to a text file. The user should be able to specify the name of the file. Add a header to the beginning of the file which has column headers for each column. At the end of the text file display the total number of weapons in the file. c) Document your code appropriately and add your name and id number as comments at the top of your code. Please also submit this text file you have created. Enter the file you want to search end with .txt: large_weapons.txt 1 Blackfang Bludgeon Great Hammer 489 1.44 339.58333333333337 2 Bones 2 Boneshredder Great Axe 256 0.84 304.76190476190476 3 Comet's Tail Great Sword 872 1.2 726.6666666666667 4 Decapitator Great Sword 188 1.08 174.07407407407408 5 Demolisher Great Hammer 887 1.32 671.9696969696969

2023-05-27 上传