Skip to content

Latest commit

Β 

History

History
569 lines (407 loc) Β· 14.5 KB

File metadata and controls

569 lines (407 loc) Β· 14.5 KB
icon
java

μžλ°” 문법 ν•œλˆˆμ— 정리 2편

λ©”μ„œλ“œ(static)


μ€‘λ³΅λœ μ½”λ“œλ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•¨

public class Main {
    public static void main(String[] args) {
        add(50,10);
        System.out.println(minus(40,10)); //return을 μ‚¬μš©μ‹œ λ°”κΉ₯μ—μ„œλ„ 좜λ ₯κ°€λŠ₯
    }

    public static void add(int x, int y){ //(νŒŒλΌλ―Έν„°) voidκ°€ μžˆλ‹€λŠ” 건 return 값이 μ—†λ‹€λŠ” 것
        System.out.println(x+y);
    }

    public static int minus(int x, int y){
        return x-y;
    }
}

λ©”μ†Œλ“œ μ˜€λ²„λ‘œλ“œ


public class Main {
    public static void main(String[] args) {
        System.out.println(add(50,10));
        System.out.println(add(40,10,12));

        System.out.println(add(10,12,13,14));
    }

    public static int add(int x, int y){ //λ©”μ„œλ“œ 이름이 κ°™λ‹€λ©΄ λ³€μˆ˜μ˜ κ°œμˆ˜κ°€ 달라야 함
        return x+y;
    }

    public static int add(int x, int y,int z){
        return x+y+z;
    }

    public static int add(int... numbers){ //...은 0κ°œμ΄μƒμ˜ 숫자
        int sum = 0;
        for(int i =0; i < numbers.length; i++){
            sum = sum + i;
        }
        return sum;
    }
}

static


static λ©”λͺ¨λ¦¬μ— 처음 ν”„λ‘œκ·Έλž¨ μ‹€ν–‰μ‹œ 처음 λ‘œλ“œκ°€ λ˜λŠ” 뢀뢄이라 λ³„λ„μ˜ μ˜μ—­μ˜ λ©”λͺ¨λ¦¬λ₯Ό μ°¨μ§€ν•˜κΈ° λ•Œλ¬Έμ— public static void main(String[] args)λŠ” μƒμœ„μΈ public class Mainκ³ΌλŠ” 상관이 μ—†μŒ

ν΄λž˜μŠ€μ™€ getter, setter와 μƒμ„±μž


클래슀 : 속성과 κΈ°λŠ₯으둜 λ‚˜λˆ μ§

getter 와 setter : ꡳ이 private으둜 λ³€μˆ˜λ₯Ό μƒμ„±ν•˜κ³  get/set을 μ΄μš©ν•΄ 데이터에 μ ‘κ·Όν•˜λŠ” μ΄μœ λŠ” 데이터 무결성지킀기 μœ„ν•¨μ΄λ‹€. λ§Œμ•½ μŒμ‹μ˜ 가격, κ°―μˆ˜κ°€ μŒμˆ˜κ°€ μ˜¨λ‹€λ©΄ 데이터 ν˜•μ‹μ— λ§žμ§€ μ•ŠκΈ° λ•Œλ¬Έμ— getset ν•¨μˆ˜ λ‚΄μ—μ„œ 무결성을 μ§€μΌœμ£Όμ–΄μ•Ό ν•œλ‹€.

private String name;
    private int age;

//private을 ν•˜λŠ” λŒ€μ‹  getter와 setter을 ν•΄μ€Œ
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

μƒμ„±μž


μƒμ„±μžμ˜ κ·œμΉ™

  1. 클래슀λͺ…κ³Ό λ©”μ†Œλ“œλͺ…이 λ™μΌν•˜λ‹€.
  2. λ¦¬ν„΄νƒ€μž…μ„ μ •μ˜ν•˜μ§€ μ•ŠλŠ”λ‹€. (void도 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€.)
