728x90
반응형

자바예제 83

[자바] 예외(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)..

[자바] 예제 3 : 도서 목록 필터링 및 리스트화 하기

1. BookInfo 클래스를 정의하시오. 클래스의 속성은 다음과 같습니다. 적당한 영어를 사용하고 속성 type은 일단 String으로 하시오. - 순번, 브랜드, 분야, 도서형태, 도서명, 저자, 역자, 발행일, 페이지, 가격, ISBN, 도서상태 2. 책의 정보를 한 줄 읽을 때마다 BookInfo 객체를 하나 생성한 후, 읽어온 정보를 조기화하시오. 3. 책의 정보를 저장할 수 있는 ArrayList book_infos 리스트를 생성하시오. 4. book_infos 리스트에 BookInfo 객체를 추가하시오. import java.io.*; import java.util.ArrayList; public class Book { public static void main(String[] args) t..

728x90
반응형