- 论坛徽章:
- 0
|
第一章:对象导论
什么是对象?
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 object—to put this
in a concrete sense, each object has a unique address in memory.
什么是类?
A class describes a set of objects that have identical characteristics (data elements) and
behaviors (functionality), a class is really a data type because a floating point number, for
example, also has a set of characteristics and behaviors.
对象特性:
每个对象都有一个接口
每个对象都提供服务:
Your program itself will provide services to the user, and it will accomplish this by using the services offered by other objects.
接口与实现互相分离:
实现被隐藏,类创建者更改实现不会导致调用者的程序会出现问题
java的访问界定:
Java uses three explicit keywords to set the boundaries in a class: public, private, and protected. These access specifiers determine who can use the definitions that follow. public means the following element is available to everyone. The private keyword, on the other hand, means that no one can access that element except you, the creator of the type, inside methods of that type. private is a brick wall between you and the client programmer. Someone who tries to access a private member will get a compile-time error. The protected keyword acts like private, with the exception that an inheriting class has access to protected members, but not private members. Inheritance will be introduced shortly.
与C++不同的地方在于:Java also has a “default” access, which comes into play if you don’t use one of the aforementioned specifiers. This is usually called package access because classes can access the members of other classes in the same package (library component), but outside of the
package those same members appear to be private.
组合与集成:
什么时候采用组合什么时候采用继承
创建一个新类时应该优先考虑组合,这样更加的简单灵活
继承:所有可以发给基类对象的消息同时也可以发送给派生类
Since we know the type of a class by the messages we can send to it, this means
that the derived class is the same type as the base class
既然类的类型取决于所能发送的消息,那就意味着派生类和基础类具有相同的类型
如何让导出类和基类产生差异?
有以下两种方法:
(1) 添加新的方法 is-like- a
(2) 覆盖基类中的方法(overriding)is-a
“I’m using the same interface method here, but I want it to do something different for my new type.”
多态性:
在C++中,在基类中函数加上virtual关键字,在派生类中重写该函数,运行时根据对象的实际类型来调用相应的函数。在java中动态绑定时缺省的,不需要添加额外的关键字来实现多态
多态性的实现时依靠late binding技术来实现的,这意味着编译时并不确定具体函数的调用,在运行时依据特定类型来确认具体使用那个函数
下面是early binding与late binding的概念
It means the compiler generates a call to a specific function name, and the runtime system resolves this call to the absolute address of the code to be executed. In OOP, the program cannot determine the address of the code until run time
在java中的late binding实现如下:
To perform late binding, Java uses a special bit of code in lieu of the absolute call. This code calculates the address of the method body, using information stored in the object
单根继承:所有的类最终都源于一个基类(除了C++外,所有的opp都是单根继承)
容器
参数化类型:
问题引入
Wouldn’t it make sense to somehow create the container so that it knows the types that it holds, eliminating the need for the downcast and a possible mistake? The solution is called a parameterized type mechanism.
问题解决:
A parameterized type is a class that the compiler can automatically customize to work with particular types. For example, with a parameterized container, the compiler could customize that container so that it would accept only Shapes and fetch only Shapes.
You’ll recognize the use of generics by the angle brackets with types inside; for example,
an ArrayList that holds Shape can be created like this:
ArrayList shapes = new ArrayList();
对象的创建、使用和使命周期:
对象的创建有两种方式:
静态创建(编译时)——————在stack上创建,需要知道对象确定的数量、生命周期
动态创建(运行时)——————在heap上创建,更加耗费时间
JAVA采用动态创建,new,但通过垃圾函收栈解决了delete问题,避免了内存泄露
In a language like C++, you must determine programmatically when to destroy
the object, which can lead to memory leaks if you don’t do it correctly (and this is a common
problem in C++ programs). Java provides a feature called a garbage collector that
automatically discovers when an object is no longer in use and destroys it. A garbage
collector is much more convenient because it reduces the number of issues that you must
track and the code you must write. More importantly, the garbage collector provides a much
higher level of insurance against the insidious problem of memory leaks, which has brought
many a C++ project to its knees.
With Java, the garbage collector is designed to take care of the problem of releasing the
memory (although this doesn’t include other aspects of cleaning up an object). The garbage
collector “knows” when an object is no longer in use, and it then automatically releases the
memory for that object. This, combined with the fact that all objects are inherited from the
single root class Object and that you can create objects only one way—on the heap—makes
the process of programming in Java much simpler than programming in C++. You have far
fewer decisions to make and hurdles to overcome
JAVA的异常处理:
Java’s exception handling stands out among programming languages, because in Java,
exception handling was wired in from the beginning and you’re forced to use it. It is the
single acceptable way to report errors. If you don’t write your code to properly handle
exceptions, you’ll get a compile-time error message. This guaranteed consistency can
sometimes make error handling much easier. (一种强制性的处理)
JAVA中的并发:
Java’s concurrency is built into the language, and Java SE5 has added significant additional
library support.
Java 和Internet
客服端编程:
概念:传统的解决模式CGI:web浏览器仅仅具备显示HTML语言的功能,而不会执行任何简单的操作,它将数据返回给服务器,服务器一般通过cgi-bin下的一个处理程序进行处理,处理完了再将结果发给web浏览器
客服端编程是web浏览器自身执行程序,负担了服务器的处理任务,称为了一个功能受限的操作系统。
实现方式:
(1)插件:This is a way for a programmer to add new functionality to the browser by
downloading a piece of code that plugs itself into the appropriate spot in the browser. It tells
the browser, “From now on you can perform this new activity.”
(2)脚本语言:With a scripting
language, you embed the source code for your client-side program directly into the HTML
page, and the plug-in that interprets that language is automatically activated while the HTML page is being displayed. Scripting languages tend to be reasonably easy to understand and, because they are simply text that is part of an HTML page, they load very quickly as part of the single server hit required to procure that page
常用脚本语言:JavaScript,TCL/Tk
脚本语言是不够安全的,只能解决简单问题
(3) JAVA
Java allows client-side programming via the applet and with Java Web Start.
服务器编程:These include Java-based Web servers that allow you to perform all your server-side programming in Java by writing what are called servlets. Servlets and their offspring, JSPs, are two of the most compelling reasons that companies that develop Web sites are moving to Java, especially because they eliminate the problems of dealing with differently abled browsers.
第二章 一切都是对象
存储在什么地方:
stack:在java中用来存放对象的引用,而不是对象本身
heap:用来存放对象
constant:存放在程序的代码区
non_ram:用来存放流对象和持久化对象,存放在disk上
作用域:Java中,引用的作用域消失后,对象依然存在。对象的释放不需要手动进行,而是依靠垃圾回收站机制。
类:
Java会对成员变量进行初始化,防止产生错误(C++不会这么做)
但对于非类成员变量,如int x,不会赋值,但Java编译时会报错
Java中任何传递对象的场合实际上传递的都是对象的应用,而不是对象本身,但传递基本类型时,是个例外
构建Java程序
名字可见性:
问题——. If you use a name in one module of the program, and another programmer uses the same name in another module, how do you distinguish one name from another and prevent the two names from “clashing”
java的解决方案:Java was able to avoid all of this by taking a fresh approach. To produce an unambiguous name for a library, the Java creators want you to use your Internet domain name in reverse since domain names are guaranteed to be unique. Since my domain name is
MindView.net, my utility library of foibles would be named
net.mindview.utility.foibles.
类库的使用:import关键字相当于include,与C++不同,java库中纯粹是类,不包括方法和数据
import java.util.ArrayList;
import java.util.*;一次性导入一群类
static关键字:这个关键字主要是为来解决两个问题
(1) 不通过对象来访问类成员
(2) 不通过对象来访问类方法
class StaticTest {
static int i = 47;
}
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
StaticTest.i++;
现在st1.i与st2.i都是48了,也就是说静态成员的空间分配时在类分定义时就分配了,而不是在对象定义时分配的,所有的对象都共享同一静态成员,对其应用既可以通过对象也可以通过类
static方法的作用在于不创建任何对象的情况下调用它。
实际上static的引入,是为了弥补Java语言一切都是对象的不足,增加了些许的过程元素
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/69999/showart_2037179.html |
|