본문 바로가기

프로그래밍/Java

(64)
Runnable 구현하기 Ex12_5.java package ex12_5; public class Ex12_5 { public static void main(String[] args) { GooGoo g2 = new GooGoo(2); Thread t2 = new Thread(g2); Thread t3 = new Thread(new GooGoo(3)); Thread t4 = new Thread(new GooGoo(4)); Thread t5 = new Thread(new GooGoo(5)); Thread t6 = new Thread(new GooGoo(6)); Thread t7 = new Thread(new GooGoo(7)); Thread t8 = new Thread(new GooGoo(8)); Thread t9 = new Th..
Thread 구현하기 스레드 구현하기 ★☆ - Thread 클래스를 이용하여 구현 Ex12_2.java package ex12_2; public class Ex12_2 { public static void main(String[] args) { Go g = new Go(); Come c = new Come(); Thread t = new Thread(g); Thread t1 = new Thread(c); t.start(); t1.start(); } } Go.java package ex12_2; public class Go extends Thread { public void run(){ while(true){ System.out.println("go"); } } } Come.java package ex12_2; public c..
Thread 스레드 (1) 프로세스와 스레드 - 멀티 태스킹 i) 프로세스란 운영체제에서 실행중인 하나의 프로그램을 말합니다. ii) 멀티 프로세스란 두 개 이상의 프로세스가 실행되는 것을 말합니다. iii) 멀티 태스킹이란 두 개 이상의 프로세스를 실행하여 일을 처리하는 것을 말합니다. - 멀티 스레드 i) 스레드란 프로세스 내에서 실행되는 세부 작업 단위입니다. ii) 멀티 스레드란 하나의 프로세스에서 여러 개의 스레드가 병행적으로 처리되는 것을 말합니다. - 간략한 그림 (2) 스레드의 생명주기 스레드는 Thread 객체가 생성되면 생명주기를 갖게 되는데 크게 5가지로 나누게 됩니다. i) New – 스레드가 만들어진 상태 ii) Runnable – 스레드 객체가 생성된 후에 start() 메서드를 호출하면 Runna..
File API package ex11_5; import java.io.File; // File 불러오기 public class Ex11_5 { public static void main(String[] args) { try{ File f = new File("D:\\temp","Ex11_5.java"); System.out.println(f.getName()); System.out.println(f.exists()); System.out.println(f.getPath()); System.out.println(f.length()); System.out.println(File.separator); boolean a = new File("D:\\temp\\ccc").mkdir(); boolean b = new File("..
다형성을 메소드 인자 ★ 다형성을 이용한 메소드 인자 지정하기 package ex06_8; class Employee{ public void taxRate ( Employee e ){ if (e instanceof Maneger){ Maneger m = (Maneger)e; System.out.println("Maneger 세금 구하기 "); } else if (e instanceof Engineer){ Engineer en = (Engineer)e; System.out.println("Enginner 세금 구하기 "); } else if (e instanceof Employee){ System.out.println("Employee 세금 구하기 "); } } } class Maneger extends Employee {} ..
인터페이스 ( interface ) (1) 인터페이스란? 클래스가 상속을 통해 구현하기에 한계가 있는 경우, 즉 자바에서 불가능한 다중상속을 흉내내기 위한 도구로써 사용됩니다.(우회적 다중상속 제공) 우회적 다중상속으로 구조적 클래스 설계시 유용하게 사용됩니다.추상클래스의 경우에는 추상메서드와 일반메서드, 일반변수를 포함하고 있지만, 인터페이스는 오직 선언만 가능하기 때문에 일반메서드를 포함할 수 없습니다.(추상메서드와 상수만으로 구성) (2) 인터페이스 특징 - 일반 클래스는 여러 인터페이스를 다중 implements(구현)하여 사용할 수 있습니다. - 인터페이스 간의 상속에서는 다중상속이 제공됩니다. - 인터페이스가 다른 인터페이스로부터 상속을 받았다고 하지만 오버라이딩(재정의)을 할 수는 없습니다.(인터페이스는 body를 가지는 일반..
super 실습하기 package ex06_3; class Employee{ String name; // 인스턴스 변수 선언 int salary; public Employee(){} // 생성자 public Employee(String name,int salary){ // 생성자 오버로딩 !! this.name = name; this.salary = salary; } public String getEmployee(){ // getEmployee() 메서드 return name+" "+salary; } } class Maneger extends Employee { // Maneger Employee 상속하기 String department; public Maneger(){} public Maneger(String name,i..
상속? 상속 ( Inheritance ) [ 자바 상속 ] ㄴ 어떤 클래스의 모든 내용(멤버필드, 메소드)을 그대로 가져온다는 뜻. ㄴ 접근제한자 지정예약어 class 클래스명 extends 상위클래스 implement 상위인터페이스{ ... } * 잠깐! 용어정리 포함 Object : 하나의 클래스 내부에 다른 클래스의 객체를 포함하는 있는 형태. ※ Object 클래스 : 자바의 모든 클래스의 부모 클래스. ㄴ Object 클래스의 멤버는 생성자와 메서드 뿐이다. ( 필드는 없음 !!) 따라서, 메서드만 상속을 받는다. @ super 예약어 ㄴ 상속관계에서 자신에게 상속한 상위 클래스를 지칭 => 자신의 클래스 내에서 자신을 지칭할 때에는 this 라는 예약어를 사용하고 자신의 입장에서 상위에 있는 멤버를 ..