📄 tij0198.html
字号:
mechanism like virtual base classes in C++.
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
create a version of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that can be instantiated, use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>implements
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">keyword,
whose syntax looks like inheritance:
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">public
interface Face {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
public void smile();
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">}</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">public
class Baz extends Bar implements Face {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
public void smile( ) {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
System.out.println("a warm smile");
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
}
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">}</FONT></TT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> There’s
no
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>virtual</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword in Java because all non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods always use dynamic binding. In Java, the programmer doesn’t have
to decide whether to use dynamic binding. The reason
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>virtual</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
exists in C++ is so you can leave it off for a slight increase in efficiency
when you’re tuning for performance (or, put another way, “If you
don’t use it, you don’t pay for it”), which often results in
confusion and unpleasant surprises. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword provides some latitude for efficiency tuning – it tells the
compiler that this method cannot be overridden, and thus that it may be
statically bound (and made inline, thus using the equivalent of a C++ non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>virtual</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
call). These optimizations are up to the compiler.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Java
doesn’t provide multiple inheritance (MI), at least not in the same sense
that C++ does. Like
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>protected</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
MI seems like a good idea but you know you need it only when you are face to
face with a certain design problem. Since Java uses a singly-rooted hierarchy,
you’ll probably run into fewer situations in which MI is necessary. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>interface</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword takes care of combining multiple interfaces.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Run-time
type identification functionality is quite similar to that in C++. To get
information about handle
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>X,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you can say, for example:
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">X.getClass().getName();</FONT></TT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
perform a type-safe downcast you say:
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">derived
d = (derived)base;
</FONT></TT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">just
like an old-style C cast. The compiler automatically invokes the dynamic
casting mechanism without requiring extra syntax. Although this doesn’t
have the benefit of easy location of casts as in C++ “new casts,”
Java checks usage and throws exceptions so it won’t allow bad casts like
C++ does.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Exception
handling in Java is different because there are no destructors. A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finally</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
clause can be added to force execution of statements that perform necessary
cleanup. All exceptions in Java are inherited from the base class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Throwable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so you’re guaranteed a common interface.
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">public
void f(Obj b) throws IOException {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
myresource mr = b.createResource();
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
try {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
mr.UseResource();
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
} catch (MyException e) {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
// handle my exception
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
} catch (Throwable e) {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
// handle all other exceptions
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
} finally {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
mr.dispose(); // special cleanup
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
}
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">}</FONT></TT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Exception
specifications in Java are vastly superior to those in C++. Instead of the C++
approach of calling a function at run-time when the wrong exception is thrown,
Java exception specifications are checked and enforced at compile-time. In
addition, overridden methods must conform to the exception specification of the
base-class version of that method: they can throw the specified exceptions or
exceptions derived from those. This provides much more robust
exception-handling code.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Java
has method overloading, but no operator overloading. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class does use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+=
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">operators
to concatenate strings and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">expressions
use automatic type conversion, but that’s a special built-in case.
</FONT><a name="OLE_LINK7"></a><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>const</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
issues in C++ are avoided in Java by convention. You pass only handles to
objects and local copies are never made for you automatically. If you want the
equivalent of C++’s pass-by-value,
<a name="OLE_LINK8"></a>you
call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clone( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to produce a local copy of the argument (although the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clone( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">mechanism
is somewhat poorly designed – see Chapter 12). There’s no
copy-constructor that’s automatically called.
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
create a compile-time constant value, you say, for example:
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black"><B>static
final int SIZE = 255;
</B></FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black"><B>static
final int BSIZE = 8 * SIZE;
</B></FONT></TT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Because
of security issues, programming an “application” is quite different
from programming an “applet.” A significant issue is that an applet
won’t let you write to disk, because that would allow a program
downloaded from an unknown machine to trash your disk. This changes somewhat
with Java 1.1<A NAME="Index3127"></A>
digital signing, which allows you to unequivocally
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>know</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
everyone that wrote all the programs that have special access to your system
(one of which might have trashed your disk; you still have to figure out which
one and what to do about it.). Java 1.2 also promises more power for applets
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Since
Java can be too restrictive in some cases, you could be prevented from doing
important tasks such as directly accessing hardware. Java solves this with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>native
methods
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that allow you to call a function written in another language (currently only C
and C++ are supported). Thus, you can always solve a platform-specific problem
(in a relatively non-portable fashion, but then that code is isolated). Applets
cannot call native methods, only applications.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Java
has built-in support for comment documentation, so the source code file can
also contain its own documentation, which is stripped out and reformatted into
HTML via a separate program. This is a boon for documentation maintenance and
use.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Java
contains standard libraries for solving specific tasks. C++ relies on
non-standard third-party libraries. These tasks include (or will soon include):
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Networking
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Database Connection (via JDBC)
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Multithreading
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Distributed Objects (via RMI and CORBA)
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Compression
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Commerce
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
availability and standard nature of these libraries allow for more rapid
application development.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Java
1.1<A NAME="Index3128"></A>
includes the Java Beans standard, which is a way to create components that can
be used in visual programming environments. This promotes visual components
that can be used under all vendor’s development environments. Since you
aren’t tied to a particular vendor’s design for visual components,
this should result in greater selection and availability of components. In
addition, the design for Java Beans is simpler for programmers to understand;
vendor-specific component frameworks tend to involve a steeper learning curve.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> If
the access to a Java handle fails, an exception is thrown. This test
doesn’t have to occur right before the use of a handle; the Java
specification just says that the exception must somehow be thrown. Many C++
runtime systems can also throw exceptions for bad pointers.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> Generally,
Java is more robust, via:
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Object handles initialized to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(a keyword)
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Handles are always checked and exceptions are thrown for failures
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
All array accesses are checked for bounds violations
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Automatic garbage collection prevents memory leaks
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Clean, relatively fool-proof exception handling
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Simple language support for multithreading
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–
Bytecode verification of network applets
</FONT></OL><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Da Vinci Extras" SIZE=39 COLOR="Black">K</FONT><a name="_Toc407441464"></a><a name="_Toc408018848"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0197.html">Prev</a> | <a href="tij0199.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -