재귀함수 이용 import java.util.Scanner; public class FiboTest { public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("피보나치 위치 입력 : ");Scanner scan = new Scanner(System.in);long n = scan.nextInt();System.out.println("fibo[" + n + "] = " + fibo(n));} static long fibo(long n) {// TODO Auto-generated method stubif (n == 1 || n == 2)return 1;elsereturn fibo(n - 1) + ..
class Vehicle {int speed; void drive() {};void stop() {};} public class AirPlane extends Vehicle {int height; void takeOff() {};void landing() {}; public static void main(String arg[]) {AirPlane ap1 = new Vehicle(); //에러 부모 -> 자식간 인스턴스 할당 불가, 형변환 생략불가AirPlane ap2 = (AirPlane) new Vehicle(); // 강제 형변환 Vehicle v1 = new AirPlane(); //정상적인 코드, 형변환 생략가능Vehicle v2 = (Vehicle) new AirPlane(); // 강제 형변환..
과목 : 자바 클래스 기본, 인터페이스, 상속, 다중상속
package afternoon; public class Factorial { static int num = 5;public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("factorial "+num+" = "+factorial(num));} private static long factorial(int i) {// TODO Auto-generated method stubif(i==1)return 1;elsereturn i * factorial(i-1);}}
클래스 변수는 해당 클래스가 공통으로 사용하는 변수를 말하며 인스턴스 변수에 static 예약어를 사용한다. //클래스 변수를 이용한 제목을 지정하지 않은 문서의 번호 매기기package afternoon; public class DocumentTest { public static void main(String[] args) {// TODO Auto-generated method stubDocument dc = new Document();Document dd = new Document("Class Define");Document de = new Document();Document df = new Document("Serial Test");Document dg = new Document();Document..
일반상속과는 다르게 인터페이스는 다중상속이 허용된다 package afternoon; public class InterfaceInheritance { public static void main(String arg[]){C obj = new C();obj.method1();obj.method2();obj.method3();obj.method4();}}interface C1{void method1();void method2();}interface C2{void method3();}interface C3 extends C1,C2{void method4();}class C implements C3{ @Overridepublic void method1() {// TODO Auto-generated method s..
public class TestCar { public static void main(String[] args) {// TODO Auto-generated method stub Car car1 = new Car("white", "auto", 4); // 생성자로 인자 전달Car car2 = new Car(car1); // 생성자를 이용한 값 복사Car car3 = car2; // 보오오옥사 // car2와 3은 동일한 객체(깊은 복사, 주소까지 복사한 것)car2.color = "black";car3.trans = "stick"; // car1은 2와 3과 다른 객체(얕은 복사)car1.door = 3; System.out.println("car1 : " + car1.color + ", " + car1.d..