Person(String name, int age){ //ν•„λ“œλ₯Ό 전달받아 λ§Œλ“œλŠ” μ˜€λ²„λ‘œλ“œ 된 μƒμ„±μž
        this.name = name; //μ΄ˆκΈ°ν™” thisλŠ” μ™ΈλΆ€λΌλŠ” λ³€μˆ˜μ—μ„œ λ„˜μ–΄μ˜¨ κ°’, λ‚΄κ°€ 가진 name이냐? μ•„λ‹ˆλƒλ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄
        this.age = age;
    }

to String


μƒμ„±μžλ₯Ό μ΄μš©ν•˜μ—¬ λ§Œλ“€λ©΄ λ‚΄κ°€ λͺ¨λ₯΄λŠ” μ–Έμ–΄λ‘œ 좜λ ₯λΌμ„œ 였λ₯Έμͺ½ 마우슀 ν•΄μ„œ toString을 ν•΄μ£Όλ©΄ μš°λ¦¬κ°€ μ•„λŠ” μ–Έμ–΄λ‘œ 좜λ ₯됨

@Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\\'' +
                ", age=" + age +
                '}';
    } //였λ₯Έμͺ½ 마우슀λ₯Ό 톡해 toString을 ν΄λ¦­ν•˜μ—¬ λ§Œλ“€μ–΄μ€Œ

μ „

ν›„

ν΄λž˜μŠ€μ™€ getter/setter와 μƒμ„±μž, toString, λͺ¨λΈ 클래슀 μ „μ²΄μ½”λ“œ


public class Main {
    public static void main(String[] args) {
        System.out.println(add(50,10));
        System.out.println(add(40,10,12));

        System.out.println(add(10,12,13,14));

        Person person = new Person();
        Person person2 = new Person("홍길동", 10);

        System.out.println(person);
        System.out.println(person2);
    }

    public static int add(int x, int y){ //λ©”μ„œλ“œ 이름이 κ°™λ‹€λ©΄ λ³€μˆ˜μ˜ κ°œμˆ˜κ°€ 달라야 함
        return x+y;
    }

    public static int add(int x, int y,int z){
        return x+y+z;
    }

    public static int add(int... numbers){ //...은 0κ°œμ΄μƒμ˜ 숫자
        int sum = 0;
        for(int i =0; i < numbers.length; i++){
            sum = sum + i;
        }
        return sum;
    }
}

class Person{ //속성과 κΈ°λŠ₯으둜 λ‚˜λˆ μ§
    private String name;
    private int age;

    Person(){ //ν΄λž˜μŠ€μ™€ 이름이 같은 κΈ°λ³Έμƒμ„±μž

    }

    Person(String name, int age){ //ν•„λ“œλ₯Ό 전달받아 λ§Œλ“œλŠ” μ˜€λ²„λ‘œλ“œ 된 μƒμ„±μž
        this.name = name; //μ΄ˆκΈ°ν™” thisλŠ” μ™ΈλΆ€λΌλŠ” λ³€μˆ˜μ—μ„œ λ„˜μ–΄μ˜¨ κ°’, λ‚΄κ°€ 가진 name이냐? μ•„λ‹ˆλƒλ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄
        this.age = age;
    }

    //private을 ν•˜λŠ” λŒ€μ‹  getter와 setter을 ν•΄μ€Œ
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\\'' +
                ", age=" + age +
                '}';
    } //였λ₯Έμͺ½ 마우슀λ₯Ό 톡해 toString을 ν΄λ¦­ν•˜μ—¬ λ§Œλ“€μ–΄μ€Œ
}

νŒ¨ν‚€μ§€μ™€ import


model νŒ¨ν‚€μ§€μ— Person.classλ₯Ό λ„£μ–΄μ€Œ

package model;

public class Person{ //속성과 κΈ°λŠ₯으둜 λ‚˜λˆ μ§
    private String name;
    private int age;

