본문 바로가기
몰입형학습

[Java] 모듈화( method and function) and Class and this의 이해

by jackypark 2022. 6. 27.

/*
 * 모듈화
 * 1.call by name
 * 2.call by value
 * 3.call by reference
 * 
 * => call by address=> c언어
 * 
 * static method //미리메모리 생성
 *  - instance field를 메소드 내에서 사용불가
 *  - static field를 사용할 수 있다. 
 * method is a way to field(capsulate)

 * instance method
 *  - static field를 사용할 수 있다
 *  - instance field를 사용할 수 있다.
 * 
 *  오버로딩// Overloading
 *  1. 동일한 함수명으로 여러개의 함수를 제공
 *  2. 매개변수의 타입이 다를경우
 *  3. 매개변수의 갯수가 다를 경우
 *  4. 2,3번중 하나만 달라도 가능
 *  5. 목적 : 편할려고
 *  6. 같은 목적으로 사용하는 것끼리만 만들어라.. ( 주의사항)
 *  7. 같은 클래스내에서 사용.
 * 
 */

public class Day5 {

	public static void disp() { //call by name ( no return no 매개변수 받기)
		System.out.println("Hello");//앞에 대문자 class 고로 static 메소드(class 메소드)
	}
	
	public  void disp2() {
		System.out.println("Hello2");//앞에 대문자 class 고로 static 메소드(class 메소드)
	}
	
	public void disp3(int a) {// call by value 
		System.out.println(a);
	}
	
	public int disp4() {//call by value
		
		return 10;//only one return type is possible
	}
	
	public int disp5(int a) {//call by value
		
		return 20;
	}
	
	public int disp(int a,int b) {//call by value + overloading
		
		return a+b;
	}
	
	public static void main(String[] args) {
	
		
		Day5.disp();// static 메소드를 사용하더라도 앞에 클래스명을 되도록이면 붙어준다

		Day5 d5;// reference 변수 생성
		d5=new Day5();//객체생성
	
		d5.disp2();
		//d5.disp(); disp();  instance인지 static인지 햇갈리기때문에 이 두 구분은 되도록이면 쓰지말자
		
		d5.disp3(200);
		int data=d5.disp4();
		System.out.println(data);
		int data2=d5.disp5(0);
		System.out.println(data2);
		
		int data3=d5.disp(100, 200);
		System.out.println(data3);
	}

}


 *  클래스의 구성요소
 * 
 * 1.field 
  :data
  -외부에서 객체를 사용할 때 지속적으로 사용되어져야하는 데이터
  -접근지정자는 주로 private 지정
  -instance field
  -static field
  -필드 사용은 한상 메소드를 통해서 사용하게끔 해라...
 * 
 * 2.constructor : 객체 생성시 자동호출되어진다. Just one time in this moment
  -(생성자함수)
  -디폴트기능: 객체등록 (생성자가 호출이 안될경우 객체를 생성할 수 없다.
      : 생성자를 명시적으로 만들지 않을 경우 디폴트 생성자를 제공해준다.
  -오버로딩이 가능 ( 객체를 다양하게 만들 수 있다.  )
  - No return type
  - 함수명이 무조건 클래스명과 동일하게 만들어야 한다.
  - 기능 : 객체생성시 필드 초기화
  - 접근 지정자는 주로 public으로 사용한다. (외부에서 사용이 용이하기 때문에
 *  
 * 
 * 3.method
  -외부에서 필드를 접근하는 목적 
  -public으로 주로 지정
 * 
 * 
 * 자바는 잘게 쪼갠다는 개념을 잊지말자  + good coding is short and easy coding
 * Java is 상속구조
  최상위클래스 Object
 
  this (There is NO!! this in static)
  : 항상 인스턴스메소드의 첫번째 멤버변수로 항상 존재하고 있다
  사용만 가능하고 만들 수 는 없다.
  - 객체를 구별해준다 -> 메모리를 절약할 수 있다.

this가 생략하고 있는부분이 Day5 dt부분이다

public class Day5{
	private int a;
    
    public static void setA1(Day5 dt,int a) {
		dt.a=a;
	}
	public static int getA1(Day5 dt) {
		return dt.a;
	}
    
    public static void main(String[] args) {
   		 Day5 dt=new Day5();
   		 //setter 호출 1000입력
			Day5.setA1(dt,1000); // static 메소드 사용
		
		//getter호출 1000 출력
			System.out.println(Day5.getA1(dt)); //static 메소드 사용
    
    }


}

this()
   : 생성자에서 오버로딩된 또 다른 생성자를 호출할 때 사용
: 관리가 용이하다

public class Day5 {
	
	int a;
	int b;
	
	public Day5() {
		//this.a=0;
		//this.b=0;
		this(0,0);
	}
	public Day5(int a) {
		//this.a=a;
		//this.b=0;
		this(a,0);
	}
	public Day5(int a,int b) {
		this.a=a;
		this.b=b;
	}
	
	public int getA() {
		return a;
	}
	public int getB() {
		return b;
	}
	
	public static void main(String[] args) {
			Day5 d=new Day5();
			Day5 d1=new Day5(10);
			Day5 d2=new Day5(20,30);
			
			System.out.println(d.getA()+"\t"+d.getB()); //0,0
			System.out.println(d1.getA()+"\t"+d1.getB()); //10,0
			System.out.println(d2.getA()+"\t"+d2.getB()); //20,30
			
	}

}

'몰입형학습' 카테고리의 다른 글

OSI 계층 모델 7계층 ~  (0) 2022.09.04
[Java]컬렉션 프레임워크  (0) 2022.07.07
[JAVA] 배열정리+ String 객체이해  (0) 2022.06.23
진법변환  (0) 2022.06.23
[JAVA] Stack and Heap  (0) 2022.06.22

댓글