/** * Overriding(오버라이딩) : * ("상속") 부모클래스에서 가지고 있는 멤버 "메서드"의 내용을 * 자식클래스에서 "재정의"해서 사용하는 기법 */ public class TestOverriding { public static void main(String[] args) { Point pp = new Point(2,3); pp.print(); Point3D ppp = new Point3D(10,20,30); ppp.print(); } // end of main } // end of class class Point { // 2차원 좌표를 추상화한 클래스 int x, y; public Point() { } public Point(int x, int y) { this.x = x; this.y..
JAVA
/** * 최고 조상 클래스 : Object * 클래스가 가지고 있어야할 멤버 (11개의 메서드)를 선언해 놓음 */ public class TestObject { public static void main(String[] args) { A a = new A(); } // end of main } // end of class class A extends Object { } // 아무것도 상속받지 않은 클래스에 extends Object 가 들어간다 class B1 extends A { } class B2 extends A { } class C extends B1 { }
import java.io.BufferedReader; import java.io.InputStreamReader; /** * 자바의 상속 - 단일 상속만을 허용한다, 부모를 두개이상 둘 수 없다 */ public class TestShape { public static void main(String[] args) { Circle c1 = new Circle(new Point(3,4), 10, "빨강"); Circle c2 = new Circle(3, 4, 10, "빨강"); Point p[] = {new Point(1,1),new Point(2,2),new Point(3,3)}; Triangle t1 = new Triangle(p); Triangle t2 = new Triangle (new Point[..
/** * 클래스의 상속 (inheritance) : 클래스간의 (부모-자식)관계를 맺어주는 것 * 부모의 멤버만 상속 받을 수 있다. (전역변수, 메서드) */ public class TestPoint { public static void main(String[] args) { Point pp = new Point(); pp.x = 3; pp.y = 4; Point3D ppp = new Point3D(); ppp.x = 10; ppp.y = 20; ppp.z = 30; } // end of main } // end of class class Point { // 2차원 좌표를 추상화한 클래스 int x; int y; public Point() { } public Point(int x, int y) { t..
/** * class 상속 활용 */ public class TestTV { public static void main(String[] args) { Tv t = new Tv("삼성파브", 11, true); CaptionTv ct = new CaptionTv("삼성파브",11,true,false); ct.print(); ct.powerOnOff(); ct.print(); ct.caption=true; ct.displayCaption("호랑이가 살금살금 다가온다"); } // end of main } // end of class class CaptionTv extends Tv { boolean caption; public CaptionTv() { super(); } public CaptionTv(Stri..
/** * Overloading(오버로딩) : 같은 class 내에서 메서드명을 동일하게 사용하는 기법 * 조건 : 매개변수의 개수 or 타입 or 순서가 달라야한다 */ public class TestOverloading { public static void main(String[] args) { MyMath mm = new MyMath(); mm.sum(6); mm.sum(3,9); mm.sum(3,9,7); mm.sum(true); System.out.println(); System.out.println(1); System.out.println(3.14); System.out.println(3.14f); System.out.println(true); System.out.println("안녕"); }..
/** * static method */ public class TestStaticMethod { public static void main(String[] args) { SM s1 = new SM(); SM s2 = new SM(); } // end of main } // end of class class SM { static int sa; int a; static void sprint() { } void print() { } }
/** * stack */ public class TestCallStack { public static void main(String[] args) { new GG().m1(); } // end of main } // end of class class GG { void m1(){ m2(); System.out.println("m1"); } void m2(){ m3(); System.out.println("m2"); } void m3(){ System.out.println(3/0); } }
import java.util.Random; public class TestCardGame { public static void main(String[] args) { CardGame cg = new CardGame(); // 생성자에서 card[]을 완성함 cg.printCard(); cg.shuffle(); cg.printCard(); } // end of main } // end of class class CardGame { Card card[]; // 52개의 카드 public CardGame() { // 기본 생성자 char kind[] = {'◆','♥','♠','♣'}; // 무늬 배열 4 String number[] = {"A","2","3","4","5","6", "7","8","9","..
/** * 변수 Variable scope */ public class TestVariable { int s; void print() { System.out.println(s); } public static void main(String[] args) { ABC abc = new ABC(); abc.printABC(); } // end of main } // end of class class ABC { // 선언된 위치에 따라 구분 int a; // 전역변수(글로벌 변수, 필드) : 클래스 안쪽 && 메서드 바깥쪽 // 각 타입의 초기값이 저장된다 static int c; // static 변수, 공통변수, 공유변수, class변수 // 클래스의 정보가 처음 로딩될 때 ~ 프로그램 종료시까지 int d;..