    public Person(){ //public을 κΌ­ μ¨μ€˜μ•Ό 함 μ•ˆκ·ΈλŸ¬λ©΄ mainμ—μ„œ μ ‘κ·Ό λΆˆκ°€

    }

    public Person(String name, int age){ 
        this.name = name; 
        this.age = age;
    }

    //private을 ν•˜λŠ” λŒ€μ‹  getter와 setter을 ν•΄μ€Œ
    public String getName() { //get은 값을 κΊΌλ‚΄λŠ” 것
        return name;
    }

    public void setName(String name) { //set은 값을 λ„£λŠ” 것
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\\'' +
                ", age=" + age +
                '}';
    }
}

com.company νŒ¨ν‚€μ§€ μ•ˆμ— Main μžλ°”

package com.company;

import model.Person;

public class Main {
    public static void main(String[] args) {

        Person person = new Person();
        Person person2 = new Person("홍길동", 10);

        System.out.println(person);
        System.out.println(person2);
    }
}

상속


μžμ‹(클래슀)이 상속받고 싢은 λΆ€λͺ¨(클래슀)λ₯Ό μ„ νƒν•΄μ„œ λ¬Όλ €λ°›λŠ”λ‹€. μ΄λ•Œ μƒμ†λ°›λŠ” 클래슀λ₯Ό μžμ‹ 클래슀, ν•˜μœ„ 클래슀 λ˜λŠ” μ„œλΈŒ 클래슀 라고 λΆ€λ₯Έλ‹€.

상속을 ν•΄μ£ΌλŠ” 클래슀λ₯Ό λΆ€λͺ¨ 클래슀, μƒμœ„ 클래슀 λ˜λŠ” 슈퍼 클래슀

  1. 상속 방법 : class 클래슀(μžμ‹) extends 클래슀(λΆ€λͺ¨)
  2. μžμ‹ ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ ν΄λž˜μŠ€λ‘œλΆ€ν„° λ©”μ†Œλ“œμ™€ ν•„λ“œλ₯Ό λ¬Όλ €λ°›μ•„ μ‚¬μš©, λΆ€λͺ¨ ν΄λž˜μŠ€λŠ” μžμ‹ ν΄λž˜μŠ€μ—μ„œ μ •μ˜ν•œ λ©”μ†Œλ“œλ‚˜ ν•„λ“œλ₯Ό μ‚¬μš©ν•˜μ§€ λͺ»ν•œλ‹€
  3. μžμ‹ ν΄λž˜μŠ€λŠ” μ—¬λŸ¬ λΆ€λͺ¨ ν΄λž˜μŠ€λ‘œλΆ€ν„° 상속받지 λͺ»ν•œλ‹€. 즉, λ‹¨μΌμƒμ†λ§Œ κ°€λŠ₯
  4. λΆ€λͺ¨ ν΄λž˜μŠ€λŠ” μ—¬λŸ¬ μžμ‹ ν΄λž˜μŠ€μ—κ²Œ 상속이 κ°€λŠ₯
  5. μžμ‹ ν΄λž˜μŠ€κ°€ 더 λ‹€μ–‘ν•œ κΈ°λŠ₯이 κ°€λŠ₯ν•˜λ―€λ‘œ μžμ‹ 클래슀둜 μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” 것이 효율적
hero.isFlying(); 
//get의 속성도 가지고 있고 boolean의 경우 isλΌλŠ” μ΄λ¦„μœΌλ‘œ getter을 가짐

β‡’ 이의 경우 μ–΄λ””μ„œ μ•Œ 수 μžˆλŠλƒ? Hero의 getterλ₯Ό 보면 μ•Œ 수 있음

