JAVA TIL(4)

YulHee KimΒ·2021λ…„ 8μ›” 5일
0

Java

λͺ©λ‘ 보기
4/8
post-thumbnail
post-custom-banner

πŸ“š ν•˜λ£¨μ— ν•œλ²ˆμ”© 배운 것과 지식을 μ •λ¦¬ν•˜λ €κ³  μ“°λŠ” κΈ€

πŸ“— Java 객체

πŸ“Œ static

보톡 λ³€μˆ˜μ˜ static ν‚€μ›Œλ“œλŠ” ν”„λ‘œκ·Έλž˜λ° μ‹œ λ©”λͺ¨λ¦¬μ˜ νš¨μœ¨λ³΄λ‹€λŠ” κ³΅μœ ν•˜κΈ° μœ„ν•œ μš©λ„λ‘œ 훨씬 더 많이 μ‚¬μš©ν•˜κ²Œ λœλ‹€.

πŸ“Œ 데이터 은닉(getter, setter)

//Student Class
private String name;
private int score;

public Student(String n, int s) {
 this.name = n;
 this.score = s;
}

public void getInfo() {
System.out.println(" -- getInfo() --");
System.out.println(" name :" + name);
System.out.println(" score :" + score);
}

public String getName() {
	return name;
}
public int getScore() {
	return score;
}
public void setScore(int score) {
	if(score > 50) this.score = score;
}

//MainClass
{
Student student1 = new Student("홍길동", 90);
student1.getInfo();

student1.setScore(100);
student1.getInfo();
}

κ²°κ³Ό

---getInfo()---
name : 홍길동
score : 90
---getInfo()---
name : 홍길동
score : 100

πŸ“— Java 상속 및 클래슀

πŸ“Œ μƒμ†μ΄λž€?

λΆ€λͺ¨ 클래슀λ₯Ό 상속받은 μžμ‹ ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ 클래슀의 속성과 κΈ°λŠ₯도 μ΄μš©ν•  수 μžˆλ‹€.

  • μƒμ†μ˜ ν•„μš”μ„± : 기쑴의 κ²€μ¦λœ classλ₯Ό μ΄μš©ν•΄μ„œ λΉ λ₯΄κ³  μ‰½κ²Œ μƒˆλ‘œμš΄ classλ₯Ό λ§Œλ“€ 수 μžˆλ‹€.
  • extends ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄μ„œ 상속을 κ΅¬ν˜„
  • javaλŠ” 단일 상속 (ν•˜λ‚˜μ˜ 클라슀만 상속할 수 μžˆλ‹€)

λΆ€λͺ¨ 클래슀의 private μ ‘κ·Όμž

: μžμ‹ ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ 클래슀의 λͺ¨λ“  μžμ›μ„ μ‚¬μš©ν•  수 μžˆμ§€λ§Œ, private μ ‘κ·Όμžμ˜ 속성과 λ©”μ„œλ“œλŠ” μ‚¬μš©ν•  수 μ—†λ‹€.

πŸ“Œ μƒμ†μ˜ νŠΉμ§•

(1) λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ“œ (override)

: λΆ€λͺ¨ 클래슀의 κΈ°λŠ₯을 μžμ‹ ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ν•΄μ„œ μ‚¬μš©ν•œλ‹€.

public class ParentClass {

	public void makeJJajang() {
    		System.out.println(" -- makeJJajang() START --");
    	}	
  }
  
public class ChildClass extends ParentClass {
	
    	@Override
	public void makeJJajang() {
    		System.out.println(" -- more delicious makeJJajang() START --");
    	}	
  }
  
ChildClass child = new ChildClass();
child.makeJJajang();

// κ²°κ³Ό
// -- more delicious makeJJajang()

(2) μžλ£Œν˜•(νƒ€μž…)
: κΈ°λ³Έ μžλ£Œν˜•μ²˜λŸΌ ν΄λž˜μŠ€λ„ μžλ£Œν˜•μ΄λ‹€.

(3) Object클래슀
: λͺ¨λ“  클래슀의 μ΅œμƒμœ„ ν΄λž˜μŠ€λŠ” Object Class이닀.

(4) super 클래슀
: μƒμœ„ 클래슀λ₯Ό ν˜ΈμΆœν•  λ•Œ super ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•œλ‹€.

public class ParentClass {
	int openYear = 1995;
    
	public void makeJJajang() {
    	    System.out.println(" -- makeJJajang() START --");
    	}	
  }
  
public class ChildClass extends ParentClass {
	int openYear = 2000;
    
