Rainnable.java
package inter;
//JavaDoc
// 단축키 : Alt + Shift + J
/**
* 비를 내리는 인터페이스
* @author Androphil
* @since 2012.8.30
* @version 1.0
*/
public interface Rainnable {
/**
* 기본강수량 200ml
*/
int DEFAULT_RAINFALL = 200; // public static final 생략됨
/**
* 비를 내리는 메소드
* @param amount 내릴 비의 양
*/
void rain(int amount); // public abstract 생략됨
}
////////////////////////////////////////////////////////////////////////////////////////
God.java
package inter;
public class God implements Rainnable {
@Override
public void rain(int amount) {
// TODO Auto-generated method stub
System.out.println(amount + "만큼 비를 내려요~ 오~ 갓!");
}
public static void main(String[] args) {
new God().rain(DEFAULT_RAINFALL);
God god = new God();
Rainnable r = god;
r.rain(DEFAULT_RAINFALL);
play(god, 1000);
}
public static void play(Rainnable g, int amount) {
g.rain(amount);
}
}
* 중첩클래스
package nested;
import inter.*;
public class TestNested {
static String name;
int value;
private static class SNest {
// 내부클래스에는 모든 접근 지정자를 적용할 수 있다.
void method() {
System.out.println(name);
//System.out.println(value);
// static 변수에만 접근이 가능하다.
}
}
private class Inner implements Rainnable {
@Override
public void rain(int amount) {
System.out.println("비내려요~" + value + name);
}
}
public static void main(String[] args) {
// static-nested class 객체 생성
TestNested.SNest sn = new TestNested.SNest();
// inner class 객체 생성
TestNested.Inner in = new TestNested().new Inner();
// anonymous inner class
Rainnable r = new Rainnable() {
@Override
public void rain(int amount) {
}
};
"".equals(new Object(){});
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("쓰레드 : " + i);
}
}
}).start();
for(int i=0; i<10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("메인 : " + i);
}
}//end main
}
* 컬렉션
TestCol.java
package col;
import java.util.*;
public class TestCol {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("첫번째");
list.add("두번째");
list.add("세번째");
for(String s : list) {
System.out.println(s);
}
LinkedList list1 = new LinkedList<>();
list1.offer("첫번째");
list1.offer("두번째");
String s = list1.poll();
System.out.println(s);
System.out.println(list.size());
// Array, List 계열은 정렬가능
// 배열은 Arrays, List계열은 Collections
ArrayList al = new ArrayList<>();
al.add("주원병준");
al.add("몽골용단");
al.add("피곤인애");
al.add("털광필");
Collections.sort(al);
Collections.reverse(al);
for(String str : al) {
System.out.println(str);
}
ArrayList list2 = new ArrayList<>();
list2.add(new Student("주원병준", 174, 95));
list2.add(new Student("털광필", 171, 100));
list2.add(new Student("몽골용단", 174, 99));
list2.add(new Student("피곤인애", 154, 100));
list2.add(new Student("멋쟁이완샘", 184, 1000));
Collections.sort(list2);
for(Student stu : list2) {
System.out.println(stu);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
Student.java
package col;
// Data Bean (데이터를 저장하는 용도)
public class Student implements Comparable{
private String name;
private int height;
private int point;
public Student() {
}
public Student(String name, int height, int point) {
super();
this.name = name;
this.height = height;
this.point = point;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
@Override
public int compareTo(Student o) {
return o.getPoint() - this.point;
}
@Override
public String toString() {
return "Student [name=" + name + ", height=" + height + ", point="
+ point + "]";
}
}