πŸ’‘ set = μ„€μ •, get = 값을 κ°€μ Έμ˜€λŠ” 것,

  • λΌμ§€μ €κΈˆν†΅μ„ λ§Œλ“ λ‹€ > λ³€μˆ˜ μ„ μ–Έ
  • κ·Έ μ•ˆμ— λˆμ„ μ €μΆ•ν•œλ‹€ > set
  • κ·Έ μ•ˆμ— 돈이 μ–Όλ§ˆλ‚˜ λ“€μ–΄ μžˆλŠ”μ§€ λ³Έλ‹€ > get
  • μ‚¬μš© 이유 : SET, GET λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” μ™ΈλΆ€λ‘œλΆ€ν„° λ³€μˆ˜κ°’μ— μ§μ ‘μ μœΌλ‘œ μ ‘κ·Όν•˜λŠ”κ²ƒμ„ 막기 μœ„ν•΄μ„œλ‹€. 직접 μ ‘κ·Όν•˜κ°œ 되면 값이 λ³€ν•˜κ²Œ 되고 그러면 데이터 무결성이 깨질 수 있기 λ•Œλ¬Έμ΄λΌκ³  함

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


좔상 ν΄λž˜μŠ€λŠ” λ¬Έλ²•λ³΄λ‹€λŠ” 무엇이고 μ™œ μ‚¬μš©ν•˜λŠ”μ§€ 본질적인 κ°œλ…μ„ μ•Œμ•„μ•Ό 함

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

μΆ”μƒν΄λž˜μŠ€λŠ” A클래슀, B클래슀, Cν΄λž˜μŠ€λ“€ 간에 λΉ„μŠ·ν•œ ν•„λ“œμ™€ λ©”μ„œλ“œλ₯Ό κ³΅ν†΅μ μœΌλ‘œ μΆ”μΆœν•΄ λ§Œλ“€μ–΄μ§„ ν΄λž˜μŠ€λ‹€.

μ‹€μ²΄ν΄λž˜μŠ€λŠ” 싀체가 λ“œλŸ¬λ‚˜λŠ” 클래슀둜 A클래슀, B클래슀, Cν΄λž˜μŠ€μ™€ 같은 것듀이닀.

μΆ”μƒν΄λž˜μŠ€λŠ” 싀체 클래슀의 곡톡적인 뢀뢄을 μΆ”μΆœν•΄ μ–΄λŠμ •λ„ κ·œκ²©μ„ μž‘μ•„λ†“μ€ 좔상적인 클래슀, 그리고 μ‹€μ²΄ν΄λž˜μŠ€μ™€λŠ” 상속관계

So, μ‹€μ²΄ν΄λž˜μŠ€λŠ” μ‹€μ œ 객체λ₯Ό 생성할 μ •λ„μ˜ ꡬ체성을 가진닀. ν•˜μ§€λ§Œ μΆ”μƒν΄λž˜μŠ€λŠ” 아직 λ©”μ„œλ“œμ™€ λ‚΄μš©μ΄ 좔상적이기 λ•Œλ¬Έμ— 객체λ₯Ό 생성 λΆˆκ°€

Charter charter = new Charter(); // Charterκ°€ μΆ”μƒν΄λž˜μŠ€μΈ 경우 μ•ˆλ¨!!!
  • μΆ”μƒν΄λž˜μŠ€λŠ” 클래슀 μ•žμ— abstractλ₯Ό λΆ™μ—¬μ£Όκ³  좔상 ν•„λ“œλ‚˜ λ©”μ„œλ“œλ₯Ό λ§Œλ“ λ‹€!
  • {}없이 λ‚΄μš©μ΄ μ—†κΈ° 떄문에 좔상 λ©”μ„œλ“œ
public abstract class Character {
    //Magician λ˜ν•œ μ‹Έμ›€μ˜ κΈ°λŠ₯을 가지고 μ‹Άλ‹€λŠ” μ „μ œμ— 캐릭터에 μ‹Έμ›€μ˜ κΈ°λŠ₯을 κ°•μ œν™”
    public abstract void attack(Hero hero);//{}없이 λ‚΄μš©μ΄ μ—†κΈ° 떄문에 좔상 λ©”μ„œλ“œ
}