	public void getOpenYear() {
    	    System.out.println("ChildClass's Open year : " + this.openYear);
            System.out.println("ParentClass's Open year : " + super.openYear);
  }
  
ChildClass c = new ChildClass();
c.getOpenYear();

// κ²°κ³Ό
// ChildClass's Open year : 2000
// ParentClass's Open year : 1995

πŸ“Œ 읡λͺ… 클래슀

: 이름이 μ—†λŠ” 클래슀둜 주둜 λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ ν•˜λŠ” λͺ©μ μœΌλ‘œ μ‚¬μš©λœλ‹€.
읡λͺ… ν΄λž˜μŠ€λŠ” μΈν„°νŽ˜μ΄μŠ€λ‚˜ μΆ”μƒν΄λž˜μŠ€μ—μ„œ 주둜 μ΄μš©λœλ‹€.

public class AnonymousClass {
	public AnonymousClass() {
    	    System.out.println("AnonymousClass constructor"); 
        }
        
    	public void method() {
            System.out.println(" -- AnonymousClass's method START == ");
        }
 }
      
// Main Class
new AnonymousClass() {

    @Override
    public void method() {
      System.out.println(" -- AnonymousClass's Override method START -- ");
    };
}.method();

// κ²°κ³Ό
// -- AnonymousClass's Override method START --

πŸ“Œ μΈν„°νŽ˜μ΄μŠ€

μΈν„°νŽ˜μ΄μŠ€λž€ ?

ν΄λž˜μŠ€μ™€ 달리 객체λ₯Ό 생성할 μˆ˜λŠ” μ—†μœΌλ©°, ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„ν•΄μ•Ό ν•˜λŠ” μž‘μ—… λͺ…μ„Έμ„œμ΄λ‹€.

- μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” 이유

: μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” λ§Žμ§€λ§Œ, κ°€μž₯ 큰 μ΄μœ λŠ” 객체가 λ‹€μ–‘ν•œ μžλ£Œν˜•(νƒ€μž…)을 κ°€μ§ˆ 수 있기 λ•Œλ¬Έμ΄λ‹€.

- μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„

: class λŒ€μ‹  interface ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©°, extend λŒ€μ‹  implements ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•œλ‹€.

// InterfaceA
public interface InterfaceA {
    public void funA();
}

//InterfaceB
public interface InterfaceB {
    public void funB();
}

//ImplementClass
public class ImplementClass implements InterfaceA, InterfaceB {

    @Override
    public void funA() { System.out.println("-- funA --"); }
    public void funB() { System.out.println("-- funB --"); }
}

- μž₯λ‚œκ° μΈν„°νŽ˜μ΄μŠ€

: interfaceλ₯Ό μ΄μš©ν•˜λ©΄ 객체가 λ‹€μ–‘ν•œ μžλ£Œν˜•(νƒ€μž…)을 κ°€μ§ˆ 수 μžˆλ‹€.

Toy robot = new ToyRobot();
Toy airplane = new ToyAirplane();

Toy toys[] = {robot, airplane};

for (int i = 0; i < toys.length; i++) {
   toys[i].walk();
   toys[i].run();
   toys[i].alarm();
   toys[i].light();
   
   System.out.println();
}

//κ²°κ³Ό
The robot can walk.
The robot can run.
The robot has no alarm function.
The robot has light function.

The airplane can not walk.
The airplane can not run.
The airplane has alarm function.
The airplane has no light function.

πŸ“Œ μΆ”μƒν΄λž˜μŠ€

μΆ”μƒν΄λž˜μŠ€λž€?

: 클래슀의 κ³΅ν†΅λœ 뢀뢄을 λ½‘μ•„μ„œ λ³„λ„μ˜ 클래슀(μΆ”μƒν΄λž˜μŠ€)둜 λ§Œλ“€μ–΄λ†“κ³ , 이것을 μƒμ†ν•΄μ„œ μ‚¬μš©ν•œλ‹€.

- abstract 클래슀의 νŠΉμ§•

