??xml version="1.0" encoding="utf-8" standalone="yes"?>
直到最q的IE老出问题Q公司派的本本装的是win2000Q用久了里面的IEl常会弹Z个crash的对话框Q似乎在XP里就很少见)Q然后所有打开的IEH口都只能关闭,解决无方Q于是想CFirefoxQ试用一下,W一感觉是非常快Q不是load firefoxq是打开新的|页Q基本上都是文字刷一下就出来Q图片则延迟了点Q不q还是比IE做得?) 很多的l节都考虑得比较符合用L操作习惯Q比如工h有一个下载的理器,理最q下载的咚咚Q不用第三方软gQ而且Firefox比IEy多了Q?.8MQ,Simple is beautifulQ呵呵,I like it.
]]>
If Java uses the pass-by reference, why won't a swap function work?
Your question demonstrates a common error made by Java language newcomers. Indeed, even seasoned veterans find it difficult to keep the terms straight.
Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.
Take the badSwap()
method for example:
public void badSwap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}
When badSwap()
returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int
to Object
, since Java passes object references by value as well.
Now, here is where it gets tricky:
public void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
tricky(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}
If we execute this main()
method, we see the following output:
X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0
The method successfully alters the value of pnt1
, even though it is passed by value; however, a swap of pnt1
and pnt2
fails! This is the major source of confusion. In the main()
method, pnt1
and pnt2
are nothing more than object references. When you pass pnt1
and pnt2
to the tricky()
method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.
Figure 1. After being passed to a method, an object will have at least two references
|
Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.
Figure 2. Only the method references are swapped, not the original ones
|
O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap objects.
Resources
As we moved from one language to another, generally we increased the level of abstraction at which the developer operates, which required the developer to learn a new, higher-level language that could then be mapped into lower-level ones, from C++ to C to assembly code to machine code and the hardware. At first, each higher layer of abstraction was introduced only as a concept. The first assembly languages were no doubt invented without the benefit of an (automated) assembler to turn mnemonics into bits, and developers were grouping functions together with the data they encapsulated long before there was any automatic enforcement of the concept. Similarly, the concepts of structured programming were taught before there were structured programming languages in widespread industrial use (for instance, Pascal).
当我们从一U语a发展到另一U语a的时候,通常我们提高了开发者进行开发所处的抽象层次Q这需要开发者学习新的、更高层ơ的能够映射C层次的语aQ如C++到C到汇~代码到机器码和g。最初,每一个更高层ơ的抽象只是作ؓ一个概念被引入。最初的汇编语言被发明的时候,作ؓ把助记符转换到字节码的汇~器q没有体pd什么明昄又是。。。。,cM的,l构化程序设计的概念也显得很不ؓ人接受直到结构化~程语言在工业界qؓ使用?br />
Over time, however, the new layers of abstraction became formalized, and tools such as assemblers, preprocessors, and compilers were constructed to support the concepts. This had the effect of hiding the details of the lower layers so that only a few experts (compiler writers, for example) needed to concern themselves with the details of how those layers work. In turn, this raises concerns about the loss of control induced by, for example, eliminating the GOTO statement or writing in a high-level language at a distance from the "real machine." Indeed, sometimes the next level of abstraction has been too big a reach for the profession as a whole, only of interest to academics and purists, and the concepts did not take a large enough mindshare to survive. (ALGOL-68 springs to mind. So does Eiffel, but it has too many living supporters to be a safe choice of example.)
随着旉的过去,然而,新的抽象层次也开始正式Ş成,相应的工具如汇编器、预处理器和~译器开始出C支持q些概念。它们把低抽象层ơ的l节隐藏Q得只有少数的专家Q如~译器的~写者)需要关心这些细节如何工作。。。。?br />
As the profession has raised the level of abstraction at which developers work, we have developed tools to map from one layer to the next automatically. Developers now write in a high-level language that can be mapped to a lower-level language automatically, instead of writing in the lower-level language that can be mapped to assembly language, just as our predecessors wrote in assembly language and had that translated automatically into machine language.
随着开发者开发的抽象层次的提高,我们开发出从一个层ơ自动映到另一个层ơ的工具。开发者现在只需~写高层ơ的~程语言Q之后的转换是自动q行的?br />
Clearly, this forms a pattern: We formalize our knowledge of an application in as high a level a language as we can. Over time, we learn how to use this language and apply a set of conventions for its use. These conventions become formalized and a higher-level language is born that is mapped automatically into the lower-level language. In turn, this next-higher-level language is perceived as low level, and we develop a set of conventions for its use. These newer conventions are then formalized and mapped into the next level down, and so forth.
昄Q这形成了一U模式:我们不断地提高抽象的层次的概念,随着旉的过去,我们学会了如何用这U语a以及应用一pd的用约束。这些约束变成正式后一U新的语a诞生了,而这U下一代的语言是从低层ơ被感知的,当我们定义一pd的用约束之后,q些新的U束又被正式化,来推动下一阶段的发展。不断地循环?br />
The next level of abstraction is the move, shown in Figure 1-1, to model-based development, in which we build software-platform-independent models.
下一阶段的抽象层ơ在发展Q如图所C。它是基于模型的开发,在这里我们徏立的是和软gq_无关的模型?br />
Software-platform independence is analogous to hardware-platform independence. A hardware-platform-independent language, such as C or Java, enables the writing of a specification that can execute on a variety of hardware platforms with no change. Similarly, a software-platform-independent language enables the writing of a specification that can execute on a variety of software platforms, or software architecture designs, with no change. So, a software-platform-independent specification could be mapped to a multiprocessor/multitasking CORBA environment, or a client-server relational database environment, with no change to the model.
软gq_无关是和gq_无关cM的概c一个硬件^台无关的语言Q如C或者JavaQ可以得一个规范的~写能够在不同的gq_上运行而不需要更攏V类似地Q一个Y件^台无关的语言可以使得一个规范的~写可以在不同的软gq_上运行,或者在不同的Y件架构中q行Q而不需要更攏V因此,一个Y件^台无关的规范可以映射到多处理?多Q务CORBA环境Q或者一个C-S关系数据库环境,而不需要更Ҏ型?br />
In general, the organization of the data and processing implied by a conceptual model may not be the same as the organization of the data and processing in implementation. If we consider two concepts, those of "customer" and "account," modeling them as classes using the UML suggests that the software solution should be expressed in terms of software classes named Customer and Account. However, there are many possible software designs that can meet these requirements, many of which are not even object-oriented. Between concept and implementation, an attribute may become a reference; a class may be divided into sets of object instances according to some sorting criteria; classes may be merged or split; statecharts may be flattened, merged, or separated; and so on. A modeling language that enables such mappings is software-platform independent.
ȝ而言Q由概念模型所驱动的数据的l成和处理也许和实际的数据组成和处理不完全相同。如果我们考虑两个概念Q比如“customer”和“Account”,他们徏模程UML的类图以期望能够被用?/p>
The Single Choice principle:
whenever a software system must support a set of variants, clients using objects from this set should not have knowledge of all the elements in the set. A violation of this principle can be illustrated with the variants of Pascal or the unions of C (as long as the union is placed inside a struct and there is a field that acts as a type descriptor). Code written that acts upon one of these “multiple personality entities? must be queried for its actual type, and we must program accordingly. This will produce multiple selection constructs in any module that uses these variant types.
Such constructs will become a hindrance to software change and evolution. Any addition of a new type to the variant set will force modifications in all modules that have to query the type to act accordingly. This multiple choices spread around many modules of a system is exactly the opposite to the single choice principle.
Traditional languages and paradigms do not provide a solution. On the other hand, object orientation, via polymorphism and dynamic binding present an elegant and complete implementation of this principle.
This concludes our consideration of the properties necessary for good modular structures. It will be seen that Abstract Data Types provide an adequate specification for modules that are consistent with the properties developed in this section. Eventually, we will cross the bridge into the software world and introduce the class as a potentially partial implementation of an Abstract Data Type.
在第三章Q介l了OO相关技术包括UMLQ其中提C一个概念:在一个承体pMQ良好的做法是越往上放多的CodeQ而Data则放到越下层Q子cor实现c)好。文章是q样解释的:公׃码尽可能地放到承层ơ的上方q样它可以被重用Q和数据不同Q在l承的过E中Q当子类实例不需要用的时候,代码也不会有额外的costQ,而数据元素则应该攑ֈ低好Q这样你不会Z需使用的数据成员付Z仗如下图Q?br />
W四章讲的是一个电脑公司的销售系l,寚w件的零售和各U搭配销售,要求pȝ能够l一对待Q这里引入了Composite模式Q这里不详述Q因是比较简单的?br />
W五章讲的是Decorator模式Q首先引入了一个汉堡店的系l,介绍了组合爆炸的设计Qƈ引入DecoratorQ解决了q种问题?br />
W六章讲的是不同~程语言Ҏ式的实现问题Q设计模式和习惯用法QidiomQ比较v来,后者是与特定语a相关的,而设计模式则是与语言无关的,q指的是支持OO的语a都支持设计模式,只不q实现v来的复杂E度各有不同Q文章D了一个例子用VB来实现State模式Q再用Java来实玎ͼ得出上述l论?br />
W七章第八章我就没详l看了,一是觉得这里的囄的不是标准UML图,看v来不太顺|另一个则是内容似乎没什么特别的。书的最后三分之一都是附录Q代码?br />
ȝ而言Q这本书不算一本经典的书,׃在图书馆借的Q在q书之前M一下。下ơ打看的模式书是《Head First Design Pattern》。不q得{辩之后才有旉了?/p>