Access, Encapsulation, and Scope
The public and private Keywords
μλ°μμλ public κ³Ό private μ΄ ν΄λμ€, μΈμ€ν΄μ€ λ³μ, constructors κ·Έλ¦¬κ³ λ©μλμ λν μ κ·Όμ μ ννλ λ° μ°μΈλ€. private μ κ²½μ° μμ±μμ μ μΈλ ν΄λμ€ λ§μ΄ μ κ·Όμ΄ κ°λ₯νλ©°, public μ κ²½μ° μ΄λ ν ν΄λμ€λ μ κ·Όμ΄ κ°λ₯νλ€.
Encapsulation
μΊ‘μνλ λ€λ₯Έ ν΄λμ€λ€λ‘λΆν° ꡬνμ κ΄ν μΈλΆμ λΆλ₯Ό μ¨κΈ°κΈ° μν΄μ μ¬μ©νλ€. μΈμ€ν΄μ€ λ³μμ κ²½μ° private μ ν΅ν΄ μΊ‘μν λλ€.
Accessor and Mutator Methods
Accessor Methods
λͺ¨λ μΈμ€ν΄μ€ λ³μκ° private μΌλ‘ μ μλμ΄ μμΌλ λ€λ₯Έ ν΄λμ€κ° μ΄ λ³μμ μ κ·Όμ ν΄μΌ νλ μν©μ΄λΌκ³ κ°μ ν΄λ³΄μ. μ΄λ―Έ private μΌλ‘ μ μ λμ΄ μμ΄μ μ νν λ³μλͺ
μ ν΅ν΄ μ κ·Ό ν μλ μκ² μ§λ§ accessor λ©μλλ₯Ό μ΄μ© νλ€λ©΄ μ νν λ³μλͺ
μ μμ§ λͺ»ν΄λ μ κ·Όμ΄ κ°λ₯νλ€. accessor λ©μλλ getter λ©μλλ‘λ μ μλ €μ Έ μλ€. μλ μμλ₯Ό 보면 λ μ μ΄ν΄ν μ μμ κ²μ΄λ€
[μμ μ½λ]
public class Dog{
private String name;
//Other methods and constructors
public String getName() {
return name;
}
}
Java
볡μ¬
λΉλ‘ name μ΄ private μΌλ‘ μ μλμ΄ μμ§λ§ λ€λ₯Έ ν΄λμ€μμ public λ©μλμΈ getName() λ©μλλ₯Ό νΈμΆν¨μΌλ‘μ¨ λͺ©νμΈ name μ΄λΌλ μΈμ€ν΄μ€ λ³μμ μ κ·Ό ν μ μλ€. Accessor λ©μλλ νμ public μ΄μ¬μΌ νλ©° μ κ·Όνλ €λ μΈμ€ν΄μ€ λ³μμ νμ
κ³Ό νμ κ°μ νμ
μ λ°νν΄μΌ νλ€.
Mutator Methods
private μΈμ€ν΄μ€ λ³μλ€μ accessor method λ§κ³ λ mutator method λ₯Ό κ°μ§κΈ°λ νλ€. setter λΌκ³ λ μλ €μ Έ μλ μ΄ λ©μλλ₯Ό μ¬μ©νλ©΄ λ€λ₯Έ ν΄λμ€κ° μ κ·Όνλ €λ μΈμ€ν΄μ€ λ³μμ μ μ₯λ κ°μ μ¬μ€μ ν μ μλ€.
[μμ μ½λ]
public class Dog{
private String name;
//Other methods and constructors
public void setName(String newName) {
name = newName;
}
public static void main(String[] args){
Dog myDog = new Dog("Cujo");
myDog.setName("Lassie");
}
}
Java
볡μ¬
setter μ κ²½μ° λλΆλΆ void λ©μλλΌ μ무κ²λ λ°ννμ§ μλλ€. λ¨μ§ μ‘΄μ¬νλ λ³μμ κ°μ μ¬μ€μ ν λΏμ΄λ€. λν λ°κΎΈλ €λ λ³μμ κ°μ νμ
μ νλΌλ―Έν°λ₯Ό νκ°λ§ κ°μ§κ³ μμ΄μΌ νλ€.
Scope
Local Variables
public μ΄λ private κ°μ μ κ·Ό μ νμλ€ μΈμλ λ³μμ λ²μ λν λ³μμ μ κ·Ό ν μ μλ λΆλΆμ κ²°μ νλ€.
λ³μμ λ²μλ μ΄λμ λ³μκ° μ μΈλμλμ§μ μν΄ κ²°μ λλ€. λ§μ½ λ³μκ° ν΄λμ€ λ΄λΆμλ μ μΈ λμμ§λ§ κ·Έ μ μΈλμ΄μλ μ₯μκ° λ©μλλ constructor λ°μ΄λΌλ©΄ λͺ¨λ λ©μλμ constructor κ° κ·Έ λ³μμ λ²μ λ΄μ μ‘΄μ¬νλ€. (μ λΆ κ·Έ λ³μλ₯Ό μΈ μ μλ€λ λ»μ΄λ€.) μλ μμλ₯Ό 보μ.
[μμ μ½λ]
class Dog{
public String name;
public int age;
public int weight;
public Dog(){
name = "Winston";
age = 8;
weight = 30;
}
public void speak(){
System.out.println("My name is " + name);
}
}
Java
볡μ¬
μ΄ μμλ₯Ό 보면 Dog ν΄λμ€ λ΄λΆμ constructor μ λ©μλκ° μΈμ€ν΄μ€ λ³μμΈ name μ΄λ age, weight μ μ°λ κ²μ μ μ μλ€.
νμ§λ§ μμ μμμ½λμ λ€λ₯΄κ² λ³μκ° λ©μλ μμ μ μΈλμ΄ μλ€λ©΄, κ·Έ λ³μλ μ€μ§ κ·Έ λ©μλ μμμλ§ μ¬μ©μ΄ κ°λ₯νλ€. μ΄λ νλΌλ―Έν°μλ ν΄λΉλλ μ¬νμ΄λ€. μ΄λ¬ν λ³μλ€μ local λ³μλΌκ³ λΆλ¦°λ€. μ§μ λ³μλ₯Ό μ μΈν λλ public μ΄λ private μ μ¬μ©νμ§ μλλ€λ κ²μ λͺ
μ¬νμ.
λ²μμ λν κ°λ
μ 쑰건문μ΄λ λ°λ³΅λ¬Έμλ λκ°μ΄ μ μ©μ΄ λλ€.
The this Keyword
μλ°μμ this ν€μλλ
(1) μΈμ€ν΄μ€ λ³μλ₯Ό μ°Έμ‘°νλ―λ‘μ¨ μΈμ€ν΄μ€ λ³μμ μ§μ λ³μλ₯Ό ꡬλΆνλ€.
(2) λ©μλλ₯Ό νΈμΆνλ λ° μ°μΈλ€.
(3) νλΌλ―Έν°μ κ°μΌλ‘ μ°μΈλ€.
[μμ μ½λ] (1)
public class Dog{
public String name;
public void speak(String name){
// Prints the instance variable named name
System.out.println(this.name);
// Prints the local variable named name
System.out.println(name);
}
}
Java
볡μ¬
κ°μ κ°μ κ°μ§λ κ² λΌλ¦¬ λ³Όλ체μ μ΄νλ¦μ²΄λ‘ νμνλ€.
[μμμ½λ] (2)
public class ExampleClass{
public void exampleMethodOne(){
System.out.println("Hello");
}
public void exampleMethodTwo(){
//Calling a method using this.
this.exampleMethodOne();
System.out.println("There");
}
}
Java
볡μ¬
exampleMethodOne μ νΈμΆνκΈ° μν΄ this λ₯Ό μ¬μ©νλ€.
private λ©μλλ₯Ό this λ‘ νΈμΆν μ μμκΉ?
λλΆλΆμ private λ©μλλ helper method μ΄λ€. μ¦, λ€λ₯Έ νΉμ λ ν° μμ€μ λ©μ€λλ₯Ό μ¬μ©νκΈ° μν λ©μλμ΄λ€. μλ μμλ₯Ό 보μ.
Bank.java (μ€μ μ€νμ΄ μΌμ΄λλ ν΄λμ€)
public class Bank{
private CheckingAccount accountOne;
private CheckingAccount accountTwo;
public Bank(){
this.accountOne = new CheckingAccount("Zeus", 100, "1");
this.accountTwo = new CheckingAccount("Hades", 200, "2");
}
public static void main(String[] args){
Bank bankOfGods = new Bank();
bankOfGods.accountOne.getAccountInformation();
bankOfGods.accountOne.calculateNextMonthInterest();
}
}
Java
볡μ¬
CheckingAccount.java
public class CheckingAccount{
private String name;
private int balance;
private String id;
private double interestRate;
public CheckingAccount(String inputName, int inputBalance, String inputId){
this.name = inputName;
this.balance = inputBalance;
this.id = inputId;
this.interestRate = 0.02;
}
public void getAccountInformation(){
System.out.println("Money in account: " + this.getBalance());
System.out.println("Next Month's Interest: " + this.calculateNextMonthInterest());
}
private int getBalance(){
return this.balance;
}
// caclulateNextMonthInterest() is helper method
private double calculateNextMonthInterest() {
return balance * interestRate;
}
}
Java
볡μ¬
calculateNextMonthInterest() λ©μλλ helper λ©μλμ΄λ€. λλ¬Έμ Bank.java μμ μ΄ λ©μλλ₯Ό μ§μ μ μΌλ‘ νΈμΆνκΈ° 보λ€λ (μ΄μ°¨νΌ private μ΄λΌ νΈμΆν μ μλ€.), public λ©μλμΈ getAccountInformation() λ©μλμμ νΈμΆν ν 볡μ‘ν μΌλ€μ μννκ³ , κ·Έ λ€μ getAccountInformation() λ©μλλ₯Ό νΈμΆνλ€. this κ° μΈμ μ°μ΄λμ§ μ½λμμ μ§μ 보면 μ΄ν΄κ° μ¬μΈ κ²μ΄λ€
[μμμ½λ](3)
public void pairWithOtherComputer(Computer other){
// Code for method that uses the parameter other
}
public void setUpConnection(){
// We use "this" to call the method and also pass "this" to the method so it can be used in that method
this.pairWithOtherComputer(this);
}
Java
볡μ¬
Static Methods of the Math Class
The Math Class
μλ°μ Math Class λ java.lang ν¨ν€μ§μ ν λΆλΆμ΄λ€. μν κ³μ°κ³Ό κ΄λ ¨λ λ€μν λ©μλλ₯Ό ν¬ν¨νκ³ μμΌλ©° static λ©μλμ non-static λ©μλκ° μ΄λ»κ² λ€λ₯Έμ§ μ μ μλ κΈ°νλ μ 곡νλ€.
Calling Static Methods
Math ν΄λμ€ μμ μλ λͺ¨λ λ©μλλ static ννμ΄λ€. μ΄λ μ°λ¦¬κ° ν΄λμ€μ κ°μ²΄λ₯Ό μμ±νμ§ μμλ νΈμΆμ΄ κ°λ₯νλ€λ λ»μ΄λ€. λ§μ½ Math ν΄λμ€λ₯Ό μ°Έμ‘°νκ³ μΆλ€λ©΄ Math.[λ©μλλͺ
] μ μ¬μ©νλ©΄ λλ€. μ΄λ static java.lang.Math.*; μ import ν΄μΌ νλ€λκ² μμ§λ§μ
Example of Math methods
int abs(int x)
x λΌλ μ«μκ° 0 μμ μΌλ§λ λ©λ¦¬ μλμ§λ₯Ό μλ €μ€λ€.
System.out.println(Math.abs(5)); // Prints: 5
System.out.println(Math.abs(-5)); // Prints: 5
Java
볡μ¬
double abs(double x)
λλΈνμ λ³μκ°μ μ λκ°μΌλ‘ λ°κΏμ€λ€.
System.out.println(Math.abs(5.0)); // Prints: 5.0
System.out.println(Math.abs(-5.0)); // Prints: 5.0
Java
볡μ¬
double pow(double base, double exponent)
base μ λ€μ΄κ°λ μ«μλ₯Ό exponent μ λ€μ΄κ°λ μ«μλ§νΌ κ³±ν΄μ€λ€. μ¦ κ±°λμ κ³±μ μννλ€κ³ ν μ μλ€.
double x = Math.pow(5, 3);
System.out.println(x); // Prints: 125.0
Java
볡μ¬
double sqrt(double x)
x μ μμ μ κ³±κ·Όμ μΆλ ₯ν΄μ€λ€.
double x = Math.sqrt(49);
System.out.println(x); // Prints: 7.0
double y = Math.sqrt(52);
System.out.println(y); // Prints: 7.211102550927978
Java
볡μ¬
double random()
0.0 λ³΄λ€ κ°κ±°λ ν¬κ³ 1.0 보λ€λ μμ λλΈνμ κ°μ λ°ννλ€.
// Random double value between 0 and 9
double a = Math.random() * 10;
Java
볡μ¬
μ΄ κ°μ νΉμ μλ₯Ό κ³±νλ©΄μ λ°ννλ κ°λ€μ λ²μλ₯Ό λ°κΏ μ μλ€.
// Random int value between 0 and 9
int b = (int)(Math.random() * 10);
Java
볡μ¬
λ§μ½ μμλ²μλ₯Ό λ°κΎΈκ³ μΆλ€λ©΄ μ΄μ ννμμ 1μ λν΄μ£Όλ©΄ λλ€.
// Random int value between 1 and 10
int c = (int)(Math.random() * 10) + 1;
Java
볡μ¬
μ΄λ₯Ό ꡬ문μΌλ‘ μ μΌλ©΄ λ€μκ³Ό κ°λ€.
(Math.random()*(maxValue - minValue + 1)) + minValue;
Static Variables and Methods
Static Variables
static λ©μλμ λ³μλ ν΄λμ€μ κ°μ²΄μ μλ ν΄λμ€ μ 체μ μ°κ΄λμ΄ μλ€. λλ¬Έμ static λ©μλ μ²λΌ, static λ³μμλ [ν΄λμ€λͺ
].[ν΄λμ€μ static λ³μλͺ
] μ μ¬μ©νμ¬ μ κ·Ό ν μ μλ€. μλ μμλ₯Ό 보μ.
[μμμ½λ]
public class ATM{
// Static variables
public static int totalMoney = 0;
public static int numATMs = 0;
// A static method
public static void averageMoney(){
System.out.println(totalMoney / numATMs);
}
public static void main(String[] args){
//Accessing a static variable
System.out.println("Total number of ATMs: " + ATM.numATMs);
// Calling a static method
ATM.averageMoney();
}
}
Java
볡μ¬