Chapter 1: Declarations and Access Control ---

本文详细阐述了Java标识符的定义、组成规则、限制条件、关键字使用、大小写敏感性以及常见错误提示,帮助开发者深入理解并正确使用Java标识符。

Legal Identifiers

■ Identifiers must start with a letter, a currency character ($), or a connecting

character such as the underscore ( _ ). Identifiers cannot start with a number!

■ After the first character, identifiers can contain a

ny combination of letters,

currency characters, connecting characters, or numbers.

■ In practice, there is no limit to the number of characters an identifier can

contain.

■ You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java

keywords including one new one for 5.0, enum.

■ Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

 

Complete List of Java Keywords (assert added in 1.4, enum added in 1.5)

 

 

Chapter 3: Introduction to Objects and Classes The Object-Oriented Paradigm Course: CS101: Java Language Programming Instructor: [Instructor Name] Date: September 22, 2025 Practical Session Objective Lab Goal: Refactor to an Object-Oriented Design Our goal is to stop using loose, primitive variables to represent our contact and instead create a proper Contact object. This is a foundational step in building professional software. Prerequisites: Java JDK 11 or higher installed. An IDE like IntelliJ IDEA or VS Code ready. Your MyContactManager project from the last lab. Final Result Preview: The console output will look exactly the same as before, but the underlying code structure will be vastly superior, organized, and ready for future expansion. Step 1: Create the Contact Class Blueprint First, we need to create the blueprint for what a "Contact" is in our application. 1. Goal: Create a new Contact.java file and define the state of a contact. 2. Instructions: 1. In your IDE's project explorer, right-click on your source folder and create a new Java Class. 2. Name the file Contact . This will generate Contact.java . 3. Inside the Contact class, declare the instance variables that every contact will have. These are the same variables that are currently cluttering up your main method. 3. Code Block (for Contact.java ): --- Contact Details --- Name: John Doe Age: 42 Phone: 555-1234 1 2 3 44. Expected Result: You now have two files in your project: MyContactManager.java and Contact.java . The code should compile without errors. Step 2: Instantiate a Contact Object Now that we have the blueprint, let's go back to our main application and create an actual contact from it. 1. Goal: In MyContactManager.java , replace the old primitive variables with a single Contact object. 2. Instructions: 1. Open MyContactManager.java . 2. Delete all the old variable declarations for the contact ( String firstName = "Jane"; , int age = 32; , etc.). 3. In their place, declare and instantiate a new Contact object. 3. Code Block (in main method of MyContactManager.java ): 4. Expected Result: Your main method is now much cleaner. Your code will no longer compile because the parts that used the old variables are now broken. This is good! We'll fix it next. Step 3: Set the Object's State Our contact1 object exists, but its instance variables are all empty ( null or 0 ). Let's fill them with data using dot notation. 1. Goal: Assign values to the instance variables of our new contact1 object. public class Contact { // Instance variables to hold the state of a single contact String firstName; String lastName; int age; String phoneNumber; boolean isFriend; } 1 2 3 4 5 6 7 8 public static void main(String[] args) { // Delete the old variables: // String firstName = "Jane"; (DELETE) // String lastName = "Doe"; (DELETE) // int age = 32; (DELETE) // ...and so on. // Create an object from our blueprint Contact contact1 = new Contact(); // Now we have a single variable, 'contact1', that holds all // the information about our contact. } 1 2 3 4 5 6 7 8 9 10 11 12 132. Instructions: 1. After the line Contact contact1 = new Contact(); , use the dot operator to access each instance variable and assign it a value. 3. Code Block (in main method of MyContactManager.java ): 4. Expected Result: The contact1 object in memory now holds all the data for John Doe. The code still won't compile because the System.out.println statements are broken. Step 4: Get the Object's State Finally, let's fix our print statements to read the data back from the object, again using dot notation. 1. Goal: Update the System.out.println statements to use the object's fields. 2. Instructions: 1. Find your old println statements that are showing compile errors. 2. Prefix each old variable name with contact1. to tell Java to get the value from the object. 3. Code Block (in main method of MyContactManager.java ): 4. Expected Result: All compile errors should now be gone! Your application is fully refactored. // ... after creating the contact object Contact contact1 = new Contact(); // Use dot notation to set the object's state contact1.firstName = "John"; contact1.lastName = "Doe"; contact1.age = 42; contact1.phoneNumber = "555-1234"; contact1.isFriend = false; 1 2 3 4 5 6 7 8 9 // ... after setting the object's state System.out.println("\n--- Contact Details ---"); // OLD: System.out.println("Name: " + firstName + " " + lastName); // NEW: System.out.println("Name: " + contact1.firstName + " " + contact1.lastName); // OLD: System.out.println("Age: " + age); // NEW: System.out.println("Age: " + contact1.age); // OLD: System.out.println("Phone: " + phoneNumber); // NEW: System.out.println("Phone: " + contact1.phoneNumber); 1 2 3 4 5 6 7 8 9 10 11 12 13 14Final Code and Verification Let's look at the final, refactored MyContactManager.java and verify that it works. 1. Goal: Run the application and confirm the output is correct. 2. Code Block (Complete MyContactManager.java ): 3. Expected Output: Success! The output is the same, but our code structure is infinitely better. Optional Challenge Task For Those Who Finish Early... 🚀 If you've completed the refactoring, this challenge will solidify your understanding of objects. Challenge: Create a Second, Independent Object 1. In your main method, create a second Contact object called contact2 . 2. Set the state for contact2 with completely different data (e.g., "Jane Smith", age 35, etc.). 3. Add println statements to display the details for contact2 after displaying the details for contact1 . public class MyContactManager { public static void main(String[] args) { // 1. Create an object (an instance) from the Contact class blueprint Contact contact1 = new Contact(); // 2. Set the state of the object using dot notation contact1.firstName = "John"; contact1.lastName = "Doe"; contact1.age = 42; contact1.phoneNumber = "555-1234"; contact1.isFriend = false; // 3. Get the state of the object using dot notation System.out.println("--- Contact Details ---"); System.out.println("Name: " + contact1.firstName + " " + contact1.lastName); System.out.println("Age: " + contact1.age); System.out.println("Phone: " + contact1.phoneNumber); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 --- Contact Details --- Name: John Doe Age: 42 Phone: 555-1234 1 2 3 4This will prove that contact1 and contact2 are two separate objects in memory, each with its own independent state, even though they were built from the same class blueprint. Q&A and Wrap-up Questions? Today's Most Important Lesson Organizing code into classes that model real-world concepts is the single most powerful strategy for managing complexity in software. We took a chaotic collection of variables and transformed it into a clean, intuitive structure. A Contact is now a "thing" in our program, just as it is in the real world. This is the foundation upon which all large-scale, professional applications are built. What's Next? Up Next: Protecting Our Objects Right now, our code has a major flaw. Anyone using our Contact class can do this: contact1.age = -99; contact1.phoneNumber = "this is not a phone number"; This is dangerous and leads to bugs! An object should be in control of its own data. Next Lecture: Chapter 4: Deeper Dive into Classes - Encapsulation We'll learn about encapsulation, a core OOP principle for data hiding and protection. We'll introduce private variables and public getter/setter methods to create robust, bug-resistant objects. 以上问题的答案是什么
09-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值