728x90
반응형

프로그래밍 166

[자바] 스레드(Thread)

스레드(Thread) 한 프로세스 내에서 두가지 또는 그 이상의 일을 동시에 할 수 있다. // Thread public class Main extends Thread { public void run() { // Thread를 상속하면 run 메서드를 구현해야한다. while(true) { System.out.println("thread run"); } } public static void main(String[] args) { Main main = new Main(); main.start(); // start()로 쓰레드를 실행 Main m2 = new Main(); m2.start(); while(true) { System.out.println("m"); } } } Thread 예제 // Thread..

[자바] 예외(Exception)

예외의 발생 import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; // 예외의 발생 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("나없는파일")); br.readLine(); br.close(); } } 예외 처리하기 // 예외 처리하기 public class Main { public static void main(String[] args) { int c; try { c = 4/0; } catch (Arithmet..

[자바] 싱글톤 패턴(singleton)

싱글톤(singleton) 클래스를 통해 생성할 수 있는 객체가 한개만 되도록 만드는 것 class Singleton { private static Singleton one; private Singleton() { } public static Singleton getInstance() { if(one ==null) { one = new Singleton(); } return one; } } public class Main { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(si..

[자바] 다형성(polymorphism)

다형성(polymorphism) : 하나의 객체가 여러개의 자료형 타입을 가질 수 있는 것 다형성을 이용하면 복잡한 형태의 분기문을 간단하게 처리할 수 있다. Main.java class MyApp extends App { // 상속 public MyApp(String name) { // 생성자 super(name); } @Override void show_menu() { // 오버라이딩 } } // 상속, 생성자, 오버라이딩 = 다형성 public class Main { public static void main(String[] args) throws IOException { App app = new MyApp("MyApp"); // 1. app을 만들고, 다형성 app.add("1", new CmdL..

[자바] 인터페이스(interface)

인터페이스(interface) : implements를 사용하여 구현한다. interface AnimalFeed { void feed(); // 2. 매개로 쓰겠다 } class Tiger implements AnimalFeed { public void feed() { // 3. overriding System.out.println("feed apple"); } } class Lion implements AnimalFeed { public void feed() { System.out.println("feed banana"); } } class Crocodile implements AnimalFeed { public void feed() { System.out.println("feed strawberry"..

[자바] 상속(inheritance)

상속(inheritance) : 부모 클래스, 자식 클래스 클래스 상속을 위해 extends를 사용한다. public class Main { public static void main(String[] args) { Cat cat = new Cat(); cat.setName("Tom"); Dog dog = new Dog(); dog.setName("Jerry"); System.out.println(dog.name); dog.sleep(); HouseDog happy = new HouseDog("happy"); HouseDog yorkshire = new HouseDog(1); // houseDog.sleep(); // houseDog.sleep(3); System.out.println(happy.name)..

[자바] 값에 의한 호출과 객체에 의한 호출 : 전달받은 숫자를 1만큼 증가시키는 update메서드

class Updater { void update(Counter counter) { counter.count++; } } class Counter { int count = 0; // 객체 변수 } public class Main { public static void main(String[] args) { Counter myCounter = new Counter(); System.out.println("before update:"+myCounter.count); Updater myUpdater = new Updater(); myUpdater.update(myCounter); System.out.println("after update:"+myCounter.count); } }

[자바] 자바 클래스(class)의 이해

클래스 : 객체를 만드는 기능 객체 변수 : 인스턴스 변수, 멤버 변수, 속성 이라고 함 class Animal { String name; public void setName(String name) { this.name = name; } } new는 객체를 생성할 때 사용하는 키워드로 Animal 클래스의 인스턴스(instance)인 cat, 즉 Animal의 객체가 만들어진다. public class Main { public static void main(String[] args) { Animal cat = new Animal(); cat.setName("body"); // 메서드 호출 Animal dog = new Animal(); dog.setName("happy"); System.out.print..

[자바] 자바 객체 지향 프로그래밍(계산기 예제)

class Calculator { static int result = 0; static int add(int num) { result += num; return result; } } public class Main { public static void main(String[] args) { Calculator calc1 = new Calculator(); // 계산기1 객체 생성 Calculator calc2 = new Calculator(); // 계산기2 객체 생성 System.out.println(calc1.add(3)); System.out.println(calc1.add(4)); System.out.println(calc2.add(3)); System.out.println(calc2.add(7)..

728x90
반응형