  • λ©€λ²„λ³€μˆ˜λ₯Ό 가진닀.
  • abstract 클래슀λ₯Ό μƒμ†ν•˜κΈ° μœ„ν•΄μ„œλŠ” extendsλ₯Ό μ΄μš©ν•œλ‹€.
  • abstract λ©”μ„œλ“œλ₯Ό 가지며, μƒμ†ν•œ ν΄λž˜μŠ€μ—μ„œ λ°˜λ“œμ‹œ κ΅¬ν˜„ν•΄μ•Όν•œλ‹€.
  • 일반 λ©”μ„œλ“œλ„ κ°€μ§ˆ 수 μžˆλ‹€.
  • 일반 ν΄λž˜μŠ€μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ μƒμ„±μžλ„ μžˆλ‹€.

πŸ“Œ μΈν„°νŽ˜μ΄μŠ€ vs μΆ”μƒν΄λž˜μŠ€

[ 곡톡점 ]

(1) 좔상 λ©”μ„œλ“œλ₯Ό 가진닀.
(2) 객체 생성이 λΆˆκ°€ν•˜λ©° μžλ£Œν˜•(νƒ€μž…)으둜 μ‚¬μš©λœλ‹€.

[ 차이점 ]

- μΈν„°νŽ˜μ΄μŠ€
: μƒμˆ˜, μΆ”μƒλ©”μ„œλ“œλ§Œ 가진닀.
: 좔상 λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„λ§Œ ν•˜λ„λ‘œκ³  ν•œλ‹€.
: λ‹€ν˜•μ„±μ„ μ§€μ›ν•œλ‹€.

- μΆ”μƒν΄λž˜μŠ€
: ν΄λž˜μŠ€κ°€ κ°€μ§€λŠ” λͺ¨λ“  속성과 κΈ°λŠ₯을 가진닀.
: 좔상 λ©”μ„œλ“œ κ΅¬ν˜„ 및 μƒμ†μ˜ κΈ°λŠ₯을 가진닀.
: 단일 μƒμ†λ§Œ μ§€μ›ν•œλ‹€.


πŸ“Œ λžŒλ‹€μ‹

λžŒλ‹€μ‹μ΄λž€?
: 읡λͺ… ν•¨μˆ˜λ₯Ό μ΄μš©ν•΄μ„œ 읡λͺ… 객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ 식

- λžŒλ‹€μ‹ κ΅¬ν˜„

public interface LambdaInterface1 {
   public void method(String s1, String s2, String s3);
}

//Main Class
LambdaInterface1 li1 = (String s1, String s2, String s3) -> { System.out.println(s1 + " " + s2 + " " + s3); };
li1.method("Hello", "java", "World");

// λ§€κ°œλ³€μˆ˜κ°€ 1κ°œμ΄κ±°λ‚˜ νƒ€μž…μ΄ 같을 λ•Œ, νƒ€μž…μ„ μƒλž΅ν•  수 μžˆλ‹€.
LambdaInterface2 li2 = (s1) -> { System.out.println(s1); };
li2.method("Hello");

// 싀행문이 1개일 λ•Œ, '{}'λ₯Ό μƒλž΅ν•  수 μžˆλ‹€.
LambdaInterface3 li3 = (s1) -> System.out.println(s1);
li3.method("Hello");

// λ§€κ°œλ³€μˆ˜μ™€ 싀행문이 1개일 λ•Œ, '()'와 '{}'λ₯Ό μƒλž΅ν•  수 μžˆλ‹€.
LambdaInterface3 li4 = s1 -> System.out.println(s1);
li4.method("Hello");

// λ§€κ°œλ³€μˆ˜κ°€ 없을 λ•Œ, '()'만 μž‘μ„±ν•œλ‹€.
LambdaInterface3 li5 = () -> System.out.println("no param");
li5.method();

πŸ“Œ λ¬Έμžμ—΄ 클래슀

λ¬Έμžμ—΄μ„ λ‹€λ£¨λŠ” String 클래슀(객체)λŠ” 데이터가 λ³€ν•˜λ©΄ λ©”λͺ¨λ¦¬μƒμ˜ λ³€ν™”κ°€ λ§Žμ•„ 속도가 λŠλ¦¬λ‹€.

StringBuffer, StringBuilder

: String 클래슀의 단점을 λ³΄μ™„ν•œ 클래슀둜 데이터가 λ³€κ²½λ˜λ©΄ λ©”λͺ¨λ¦¬μ—μ„œ κΈ°μ‘΄ 객체λ₯Ό μž¬ν™œμš©ν•œλ‹€.

StringBuffer sf = new StringBuffer("JAVA");
sf.append("_8");
  • λ¬Έμžμ—΄μ΄ λ³€κ²½λ˜λ©΄ 기쑴의 객체λ₯Ό μž¬ν™œμš©ν•œλ‹€.
  • μ†λ„λŠ” StringBuilderκ°€ 쑰금 더 λΉ λ₯΄λ©°, 데이터 μ•ˆμ •μ„±μ€ StringBufferκ°€ 쑰금 더 μ’‹λ‹€.

πŸ“Œ Collections

πŸ“Ž List
: ListλŠ” μΈν„°νŽ˜μ΄μŠ€λ‘œ 이λ₯Ό κ΅¬ν˜„ν•œ ν΄λž˜μŠ€λŠ” 인덱슀λ₯Ό μ΄μš©ν•΄μ„œ 데이터λ₯Ό κ΄€λ¦¬ν•œλ‹€.

πŸ“Ž Map
: Map은 μΈν„°νŽ˜μ΄μŠ€λ‘œ 이λ₯Ό κ΅¬ν˜„ν•œ ν΄λž˜μŠ€λŠ” keyλ₯Ό μ΄μš©ν•΄μ„œ 데이터λ₯Ό κ΄€λ¦¬ν•œλ‹€.

profile
λ°±μ—”λ“œ 개발자
post-custom-banner

0개의 λŒ“κΈ€