좔상 클래슀λ₯Ό μƒμ†ν•˜λŠ” μžμ‹ν΄λž˜μŠ€μ˜ 경우 extendsλ₯Ό μ‚¬μš©ν•΄μ„œ μΆ”μƒν΄λž˜μŠ€ 상속

public class Magician extends Character {

    @Override
    public void attack(Hero hero) {
     //μ•ˆμ— λ‚΄μš©μ„ μ μ–΄μ€˜μ•Ό 함
    }
}

interface


μΆ”μƒν΄λž˜μŠ€μ™€ 같은 μ—­ν• 

ν™•μž₯μ„±κ³Ό μœ μ—°μ„±μ„ 가짐, 닀쀑상속 κ°€λŠ₯, λ‹€ν˜•μ„±

μ•„λž˜ λ‘κ°œλŠ” 같은 의미

public abstract class Character {
    //Magician λ˜ν•œ μ‹Έμ›€μ˜ κΈ°λŠ₯을 가지고 μ‹Άλ‹€λŠ” μ „μ œμ— 캐릭터에 μ‹Έμ›€μ˜ κΈ°λŠ₯을 κ°•μ œν™”
    public abstract void attack(Hero hero);//{}없이 λ‚΄μš©μ΄ μ—†κΈ° 떄문에 좔상 λ©”μ„œλ“œ
}
interface Character{ //μΆ”μ‚΄ν΄λž˜μŠ€μ™€ λ˜‘κ°™μ€ κΈ°λŠ₯
    void attack(Hero hero);
}

but μΆ”μƒν΄λž˜μŠ€λ‚˜ μΈν„°νŽ˜μ΄μŠ€ μ°Έμ‘°μ‹œ

μΆ”μƒν΄λž˜μŠ€ : extends μ‚¬μš©

public class Magician extends Character {

    @Override
    public void attack(Hero hero) {

    }
}

μΈν„°νŽ˜μ΄μŠ€ : implements μ‚¬μš©

public class Magician implements Character {

    @Override
    public void attack(Hero hero) {

    }
}

μž₯점

ArrayList μ‚¬μš©μ‹œ μ—¬λŸ¬κ°€μ§€ νƒ€μž… μ‚¬μš©κ°€λŠ₯

ArrayList<Character> characterArrayList = new ArrayList<>(); //<>μ•ˆμ— Charcter λŒ€μ‹  Magician λ“± κ°€λŠ₯
        characterArrayList.add(magician);
        characterArrayList.add(magician2);
        characterArrayList.add(hero); //μ—¬λŸ¬κ°€μ§€ νƒ€μž…μ„ 담을 수 μžˆλ‹€λŠ” λ‹€ν˜•μ„±

instanceof


μ°Έμ‘°λ³€μˆ˜κ°€ μ°Έμ‘°ν•˜κ³  μžˆλŠ” μΈμŠ€ν„΄μŠ€μ˜ μ‹€μ œ νƒ€μž…μ„ μ•Œμ•„λ³΄κΈ° μœ„ν•΄ instanceof μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 주둜 쑰건문에 μ‚¬μš©

Magician.class

public class Magician extends Character implements ICharacter {

    @Override
    public void attack(Hero hero) {

    }

    @Override
    public void attack(Person person) {

    }
}

IChatacter μΈν„°νŽ˜μ΄μŠ€

package model;

public interface ICharacter { //μΆ”μ‚΄ν΄λž˜μŠ€μ™€ λ˜‘κ°™μ€ κΈ°λŠ₯
    void attack(Person person);
}

Character.class

package model;

