본문 바로가기

자바/클래스

anonymous class 익명클래스 예제) //익명 클래스/** new 상위클래스(){* 메소드 재정의* };*/public class Test3 {public Object getAnno(){return new Object(){//public String toString(){// 메소드를 간단하게 재정의 하면서 클래스에 이름을 붙이고 싶지 않을 때 사용.return "익명 예제...";// 여기가 익명클래스 사용(익명클래스는 클래스이름$숫자.class 로 만들어짐)}//};//}public static void main(String[] args) {Test3 ob=new Test3();System.out.println(ob);System.out.println(ob.toString());// Object의 toString()Object oo.. 더보기
내부클래스 예제1) public class Test1 {public static void main(String[] args) {Demo1 aa=new Demo1(); //객체생성=> 메모리 할당.//Demo1.Ex1 bb=new Demo1.Ex1();Demo1.Ex1 bb=aa.new Ex1();// 내부클래스의 객체생성.bb.write();}} class Demo1{public static int a=10;public int b=20;// 내부 클래스public class Ex1 {public int c;public void write() {System.out.println(a+":"+b+":"+c);}}public void test() {Ex1 oo=new Ex1();oo.write();}public void .. 더보기
인터페이스 interface 예제1) public class Test7 {public static void main(String[] args) {System.out.println(Demo7.a); //10 => 인터페이스는 static이 들어가 있기때문에 바로 사용가능.Ex7 ob=new Ex7();ob.write(); // 테스트.........ob.print(); // print.................Demo7 oo=new Ex7(); // 업캐스팅 //Demo7아버지를 Ex7자식으로 업캐스팅함.oo.write();//oo.print(); //에러((Ex7)oo).print(); // 다운 캐스팅, 업된것만 다운캐스팅 가능}} /* * 인터페이스 : 메소드가 선언만 있고 정의가 없다. * 변수는 static final 만.. 더보기
abstract 클래스 예제) public class Test6 {public static void main(String[] args) {int[] value={25,65,32,84,75};SelectionSort ss=new SelectionSort();ss.sort(value);for(int n: value)System.out.println(n+" ");System.out.println();int[] data={66,85,7,53,55};SortInt bs=new BubbleSort();bs.sort(data);for(int n: data)System.out.println(n+" ");System.out.println();}} /* * abstract 클래스 * abstract 클래스는 선언만 있고 정의가 없는 메소드를 포.. 더보기
상속 super 슈퍼클래스=상위클래스=부모클래스서브클래스=하위클래스=자식클래스 예제1) public class Test1 {public static void main(String[] args) { Demo1 ob=new Demo1(); ob.write();}} class Ex1{protected int x;public Ex1(int x) {this.x=x;}} /*class Demo1 extends Ex1 { =>디폴트 생성자를 호출한 셈이다. 그런데 상위클래스에는 디폴트생성자는 없고 인자가있는 생성자만 있다.public void write(){System.out.println(x);}}*/ class Demo1 extends Ex1 {public Demo1() {super(10); System.out.println(".. 더보기
Scanner 클래스 예제1) import java.util.Scanner; /* * Scanner * 단락문자의 패턴을 사용하여 입력을 토근에 따라 * 분할하며 기본 토근은 공백이다. * 예외처리를 명시하지 않아도 된다. */public class Test5 {public static void main(String[] args) {String name;int k, e;Scanner sc=new Scanner(System.in); /*System.out.print("이름?");name=sc.next();System.out.print("국어?");k=sc.nextInt();System.out.print("영어?");e=sc.nextInt();*//*// 홍길동 80 60System.out.print("이름 국어 영어(공백으로 .. 더보기
SimpleDateFormat 예제1) import java.text.SimpleDateFormat;import java.util.Date; public class Test3 {public static void main(String[] args) throws Exception {Date dd=new Date();System.out.println(dd);/*// 년, 월, 일(추천되지 않음)int y=dd.getYear()+1900;int m=dd.getMonth()+1;int d=dd.getDate();System.out.println(y+"-"+m+"-"+d);*//* * SimpleDateFormat : Date 객체를 텍스트로 변환하거나 * 텍스트를 Date 객체로 변환 * yyyy-MM-dd HH:mm:ss * h:mm a -.. 더보기
Wrapper 클래스 예제) public class Test1 {public static void main(String[] args) {int a=10;Integer ii;// 기본자료형 -> Wrapper 클래스로 변환ii=new Integer(a);// JDK 5.0부터는 다음과 같이 가능ii=a; // autoboxing(컴파일시 ii=new Integer(a); 로 변환됨)// Wrapper -> 기본자료형으로 변환int b=ii.intValue();// JDK 5.0 부터는 다음과 같이 사용 가능int c=ii; // auto-unboxing(컴파일시 c=ii.intValue(); 로변환)System.out.println(a);System.out.println(b);System.out.println(c);System.. 더보기