Introduction To Objects

本文探讨了编程语言中的抽象概念及其对问题解决能力的影响。从汇编语言到面向对象编程,作者阐述了抽象从机器层面到问题层面的演变,强调了面向对象编程通过对象模型实现更高效、灵活的解决方案。通过分析Smalltalk等语言的关键特征,揭示了面向对象编程的核心优势,并解释了其如何促进问题与计算机解决方案之间的无缝对接。

1.The progress of abstraction

All programming languages provide abstractions. It can be argued that the complexity of the problems youre able to solve is directly related to the kind and quality of abstraction. By kind” I mean, What is it that you are abstracting?” Assembly language is a small abstraction of the underlying machine. Many so-called imperative” languages that followed (such as FORTRAN, BASIC, and C) were abstractions of assembly language. These languages are big improvements over assembly language, but their primary abstraction still requires you to think in terms of the structure of the computer rather than the structure of the problem you are trying to solve. The programmer must establish the association between the machine model (in the solution space,” which is the place where youre modeling that problem, such as a computer) and the model of the problem that is actually being solved (in the problem space,” which is the place where the problem exists). The effort required to perform this mapping, and the fact that it is extrinsic to the programming language, produces programs that are difficult to write and expensive to maintain, and as a side effect created the entire programming methods” industry. 

The alternative to modeling the machine is to model the problem youre trying to solve. Early languages such as LISP and APL chose particular views of the world (All problems are ultimately lists” or All problems are algorithmic,” respectively). PROLOG casts all problems into chains of decisions. Languages have been created for constraint-based programming and for programming exclusively by manipulating graphical symbols. (The latter proved to be too restrictive.) Each of these approaches is a good solution to the particular class of problem theyre designed to solve, but when you step outside of that domain they become awkward. 

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space. This representation is general enough that the programmer is not constrained to any particular type of problem. We refer to the elements in the problem space and their representations in the solution space as objects.” (You will also need other objects that dont have problem-space analogs.) The idea is that the program is allowed to adapt itself to the lingo of the problem by adding new types of objects, so when you read the code describing the solution, youre reading words that also express the problem. This is a more flexible and powerful language abstraction than what weve had before. Thus, OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run. Theres still a connection back to the computer: each object looks quite a bit like a little computerit has a state, and it has operations that you can ask it to perform. However, this doesnt seem like such a bad analogy to objects in the real worldthey all have characteristics and behaviors. 

Alan Kay summarized five basic characteristics of Smalltalk, the first successful object-oriented language and one of the languages upon which Java is based. These characteristics represent a pure approach to object-oriented programming: 

Everything is an object. Think of an object as a fancy variable; it stores data, but you can make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem youre trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program. 

A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you send a message” to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object. 

Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects. 

Every object has a type. Using the parlance, each object is an instance of a class, in which class” is synonymous with type.” The most important distinguishing characteristic of a class is What messages can you send to it?” 

All objects of a particular type can receive the same messages. This is actually a loaded statement, as you will see later. Because an object of type circle” is also an object of type shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP. 

Booch offers an even more succinct description of an object:

An object has state, behavior and identity.

This means that an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely distinguished from every other objectto put this in a concrete sense, each object has a unique address in memory.

对象入门

1、抽象的进步
    所有编程语言的最终目的都是提供一种“抽象”方法。一种较有争议的说法是:解决问题的复杂程度直接取决于抽象的种类及质量。这儿的“种类”是指准备对什么进行“抽象”?汇编语言是对基础机器的少量抽象。后来的许多“命令式”语言(如FORTRAN,BASIC和C)是对汇编语言的一种抽象。与汇编语言相比,这些语言已有了长足的进步,但它们的抽象原理依然要求我们着重考虑计算机的结构,而非考虑问题本身的结构。在机器模型(位于“方案空间”)与实际解决的问题模型(位于“问题空间”)之间,程序员必须建立起一种联系。这个过程要求人们付出较大的精力,而且由于它脱离了编程语言本身的范围,造成程序代码很难编写,而且要花较大的代价进行维护。由此造成的副作用便是一门完善的“编程方法”学科。
    为机器建模的另一个方法是为要解决的问题制作模型。对一些早期语言来说,如LISP和APL,它们的做法是“从不同的角度观察世界”——“所有问题都归纳为列表”或“所有问题都归纳为算法”。PROLOG则将所有问题都归纳为决策链。对于这些语言,我们认为它们一部分是面向基于“强制”的编程,另一部分则是专为处理图形符号设计的。每种方法都有自己特殊的用途,适合解决某一类的问题。但只要超出了它们力所能及的范围,就会显得非常笨拙。
    面向对象的程序设计在此基础上则跨出了一大步,程序员可利用一些工具表达问题空间内的元素。由于这种表达非常普遍,所以不必受限于特定类型的问题。我们将问题空间中的元素以及它们在方案空间的表示物称作“对象”(Object)。当然,还有一些在问题空间没有对应体的其他对象。通过添加新的对象类型,程序可进行灵活的调整,以便与特定的问题配合。所以在阅读方案的描述代码时,会读到对问题进行表达的话语。与我们以前见过的相比,这无疑是一种更加灵活、更加强大的语言抽象方法。总之,OOP允许我们根据问题来描述问题,而不是根据方案。然而,仍有一个联系途径回到计算机。每个对象都类似一台小计算机;它们有自己的状态,而且可要求它们进行特定的操作。与现实世界的“对象”或者“物体”相比,编程“对象”与它们也存在共通的地方:它们都有自己的特征和行为。

Alan Kay总结了Smalltalk的五大基本特征。这是第一种成功的面向对象程序设计语言,也是Java的基础语言。通过这些特征,我们可理解“纯粹”的面向对象程序设计方法是什么样的。

所有东西都是对象。可将对象想象成一种新型变量;它保存着数据,但可要求它对自身进行操作。理论上讲,可从要解决的问题身上提出所有概念性的组件,然后在程序中将其表达为一个对象。
    (2) 程序是一大堆对象的组合;通过消息传递,各对象知道自己该做些什么。为了向对象发出请求,需向那个对象“发送一条消息”。更具体地讲,可将消息想象为一个调用请求,它调用的是从属于目标对象的一个子例程或函数。
    (3) 每个对象都有自己的存储空间,可容纳其他对象。或者说,通过封装现有对象,可制作出新型对象。所以,尽管对象的概念非常简单,但在程序中却可达到任意高的复杂程度。
    (4) 每个对象都有一种类型。根据语法,每个对象都是某个“类”的一个“实例”。其中,“类”(Class)是“类型”(Type)的同义词。一个类最重要的特征就是“能将什么消息发给它?”。
    (5) 同一类所有对象都能接收相同的消息。这实际是别有含义的一种说法,大家不久便能理解。由于类型为“圆”(Circle)的一个对象也属于类型为“形状”(Shape)的一个对象,所以一个圆完全能接收形状消息。这意味着可让程序代码统一指挥“形状”,令其自动控制所有符合“形状”描述的对象,其中自然包括“圆”。这一特性称为对象的“可替换性”,是OOP最重要的概念之一。
    一些语言设计者认为面向对象的程序设计本身并不足以方便解决所有形式的程序问题,提倡将不同的方法组合成“多形程序设计语言”。

    ②:参见Timothy Budd 编著的《Multiparadigm Programming in Leda》,Addison-Wesley 1995 年出版。



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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值