'오라클자바커뮤니티'에 해당되는 글 8건
- 2014.10.12 재귀호출
- 2014.10.12 JVM 메모리 구조
- 2014.10.12 메서드의 사용
- 2014.10.12 메서드의 정의 및 작성
- 2014.10.12 클래스의 정의
- 2014.10.12 클래스의 이해
- 2014.10.12 다형성 예제1
- 2014.10.12 상속과 컴포지션
public class FactorialTest { public static void main(String[] args) { System.out.println(factorial(3)); System.out.println(factorialTri(5)); } static long factorial(int n){ long result = 0; if(n == 1) { result = 1; } else { result = n * factorial(n-1); } return result; } static long factorialTri(int n) { return (n==1) ? 1 : n * factorialTri(n-1); } }
'오라클자바커뮤니티 > 자바' 카테고리의 다른 글
JVM 메모리 구조 (0) | 2014.10.12 |
---|---|
메서드의 사용 (0) | 2014.10.12 |
메서드의 정의 및 작성 (0) | 2014.10.12 |
클래스의 정의 (0) | 2014.10.12 |
클래스의 이해 (0) | 2014.10.12 |
Method Area [class data(cv)]
Call Stack [main(lv)]
Heap[instance(iv)]
메서드가 호출되면 수행에 필요한 메모리를 스택에 할당
메서드가 수행을 마치면 사용했던 메모리 반환 및 스택에서 제거
호출스택 제일 위의 메서드가 현재 실행중인 메서드
아레의 메서드가 바로위의 메서드를 호출한 메서드
Call Stack [main(lv)]
Heap[instance(iv)]
메서드가 호출되면 수행에 필요한 메모리를 스택에 할당
메서드가 수행을 마치면 사용했던 메모리 반환 및 스택에서 제거
호출스택 제일 위의 메서드가 현재 실행중인 메서드
아레의 메서드가 바로위의 메서드를 호출한 메서드
public class CallStackTest { static void first() { System.out.println("1. first()가 시작되었음"); second(); System.out.println("2. first()가 끝났음"); } static void second() { System.out.println("3. second()가 시작되었음"); System.out.println("4. second()가 끝났음"); } public static void main(String[] args) { System.out.println("5. main(String[] args)이 시작되었음"); first(); System.out.println("6. main(String[] args)이 끝났음"); } }
public class MyMathTest { public static void main(String[] args) { MyMath mm = new MyMath(); long result1 = mm.add(5, 3); long result2 = mm.subtract(5, 3); long result3 = mm.multiply(5, 3); double result4 = mm.divide(5, 3); System.out.println("add(5, 3) = " + result1); System.out.println("subtract(5, 3) = " + result2); System.out.println("multiply(5, 3) = " + result3); System.out.println("divide(5, 3) = " + result4); } } class MyMath { long add(long a, long b) { return a+b; } long subtract(long a, long b) { return a-b; } long multiply(long a, long b) { return a*b; } double divide(double a, double b) { return a/b; } }
'오라클자바커뮤니티 > 자바' 카테고리의 다른 글
재귀호출 (0) | 2014.10.12 |
---|---|
JVM 메모리 구조 (0) | 2014.10.12 |
메서드의 정의 및 작성 (0) | 2014.10.12 |
클래스의 정의 (0) | 2014.10.12 |
클래스의 이해 (0) | 2014.10.12 |
특정한 작업을 수행하기 위한 명령문의 집합
필요요소
1. 하나의 메서드는 한가지 기능만 수행하도록 작성
2. 반복적으로 수행되어야하는 여러문장을 하나의 메서드로 정의
3. 관련된 여러문장을 하나의 메서드로 만들어 놓는것이 좋다.
메서드의 작성
선언부와 구현부로 분리
선언부는 리턴타입, 메서드명, ()안에 매개변수(파라미터)를 선언
구현부는 메서드가 호출되었을때 수행되어야 할 코드를 작성
리턴타입 메서드명 (데이터타입(클래스명) 변수명(객체명)) {
// 메서드 호출시 수행될 코드
}
필요요소
1. 하나의 메서드는 한가지 기능만 수행하도록 작성
2. 반복적으로 수행되어야하는 여러문장을 하나의 메서드로 정의
3. 관련된 여러문장을 하나의 메서드로 만들어 놓는것이 좋다.
메서드의 작성
선언부와 구현부로 분리
선언부는 리턴타입, 메서드명, ()안에 매개변수(파라미터)를 선언
구현부는 메서드가 호출되었을때 수행되어야 할 코드를 작성
리턴타입 메서드명 (데이터타입(클래스명) 변수명(객체명)) {
// 메서드 호출시 수행될 코드
}
public class ReturnTest { public static void main(String[] args) { ReturnTest r = new ReturnTest(); int result = r.add(3, 5); System.out.println(result); int[] resultArr = new int[1]; r.add(3, 5, resultArr); System.out.println(resultArr[0]); } int add(int a, int b) { return a + b; } void add(int a, int b, int[] result) { result[0] = a + b; } }
public class TimeTest { public static void main(String[] args) { // 메서드 변수 사용 int hour; int minute; int second; // 메서드 변수의 나열 int hour1, hour2, hour3; int minute1, minute2, minute3; int second1, second2, second3; // 배열화 int[] hourArr = new int[3]; int[] minuteArr = new int[3]; int[] secondArr = new int[3]; // UserDefinedType1 Time t1 = new Time(); Time t2 = new Time(); Time t3 = new Time(); // UserDefinedType2 Time[] t = new Time[3]; } } class Time { int hour; int minute; int second; // 이하 제약조건 /*public int getHour() { return hour; } public void setHour(int hour) { if(hour >= 0 && hour < 24) { this.hour = hour; } } public int getMinute() { return minute; } public void setMinute(int minute) { if(minute > 0 && minute <= 60) { this.minute = minute; } } public int getSecond() { return second; } public void setSecond(int second) { if(minute > 0 && minute <= 60) { this.second = second; } }*/ }
클래스 : TV의 설계도
객체 : TV
클래스를 인스턴스화 하여 인스턴스(객체)를 생성
TV의 클래스화
속성 : 멤버변수
기능 : 메서드
변수 : 크기, 길이, 높이, 색상, 볼륨, 채널 등
기능 : 커기, 끄기, 볼륨 높이기 / 낮추기, 채널변경하기
package score; public class TvTest { public static void main(String[] args) { Tv t1; // Tv클래스타입의 참조변수 t를 선언 / 메모리에 t를 위한 공간이 마련됨 t1 = new Tv(); // 연산자 new에 의해 Tv클래스의 인스턴스가 메모리의 빈 공간에 생성 / 각 항목들은 기본값으로 초기화 / 대입연산자로 인해서 해당 주소값이 객체t에 지정 t1.channel = 7; // 참조변수 t에 저장된 주소에 있는 인스턴스의 멤버변수 channel에 7을 저장 t1.channelDown(); // t가 참조하고 있는 Tv인스턴스의 channelDown메서드를 호출 channel에 저장되어있는값을 1감소 System.out.println("현재 t1의 채널은 " + t1.channel + "입니다."); Tv t2 = new Tv(); // 새 객체 생성 다른 메모리 주소를 참조하는 빈 항목들을 t2에 지정 System.out.println("현재 t2의 채널은 " + t2.channel + "입니다."); t2 = t1; // 바라보는 참조 주소를 복사 t1.channelUp(); // t1인스턴스가 바라보는 멤버변수 channel의 값을 1증가 System.out.println("현재 t2의 채널은 " + t2.channel + "입니다."); } } class Tv { String color; boolean power; int channel; void power() { power = !power; } void channelUp() { ++channel; } void channelDown() { --channel; } }
public class Poly { public static void main(String[] args) { A a = new B(); a.aaa(); a.bbb(); ((B)a).bbb(); ((B)a).ccc(); } } class A { public void aaa() { System.out.println("aaa"); } public void bbb() { System.out.println("bbb"); } } class B extends A{ public void bbb() { System.out.println("bbb1"); } public void ccc() { System.out.println("ccc"); } }
1. Point를 상속하는 경우 원은 반지름 값을 갖는 하나의 점이다(a Circle is a Point with a radius.) class Point { private double x, y; Point(double x, double y) { this.x = x; this.y = y; } double getX() { return x; } double getY() { return y; } } class Circle extends Point { private double radius; Circle (double x, double y, double radius) { super (x, y); // Call Point (double x, double y) this.radius = radius; } double getRadius () { return radius; } } 2. 포인트를 상속하지 않고 Circle안에서 new 하는 경우 (원은 한 점과 반지름을 가지고 있다(a circle has a point and a radius)) ==> 아래 코드는 Point의 getX, getY를 다시 만듦으로 redundant code 로 인한 유지보수 어려움(코드의 재사용 실패) 문제가 있고 class Point { private double x, y; Point(double x, double y) { this.x = x; this.y = y; } double getX() { return x; } double getY() { return y; } } class Circle { private Point p; private double radius; Circle(double x, double y, double radius) { p = new Point(x, y); this.radius = radius; } double getX() { return p.getX(); } double getY() { return p.getY(); } double getRadius() { return radius; } } [예제] package day3; class Point { private double x, y; Point(double x, double y) { this.x = x; this.y = y; } double getX() { return x; } double getY() { return y; } } //상속 class Circle1 extends Point { private double radius; Circle1 (double x, double y, double radius) { super (x, y); // Call Point (double x, double y) this.radius = radius; } double getRadius () { return radius; } } //컴포지션 class Circle2 { private Point p; private double radius; Circle2(double x, double y, double radius) { p = new Point(x, y); this.radius = radius; } double getX() { return p.getX(); } double getY() { return p.getY(); } double getRadius() { return radius; } } public class CircleTest { public static void main(String[] args) { Circle1 c1 = new Circle1(1,1, 10); System.out.println(c1.getX()); System.out.println(c1.getX()); System.out.println(c1.getRadius()); Circle2 c2 = new Circle2(1,1, 10); System.out.println(c2.getX()); System.out.println(c2.getX()); System.out.println(c2.getRadius()); } }