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

ค่าเริ่มต้น

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

    ลำดับตอนที่ #9 : Java III - The Basics of Classes

    • เนื้อหาตอนนี้เปิดให้อ่าน
    • 65
      0
      29 ธ.ค. 61

    Using Classes

    Classes

              Now let's learn about classes. Up to this point, we've split up the main method of the Main class into smaller methods. As a result, we have too many methods in the Main class! Next, we'll learn how to split these methods into different classes.


    Using Multiple Classes

              A class is a component that groups smaller components (methods) together. We've worked with methods that share the same class (Main class) with the main method. Now we will learn how to use methods in other classes.


    Calling Methods of Other Classes

              Below, there are 2 classes, Main and Person, and Person.hello() is called in the main method of the Main class. Using the syntax className.methodName(), you can call methods of other classes. You'll learn more about this later so let's move on to the exercise now!



    Class Definitions

    Defining Classes

              Define a class in the following way: class className. As shown in the example below, class names should be capitalized and the file name should be the same as the class name (e.g. className.java). Be sure to remember these naming conventions for Java classes.


    Creating a Person Class

              By creating the Person class and moving all the code except for the main method from the Main class, we can give the execution role to the Main class and the logic role to the Person class.


    Calling Person Class Methods

              Let's look at how to call a method of the Person class from the main method of the Main class in the example below!


    Supplement for Classes

              When you execute a class, Java will always look for the main method. If a class doesn't have a main method, it cannot be executed. It has to be called from another class. In our example, the Main class has a main method but the Person class doesn't, so only the Main class can be executed.



    Using Libraries

    External Libraries

              We can use classes made by other programmers in Java. Such classes are called external libraries and they are available after you import them into your program. You don't have to create everything by yourself from scratch! We can expand the range of development by using convenient methods created by engineers all over the world.


    Importing Libraries

              Use import to load external libraries into your program. For example, there's a class (library) called Math that contains mathematical methods. Above a class definition, you can import the Math class as follows: import java.lang.Math. Note that java.lang refers to the location of the Math class.


    Math Class Methods

              External libraries make it easy for us to implement programs. For example, the max method of the Math class returns the maximum of the two arguments. In the exercise, use the round method to return a numerical value rounded to the nearest integer. To call the round method, use the syntax: Math.round(argument).



    Receiving Input

    Automatically Imported Libraries

              Some classes, like java.lang.Math are so fundamental to the Java programming language that they actually get imported automatically. (All the java.lang libraries are imported by default.)


    Console Input and Scanner

              So far, we have only learned about output to the console. Next, we'll learn how to receive input from the console using a library called Scanner. To import Scanner, write: import java.util.Scanner.


    Receiving Strings Using Scanner

              Let's look at how to use Scanner. First, we must initialize a new Scanner instance and assign it to a variable called scanner. Then, we can use scanner.next() to get String inputs from the console (be careful, it's not Scanner.next()).


    Flow of Scanner

              When scanner.next() is called, the program stops executing and waits for some value to be entered. As shown below, scanner.next() gets some input from the console and assigns it to the variable name. (System.out.print is used to print a value without starting a new line.)


    Receiving Numerical Inputs

              There are two methods used to get numerical input using Scanner: the nextInt method for integers and the nextDouble method for decimals.





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

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

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

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

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

    ความคิดเห็น

    ×