JAVA

· JAVA
/** * 추상클래스 */ public class TestAbstract { public static void main(String[] args) { Boy b = new Boy(); //AbsBoy bb = new AbsBoy(); // 객체를 생성할 수 없다 } // end of main } // end of class // 추상클래스로는 객체를 생성할 수 없다 abstract class AbsBoy { // 추상클래스 : 추상메서드를 가질 수 있는 클래스 int age = 10; // 일반 변수 int tall; abstract public void setTall(); // 추상 메서드 : 선언부만 작성한 메서드 // 메서드의 바디{}가 없음, ;으로 끝남 public String toString(..
· JAVA
/** * 상수 : 프로그램 종료시까지 같은 값 유지 */ public class TestFinal { public static void main(String[] args) { final float PI = 3.141592f; System.out.println(PI * 3); Week today = Week.SUNDAY; Week today2 = Week.SUNDAY; Week tomorrow = Week.MONDAY; if (today == today2) System.out.println("today == today2"); if (today.equals(today2)) System.out.println("today.equals(today2)"); System.out.println(today.name()..
· JAVA
/** * 객체를 1개만 생성해야할 경우 */ public class TestSingleton { public static void main(String[] args) { DB d = DB.getInstance(); // DB d = new DB(); System.out.println(d); pp(); } // end of main public static void pp() { DB d = DB.getInstance(); // DB d = new DB(); System.out.println(d); } } // end of class class DB { private static DB db; // null private DB() { // 기본생성자 } public static DB getInstance()..
· JAVA
/** * 상속관계의 클래스 객체생성 * 1. 모든 클래스에는 반드시 하나이상의 생성자가 존재해야한다. * 생성자가 없으면 컴파일러가 기본생성자를 추가해준다. * 2. 모든 생성자에는 반드시 또 다른 생성자를 호출해주는 코드를 첫번째줄에 작성해야한다 * 또 다른 생성자를 호출하지 않으면, super() 부모의 기본생성자코드를 넣어준다 */ public class TestSuper { public static void main(String[] args) { Ch c = new Ch(); } // end of main } // end of class class Gr { public Gr() { super(); } } class Pa extends Gr { public Pa() { super(); } } cl..
· JAVA
/** * 다형성, 오버라이딩 */ public class TestOverriding { public static void main(String[] args) { Par p = new Par(); Chil c = new Chil(); Par pp = c; // 다형성 if (pp instanceof Chil) { Chil cc = (Chil) pp; } p.over(); c.over(); System.out.println(pp.x);// 10 // 참조변수 타입의 변수가 출력 System.out.println(c.x); // 200 pp.over(); // chil, 메서드는 실제 저장된 객체의 메서드가 실행 c.over(); // chil } // end of main } // end of class ..
· JAVA
/** * 다형성 활용 */ public class TestPolymorphism2 { public static void main(String[] args) { Human h = new Human(); Boy b = new Boy(); Girl g = new Girl(); Baby ba = new Baby(); // 배열에 여러가지 타입을 저장해 보자 (배열 : 같은 타입만 저장가능) //Human hh = b; hh = g; hh = ba; hh = h; Human arrH[] = new Human[4]; arrH[0] = h; // 다형성 arrH[1] = b; // 다형성 arrH[2] = g; // 다형성 arrH[3] = ba;// 다형성 //Human arrH2[] = {h,b,g,ba}; f..
· JAVA
/** * 다형성(polymorphism) : 여러가지 타입으로 바꿀 수 있는 성질 * 부모의 참조변수에 자식의 객체를 담을 수 있는 성질 */ public class TestPolymorphism { public static void main(String[] args) { Parent p1 = new Parent(); Child1 c1_1 = new Child1(); Child2 c2_1 = new Child2(); GrandChild g1 = new GrandChild(); Parent p; // 참조변수 p = p1; p = c1_1; // 다형성 : 부모의 참조변수에 자식의 객체를 저장할 수 있다 p = c2_1; // 다형성 p = g1; // 다형성 Child1 c; c = c1_1; //c ..
· JAVA
/** * Object - hashCode */ public class TestObject3 { public static void main(String[] args) { Student s1 = new Student(1200,"하승우",100,50); Student s2 = new Student(1201,"김용진",90,90); Student s3 = new Student(1201,"정윤오",90,90); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode());//객체의 주소로 해시코드값을 생성 // 1. 내용물이 같은 객체는 hashCode 값은 동일해야한다 //- Objec..
· JAVA
/** * Object - toString : 문자열 정보를 리턴 */ public class TestObject2 { public static void main(String[] args) { Woman w1 = new Woman(24, "수지", 1000000000); Woman w2 = new Woman(24, "수지", 1000000000); System.out.println(w1.toString()); System.out.println(w1.equals(w2)); // true } // end of main } // end of class class Woman { int age; String name; int money; public Woman(int age, String name, int mon..
· JAVA
/** * Object : 최고 조상 클래스 */ public class TestObject { public static void main(String[] args) { Man m1 = new Man(true, 20, "정윤오"); Man m2 = new Man(true, 20, "정요"); if (m1==m2) { // 객체의 주소가 같은지 비교 System.out.println("m1==m2"); } if (m1.equals(m2)) { // 내용물이 같은지 비교 - Overriding 이 필요함 System.out.println("m1.equals(m2)"); } String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); String ..
'JAVA' 카테고리의 글 목록 (2 Page)