-
Class : MethodJava 2022. 2. 4. 23:52
자바의 클래스에서, 메소드에 대한 개념 및 문법을 정리하였습니다.
< Introduction >
Objects have state(information) and method(behavior).
We've seen how to give objects state through instance fields. We're going to learn how to create object behavior using methods.
Methods are repeatable, modular blocks of code used to accomplish specific tasks.+. procedural abstraction : knowing what a method does, but not how it accomplishes it.
If we were to share a program with another person, they wouldn't have to understand how the program worked.
All they would need to know is that if they called the program.
< Defining Methods >
To define object's method.
Every method has its own unique method signature which is comprised of the method’s name and its parameter type.< Syntax >
public void methodName() { // method's body }
- public means that other classes can access this method.
- void keyword means that there is no specific output from the method.Example)
public void checkBalance(){ System.out.println("Hello!"); System.out.println("Your balance is " + balance); }
< Calling Methods >
When we add a non-static method to a class, it becomes available to use on an object of that class.
In order to have our methods get executed, we must call the method on the object we created or call the method inside another method- To call a method, use dot operator . and parentheses ( ) after the method name.
Example)
class Car { String color; public Car(String carColor) { color = carColor; } public void startEngine() { System.out.println("Starting the car!"); System.out.println("Vroom!"); } public static void main(String[] args){ Car myFastCar = new Car("red"); // Call a method on an object myFastCar.startEngine(); System.out.println("That was one fast car!"); } } // Starting the car! // Vroom! // That was one fast car!
+. methods are ignored by the compiler unless they are being called.
< Scope >
We can’t access variables that are declared inside a curly braces { } in code that is outside the scope of that curly braces.
Example)
class Car { String color; int milesDriven; public Car(String carColor) { color = carColor; milesDriven = 0; } public void drive() { String message = "Miles driven: " + milesDriven; System.out.println(message); } public static void main(String[] args){ Car myFastCar = new Car("red"); myFastCar.drive(); } }
: The variable message, which is declared and initialized inside of drive, cannot be used inside of main()! It only exists within the scope of the drive() method.
However, milesDriven, which is declared at the top of the class, can be used inside all methods in the class, since it is in the scope of the whole class.
< Adding Parameters >
Similar to how we added parameters to constructors, we can customize all other methods to accept parameters.
Example)
class Car { String color; public Car(String carColor) { color = carColor; } public void startRadio(double stationNum, String stationName) { System.out.println("Turning on the radio to " + stationNum + ", " + stationName + "!"); System.out.println("Enjoy!"); } public static void main(String[] args){ Car myCar = new Car("red"); myCar.startRadio(103.7, "Meditation Station"); } } // Turning on the radio to 103.7, Meditation Station! // Enjoy!
+. Multiple Methods
Through method overloading, our Java programs can contain multiple methods with the same name as long as each method’s parameter list is unique.
Example)
// Method 1 public void startRadio(double stationNum, String stationName) { System.out.println("Turning on the radio to " + stationNum + ", " + station + "!"); System.out.println("Enjoy!"); } // Method 2 public void startRadio(double stationNum) { System.out.println("Turning on the radio to " + stationNum + "!"); } public static void main(String[] args){ Car myCar = new Car("red"); // Calls the first startRadio() method myCar.startRadio(103.7, "Meditation Station"); // Calls the second startRadio() method myCar.startRadio(98.2); }
< Returns >
To pass back information from the function call, use return keyword.
Any code that exists after the return statement in a function is ignored.< Syntax >
public typeOfValue methodName() { // method's body }
- Replace the typeOfValue to type keyword to signify that the return value is what type. such as int, double, etc.
Example)
public int numberOfTires() { int tires = 4; return tires; } // return 4
+. Method's return value's type
When we return a primitive value, a copy of the value is returned; however, when we return an reference value (like object), we return a reference to the object instead of a copy of it.
Example)
class CarLot { Car carInLot; public CarLot(Car givenCar) { carInLot = givenCar; } public Car returnACar() { return carInLot; } public static void main(String[] args) { Car myCar = new Car("red", 70); System.out.println(myCar); CarLot myCarLot = new CarLot(myCar); System.out.println(myCarLot.returnACar()); } } // Car@2f333739 // Car@2f333739
< The toString() Method >
When print object, to return descriptive String instead of location in memory.
Example)
class Car { String color; public Car(String carColor) { color = carColor; } public static void main(String[] args){ Car myCar = new Car("red"); System.out.println(myCar); } public String toString(){ return "This is a " + color + " car!"; } } // print : This is a red car! // not print : Car@2aae9190
'Java' 카테고리의 다른 글
Conditional Operators (0) 2022.02.05 Conditional (0) 2022.02.05 Class : Introduction (0) 2022.02.03 Variables : Manipulating (0) 2022.02.01 Variables (0) 2022.01.31