毕业论文

打赏
当前位置: 毕业论文 > 外文文献翻译 >

JAVA编程思想英文参考文献和翻译(5)

时间:2016-11-15 14:44来源:毕业论文
Each object keeps its own storage for its fields; ordinary fields are not shared among objects. Here is an example of a class with some fields: class DataOnly { int i; double d; boolean b; } This clas


Each object keeps its own storage for its fields; ordinary fields are not shared among objects. Here is an example of a class with some fields:
class DataOnly {
int i;
double d;
boolean b;
}
This class doesn’t do anything except hold data. But you can create an object like this:
DataOnly data = new DataOnly();
You can assign values to the fields, but you must first know how to refer to a member of an object. This is accomplished by stating the name of the object reference, followed by a period (dot), followed by the name of the member inside the object:
objectReference.member
For example:
data.i = 47;
data.d = 1.1;
data.b = false;
It is also possible that your object might contain other objects that contain data you’d like to modify. For this, you just keep “connecting the dots.” For example:
myPlane.leftTank.capacity = 100;
The DataOnly class cannot do much of anything except hold data, because it has no methods. To understand how those work, you must first understand arguments and return values, which will be described shortly.
Default values for primitive members
When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it:
The default values are only what Java guarantees when the variable is used as a member of a class. This ensures that member variables of primitive types will always be initialized (something C++ doesn’t do), reducing a source of bugs. However, this initial value may not be correct or even legal for the program you are writing. It’s best to always explicitly initialize your variables.
This guarantee doesn’t apply to local variables—those that are not fields of a class. Thus, if within a method definition you have:
int x;
Then x will get some arbitrary value (as in C and C++); it will not automatically be initialized to zero. You are responsible for assigning an appropriate value before you use x. If you forget, Java definitely improves on C++: You get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.)
Methods, arguments,
and return values
In many languages (like C and C++), the term function is used to describe a named subroutine. The term that is more commonly used in Java is method, as in “a way to do something.” If you want, you can continue thinking in terms of functions. It’s really only a syntactic difference, but this book follows the common Java usage of the term “method.”
Methods in Java determine the messages an object can receive. The fundamental parts of a method are the name, the arguments, the return type, and the body. Here is the basic form:
ReturnType methodName( /* Argument list */ ) {
/* Method body */
}
The return type describes the value that comes back from the method after you call it. The argument list gives the types and names for the information that you want to pass into the method. The method name and argument list (which is called the signature of the method) uniquely identify that method.
Methods in Java can be created only as part of a class. A method can be called only for an object,3 and that object must be able to perform that method call. If you try to call the wrong method for an object, you’ll get an error message at compile time. You call a method for an object by naming the object followed by a period (dot), followed by the name of the method and its argument list, like this:
objectName.methodName(arg1, arg2, arg3);
For example, suppose you have a method f( ) that takes no arguments and returns a value of type int. Then, if you have an object called a for which f( ) can be called, you can say this:
int x = a.f();
The type of the return value must be compatible with the type of x. This act of calling a method is commonly referred to as sending a message to an object. In the preceding example, the message is f( ) and the object is a. Object-oriented programming is often summarized as simply “sending messages to objects.” JAVA编程思想英文参考文献和翻译(5):http://www.751com.cn/fanyi/lunwen_71.html
------分隔线----------------------------
推荐内容