ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210420_3~4(변수 유효범위,생성자특징)
    JAVA(Sol)(정리대기중..언젠가) 2021. 4. 20. 13:09

    변수 유효 범위

    클래스(static) 변수 <=== 다른 클래스에서도 사용가능

    멤버 변수 <--- 전역변수로서 클래스 내부의 모든 함수에서 사용,호출,참조 가능

    지역 변수 <---  함수내부에서 선언되며, 함수밖에서 쓸수없다.

    모양(크기와갯수)으로 보는변수

    기본형: 8가지 boolean, char, byte, short, int, long, float, double

    배열 변수 : 동일크기가 반복

    객체형 변수 : 어떤 객체영역을 가리키기 위한 변수 - 해쉬코드로

                      4바이트(레퍼런스변수) 데이타형이 클래스이다.

    객체 배열 변수  : 같은크기인(4바이트) 객체형변수가 여러개

     

    범위특징 : 1. 바깥블럭의 변수는 내부블럭에서 사용가능

                  2. 역으로 안쪽변수는 바깥블럭에서 사용하지 못한다

    package chapter10;
    
    public class Variable {
    	String movie = "트로이";
    	
    	public void show() {
    	System.out.println("show 메소드영역 :" + movie);
    	}
    	
    	public void title() {
    		String movie ="바람의 전설";
    		System.out.println("title 메소드영역:" + movie);
    		System.out.println("title 메소드영역:" +this.movie);
            		//this를사용하여 전역변수의movie를가져옴
    	}
    
    	public static void main(String[] args) {
    		Variable v = new Variable();
    		v.show();
    		v.title();
    
    	}
    
    	}
    
    package chapter10;
    
    public class Block {
    	
    	String block ="바람의 전설";
    	
    	public static void main(String[] args) {
    		String block = "트로이";
    		System.out.println("Movie :" + block);
    
    		int i =0;
    		{
    			String block2="달콤한인생";
    			System.out.println("Movie :" + block2);
    			
    			for(int j=0; j<5; j++) {
    			} //for문 안에서 선언된 변수 j는 반복문을 벗어날 수 없습니다
    			for(i=0; i<5; i++) {
    			} // for문 안의 변수 i는 외부변수이므로 반복문 안과밖에서도 사용가능
    			 	System.out.println("block2: " + block2);
    		} // block     여기서 block2 호출이 가능할까요 테스트하세요
    				//System.out.println("block2: " + block2); 여기선 안된다
    	} //main
    } // class
    

     


     

    생성자특징

    1.클래스내부에 생성자의 정의가 없으면 기본생성자가 자동으로 만들어져서 반환된다 - 인스턴스를 생성할때

    package chapter10;
    
    class School2 {
    	int kuk = 0;
    	int eng = 0;
    	int tot = 0;
    
    	// 기본생성자의 생략 (자동으로만들어져서 반환됨)
    	public int hap() {
    		tot = kuk + eng;
    		return tot;
    	}
    }
    
    public class SchoolMain2 {
    
    	public static void main(String[] args) {
    		School2 sc2 = new School2(); // 기본생성자의 호출
    		sc2.kuk = 90;
    		sc2.eng = 100;
    		System.out.println("hap :" + sc2.hap());
    
    	}
    
    }
    
    package chapter10;
    
    class School3 {
    	int kuk = 0;
    	int eng = 0;
    	int tot = 0;
    
    	public School3() { // 기본생성자 정의
    	}
    
    	public int hap() {
    		tot = kuk + eng;
    		return tot;
    	}
    }
    
    public class SchoolMain3 {
    
    	public static void main(String[] args) {
    		School3 sc3 = new School3(); // 생성자 호출
    		sc3.kuk = 90;
    		sc3.eng = 100;
    		System.out.println("hap :" + sc3.hap());
    
    	}
    
    }
    

     

    2.클래스에 인자가 있는 생성자가 존재하면, 기본생성자는 자동으로 만들어지지 않는다

    package chapter10;
    
    class School4 {
    	int kuk = 0;
    	int eng = 0;
    	int tot = 0;
    
    	// 기본 생성자가 반드시 필요한 경우
    	public School4() {
    	}
    
    	public School4(int kuk, int eng) { // 생성자 오버로딩
    		//클래스에 인자가 있는 생성자가 존재하면, 기본생성자는 자동으로 만들어지지 않는다
    		this.kuk = kuk;
    		this.eng = eng;
    	}
    
    	public int hap() {
    		tot = kuk + eng;
    		return tot;
    	}
    }
    
    public class SchoolMain4 {
    
    	public static void main(String[] args) {
    		School4 sc4 = new School4();
    		sc4.kuk = 90;
    		sc4.eng = 100;
    		System.out.println("hap : " + sc4.hap());
    
    		School4 sc = new School4(85, 95);
    		System.out.println("hap : " + sc.hap());
    
    	}
    
    }

    3.생성자가 여러개라는 것은 인스턴스 만드는 방법이 여러개다 라고 해석하면 된다.

    4.각각의 생성자들은 오버로딩으로 정의된다.

    package testjava;
    
    class School{
    	int kuk=0;
    	int eng=0;
    	int mat=0;
    	
    
    	public School() {
    	}
    
    	public School(int kuk) {
    		this.kuk = kuk;
    	}
    
    	public School(int kuk, int eng) {
    		new School(kuk);
            //this.kuk=kuk;
    		this.eng = eng;
    	}
    
    	public School(int kuk, int eng, int mat) {	
    		new School(kuk, eng);
            //this.kuk=kuk;
    		//this.eng=eng;
    		this.mat = mat;
    	}
    	
    	// 생성자자동생성을 이용한 생성자생성과 오버로딩,중복코딩 간략화
    	
    }
    package chapter10;
    class School{
    	int kuk=0;
    	int eng=0;
    	int mat=0;
    	int tot=0;
    
    	public School() {
    	}
    	public School(int kuk) {
    		this.kuk=kuk;
    	}
    	public School(int kuk, int eng) {
    		this.kuk=kuk;
    		this.eng=eng;
    	}
    	public School(int kuk, int eng, int mat) {
    		this.kuk=kuk;
    		this.eng=eng;
    		this.mat=mat;
    	}
    	public int hap() {
    		tot=kuk+eng+mat;
    		return tot;
    	}
    }
    
    public class SchoolMain {
    
    	public static void main(String[] args) {
    		School sc1= new School(90);
    		System.out.println("hap : " + sc1.hap());
    
    		School sc2 = new School(90,90);
    		System.out.println("hap : " + sc2.hap());
    		
    		School sc3 = new School(90,80,100);
    		System.out.println("hap : " + sc3.hap());
    
    	}
    
    }
    

     

Designed by Tistory.