Search
Duplicate

Access, Encapsulation and Static Methods

νƒœκ·Έ
μ ‘κ·Όμ œν•œ
μΊ‘μŠν™”
Scope
ꡬ뢄
코데카데미

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
볡사