βοΈ Info.
- Java Class νμ΅
π List.
1. ν΄λμ€ μ μνλ λ°©λ²
2. κ°μ²΄ λ§λλ λ°©λ² (new ν€μλ μ΄ν΄νκΈ°)
3. λ©μλ μ μνλ λ°©λ²
4. μμ±μ μ μνλ λ°©λ²
5. this ν€μλ μ΄ν΄νκΈ°
βοΈ Content.
1. ν΄λμ€ μ μνλ λ°©λ²
- Class
- κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°(OOP, Object-Oriented Programming) μμ ν΅μ¬μΈ Objectλ₯Ό λ§λ€κΈ°μν μ€κ³λμ κ°μ κ°λ
- Class λ κ°μ²΄μ μνλ₯Ό λνλ΄λ Field μ κ°μ²΄μ νλμ λνλ΄λ Methodλ‘ κ΅¬μ±λλ€.
- Class μ μΈ λ°©λ²
public class StudyClass {
// Field
// Method
// Constructor
}
- Class, Field, Method μμ μ κ·Όμ μ΄λ₯Ό μ¬μ©ν¨μ λ³Όμ μλλ° [μ κ·Όμ μ΄] λ OOPμμμ μΊ‘μνλ‘ λ΄λΆ λ°μ΄ν° λλ λ©μλλ€μ μ κ·Όμ μ μ΄νλ κ²μ΄λ€.
- κ°κ°μ μ κ·Ό μ μ΄μ μ’ λ₯μ μ¬μ©λ²μλ μλμ κ°λ€.
- μ κ·Ό νμ© ν
μ κ·Ό μ μ΄μ | κ°μ ν΄λμ€ | κ°μ ν¨ν€μ§ | μμ ν΄λμ€ | μ 체 |
public | O | O | O | O |
protected | O | O | O | X |
default | O | O | X | X |
private | O | X | X | X |
- μ κ·Ό λμ ν
μ κ·Ό μ μ΄μ | ν΄λμ€ | νλ | μμ±μ | λ©μλ |
public | O | O | O | O |
protected | X | O | O | O |
default | O | O | O | O |
private | X | O | O | O |
2. κ°μ²΄ λ§λλ λ°©λ² (new ν€μλ μ΄ν΄νκΈ°)
public static void main(String[] args) {
StudyClass studyClass; // κ°μ²΄ μμ±
studyClass = new StudyClass(); // κ°μ²΄μ μΈμ€ν΄μ€ν
}
- StudyClass ν΄λμ€μ studyClassλΌλ κ°μ²΄λ₯Ό μμ±νλ€.
- new μ°μ°μλ₯Ό ν΅ν΄ studyClass κ°μ²΄λ₯Ό λ©λͺ¨λ¦¬μ ν λΉνλ©΄μ studyClass κ°μ²΄λ StudyClass ν΄λμ€μ μΈμ€ν΄μ€κ° λμλ€.
3. λ©μλ μ μνλ λ°©λ²
- [μ κ·Όμ μ΄] [λ°ννμ] [Method μ΄λ¦](맀κ°λ³μ){ μ½λꡬνλΆ(return [λ°ννμ κ°]) }
public class StudyClass {
// Field
// Constructor
/**
* Method
*/
public void method() {
int level = 0; // local variable
}
String method2(String name){
return name + " μ
λλ€.";
}
}
- λ©μλ μ€λ²λ‘λ©(overloading)
- λ©μλ μκ·Έλμ³ ( Method Signature)
- λ©μλ μ΄λ¦ + 맀κ°λ³μ 리μ€νΈμ μ‘°ν©
- λ©μλμ μ΄λ¦μ κ°μ§λ§ 맀κ°λ³μ 리μ€νΈμ μ‘°ν©μ΄ λ€λ₯Έ κ²½μ°
- μ¦, λ©μλμ μκ·Έλμ³κ° λ€λ₯Έ κ²½μ°μ΄λ€.
- λ©μλ μκ·Έλμ³ ( Method Signature)
public class StudyClass{
public void StudyMethod() {
System.out.println("StudyClass μ
λλ€.");
}
public void StudyMethod(String name){
System.out.println("StudyClass OverLoading : " + name);
}
public static void main(String[] args) {
StudyClass studyClass = new StudyClass();
studyClass.StudyMethod();
studyClass.StudyMethod("Test");
}
}
- λ©μλ μ€λ²λΌμ΄λ©(overriding)
- μμκ΄κ³μ μλ λΆλͺ¨ ν΄λμ€λ‘ λΆν° μ μλ λ©μλλ₯Ό μ¬μ μ νλ κ²
class Study{
public void StudyMethod(){
System.out.println("Study μ
λλ€.");
}
}
public class StudyClass extends Study{
@Override
public void StudyMethod() {
System.out.println("StudyClass μ
λλ€.");
}
public static void main(String[] args) {
Study study = new Study();
StudyClass studyClass = new StudyClass();
study.StudyMethod();
studyClass.StudyMethod();
}
}
4. μμ±μ μ μνλ λ°©λ²
- ν΄λμ€μ μ‘΄μ¬νλ©° νλμ΄μ κ°μ§κ³ μλ€.
- μμ±μλ₯Ό μ μ νμ§μμ κ²½μ° μλ° μ»΄νμΌλ¬κ° μλ¬΄λ° λ§€κ°λ³μκ° μμΌλ©° ꡬνλΆ λ΄μ©μ΄ μλ κΈ°λ³Έ μμ±μλ₯Ό μ 곡νλ€.
- λ°λμ ν΄λμ€μ μ΄λ¦κ³Ό κ°μμΌ νλ€.
- λ°νκ°μ΄ μμ§λ§, λ°ν νμ μ voidνμΌλ‘ μ μΈνμ§ μλλ€.
- overloding μ΄ κ°λ₯νλ€.
public class StudyClass extends Study{
/**
* Method(Constructor)
*/
public StudyClass() {
}
public StudyClass(String name) {
this.name = name;
}
public StudyClass(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
}
5. this ν€μλ μ΄ν΄νκΈ°
- μΈμ€ν΄μ€κ° λ°λ‘ μκΈ°μμ μ μ°Έμ‘°νλλ° μ¬μ©νλ λ³μ
- μΈμ€ν΄μ€μ μ£Όμλ₯Ό κ°λ¦¬ν€κ³ μλ€.
- μμ±μ λ΄μμ λ€λ₯Έ μμ±μλ₯Ό νΈμΆν λ μ¬μ© κ°λ₯νλ€.
public StudyClass(String name) {
this.name = name;
}
public StudyClass(String name, int age, int grade) {
this(name); //StudyClass(String name) μμ±μ νΈμΆ
this.age = age;
this.grade = grade;
}
βοΈ Refer.
ν΄λμ€ μ μνλ λ°©λ²
- http://www.tcpschool.com/java/java_class_intro
- http://blog.naver.com/PostView.nhn?blogId=mals93&logNo=220631298165&parentCategoryNo=&categoryNo=15&viewDate=&isShowPopularPosts=false&from=postList
κ°μ²΄ λ§λλ λ°©λ² (new ν€μλ μ΄ν΄νκΈ°)
λ©μλ μ μνλ λ°©λ²
- http://www.tcpschool.com/java/java_usingMethod_overloading
- http://www.tcpschool.com/java/java_inheritance_overriding
μμ±μ μ μνλ λ°©λ²
- http://www.tcpschool.com/java/java_methodConstructor_constructor
this ν€μλ μ΄ν΄νκΈ°
- http://www.tcpschool.com/java/java_methodConstructor_this
'Live-Study' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[7μ£Όμ°¨] ν¨ν€μ§ (0) | 2021.02.19 |
---|---|
[6μ£Όμ°¨] μμ (0) | 2021.02.19 |
[4μ£Όμ°¨] μ μ΄λ¬Έ (0) | 2020.12.31 |
[3μ£Όμ°¨] μ°μ°μ (0) | 2020.11.23 |
[2μ£Όμ°¨] μλ° λ°μ΄ν° νμ , λ³μ κ·Έλ¦¬κ³ λ°°μ΄ (0) | 2020.11.20 |