菜单
  

    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.”
  1. 上一篇:PLC数控加工工业机械手英文文献和翻译
  2. 下一篇:机械电子轧机的空间振动英文文献和翻译
  1. 集成计算机英文文献和中文翻译

  2. 电站锅炉暖风器设计任务书

  3. java+mysql车辆管理系统的设计+源代码

  4. 中考体育项目与体育教学合理结合的研究

  5. 杂拟谷盗体内共生菌沃尔...

  6. 河岸冲刷和泥沙淤积的监测国内外研究现状

  7. 当代大学生慈善意识研究+文献综述

  8. 十二层带中心支撑钢结构...

  9. 乳业同业并购式全产业链...

  10. 大众媒体对公共政策制定的影响

  11. 酸性水汽提装置总汽提塔设计+CAD图纸

  

About

751论文网手机版...

主页:http://www.751com.cn

关闭返回