public abstract class Character extends Person{
    //Magician λ˜ν•œ μ‹Έμ›€μ˜ κΈ°λŠ₯을 가지고 μ‹Άλ‹€λŠ” μ „μ œμ— 캐릭터에 μ‹Έμ›€μ˜ κΈ°λŠ₯을 κ°•μ œν™”
    public abstract void attack(Hero hero);//{}없이 λ‚΄μš©μ΄ μ—†κΈ° 떄문에 좔상 클래슀
}

πŸ€ instanceof μ‚¬μš©μ²˜

  • 쑰상 μ°Έμ‘°λ³€μˆ˜ instanceof μžμ† μΈμŠ€ν„΄μŠ€ (무쑰건 O)
  • Object o instanceof μžμ† μΈμŠ€ν„΄μŠ€ (무쑰건 O)
  • μžμ† μ°Έμ‘°λ³€μˆ˜ instanceof 쑰상 μΈμŠ€ν„΄μŠ€ (κ°€λŠ₯함 O)
Magician magician = new Magician(); 
        Character magician2 = new Magician();
        ICharacter magician3 = new Magician();

        if(magician2 instanceof Magician){ //Magician인지 μ•„λ‹Œμ§€ μ²΄ν¬ν•˜λŠ” 것이 instanceof

        }

μ œλ„€λ¦­


μ–΄λ–€ νƒ€μž…μ΄ 듀어와도 λœλ‹€μ˜ 의미

public class Main {
    public static void main(String[] args) {
        print("μ•ˆλ…•");
        print(1);
        print(3L);
        print(true);
    }
    
    public static <T> void print(T type){ //μ–΄λ–€ νƒ€μž…μ΄ 듀어와도 λœλ‹€μ˜ 의미
        System.out.println(type);
    }
}

Thread(μŠ€λ ˆλ“œ)


μ—¬λŸ¬κ°€μ§€ 일을 λ™μ‹œμ— ν•  λ•Œ μ‚¬μš©

Runnable은 interfaceκ³ , run은 μΆ”μƒλ©”μ„œλ“œ

public class Main {
    public static void main(String[] args) {
        System.out.println("1");

      new Thread(new Runnable() {
          @Override
          public void run() {
              for(int i = 0; i < 5; i++){ try {
                  Thread.sleep(1000); //1μ΄ˆν›„μ—
                  System.out.println(Thread.currentThread().getName() +":" + i);
              }
             catch (InterruptedException e){
                  e.printStackTrace();
              }
            }
              
          }
      }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 5; i++){ try {
                    Thread.sleep(1000); //1μ΄ˆν›„μ—
                    System.out.println(Thread.currentThread().getName() +":" + i);
                }
                catch (InterruptedException e){
                    e.printStackTrace();
                }
                }
            }
        }).start();
        System.out.println("2");
    }
}

κ²°κ³Όμ°½

λžŒλ‹€μ‹


μΆ”μƒλ©”μ„œλ“œκ°€ 1개이고 νŒŒλΌλ―Έν„°κ°€ 없을 λ•Œ μ‚¬μš© κ°€λŠ₯

μœ„μ˜ μŠ€λ ˆλ“œμ—μ„œ ctrl + shift + enterλ₯Ό λˆ„λ₯΄λ©΄ λ³€κ²½κ°€λŠ₯

new Thread(() -> {
          for(int i = 0; i < 5; i++){ try {
              Thread.sleep(1000); //1μ΄ˆν›„μ—
              System.out.println(Thread.currentThread().getName() +":" + i);
          }
         catch (InterruptedException e){
              e.printStackTrace();
          }
        }
      }).start();

λŠλ‚Œμ  🀣


문법도 μ€‘μš”ν•˜μ§€λ§Œ ν”„λ ˆμž„μ›Œν¬μ˜ μ΄μš©λ„λ₯Ό μ•Œλ©΄ 문법을 크게 곡뢀 μ•ˆν•΄λ„ λ λ“―ν•œ λŠλ‚Œ

https://wikidocs.net/281 : 점프 투 μžλ°”