ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Java

    ลำดับตอนที่ #13 : Java IV - Introduce Yourself with Java

    • อัปเดตล่าสุด 29 ธ.ค. 61


    Overview

    Improving the Self introduction Program

              So far, we've completed learning about the very foundation of object-oriented programming. From here, we will use this knowledge to implement the self-introduction program we created in Java III. Let's check the desired outcome first.



    Fields and Constructors

    Declaring Instance Fields

              Let's use instances of the Person class for the self-introduction program. First, we'll declare an instance field. See the new information below—for development, we also write out a list of information to give to the instance. Real-world objects have all sorts of properties, but software objects only need the minimum information.


    Making the Constructor

              Let's make a constructor so that the program can set values to instance fields when we create a new instance.



    Instance Methods

    Defining Instance Methods

              Next, let's learn how to define instance methods. Recall that we created the fullName and bmi methods in Java III. We'll use them as the instance methods. Later, we'll make the printData method (we won’t use the isHealthy method from Java III in this exercise).


    Changing Other Methods to Instance Methods

              Compare the fullName method from Java III with the new method we'll make here. In Java III, we passed a value ​​as an argument. Now, we enable the instance method to get a value of its own instance field using this, so we don't have to use an argument. The same is true for the bmi method.

    Calling Other Instance Methods

              Let's define an instance method called printData that will allow the instance to print its own data to the console. The printData method calls other methods including the fullName and bmi methods. We can call the other instance methods using this because it points to the specific instance directly.



    Class Fields

    Declaring Class Fields

              We've learned about instance fields that belong to particular instances. Now we'll learn about class fields which belong to a class. We declare a class field as follows: public static dataType variableName (note that we use static here).


    Class Field Example

              In order to count how many instances we've created so far, we can use a field called count, which keeps track of the number of instances being created. Since it's sufficient for just the Person class itself to have the count field (it's not needed for each instance separately), we'll make the count field a class field.


    Setting Initial Values for Fields

              We can give the count field an initial value at the time of declaration. Let's set the initial value for the count field to 0 since it keeps track of the number of instances being created (see the example below).


    Accessing Class Fields

              We can access a class field as follows: ClassName.classFieldName. On the right, you can see the constructor adding 1 to count every time a new instance is created. Simultaneously, the value of count on the left is being updated.



    Class Methods

    Methods that Belong to a Class

              Like class fields, some methods belong to classes. That kind of method is called a class method. We define class methods like so: public static returnType methodName().


    Calling Class Methods

              All the methods we learned in Java III began with static. Those were all class methods. To call a class method write: ClassName.methodName(). We use this syntax because class methods belong to a class. So, we can call class methods without creating an instance.



    Constructor Overloading

    Adding a Middle Name

              Let’s take another look at instance fields. Now we want the instance to have a field called middleName. Though it's easy to declare the middleName field, we need to be careful. If we just add middleName to the constructor arguments, there will be an error when an instance (person) is created without a value for middleName.


    Overloading Constructors

              To solve this issue, we can overload the constructors. Overloading allows you to define methods with the same name if they have a unique list of arguments (Java III). If we make two constructors—one accepts the middleName as an argument and the other doesn't—Java will be able to call the appropriate constructor for the arguments passed.



    Calling Constructors

    Duplicate Code in Constructors

              In the previous section, we successfully overloaded the constructor. However, most of the content of the constructors are the same. Duplicate code is bad because it makes it difficult for us to correct and modify. So what should we do?


    Calling Other Constructors

              To tackle this issue, we can use this() to call one constructor from another. this() is a special method for calling constructors. You can pass arguments between the () of this(). Note that we can only call this() at the beginning of the constructor.



    Rewriting the fullName Method

    Updating the fullName Method

              Now we know how to add the middleName field. However, middleName is currently not displayed in the fullName method (see the example on the left). Since some people don't have a middle name, we can't just combine the middleName value with the others. What should we do?


    Null

              What is in the middleName field when the value is not set in the first place? Actually, the middleName field has a default value called null. null is a special value that means "nothing" (note that null is not a string). Although it’s an unfamiliar term, keep in mind that null is an important concept in programming.


    Initial Values of Variables

              If we don't assign an initial value to a variable or a field when declaring it, Java assigns it a fixed default value. As shown in the table below, the default value depends on the data type of the variable. The middleName field has null because it is a String.


    Rewriting the fullName Method

              Let's make our program return different values in the fullName method depending on whether or not the middleName is null. Using if statements, we can allow the program to return a full name with a middle name only if the middleName field has a value.





    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน

    ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×