본문 바로가기
수업(국비지원)/Java

[Java] chap6: 클래스와 객체 - This 예약어, This 예약어 Exam6

by byeolsub 2023. 4. 15.
  • This 예약어
 ===this 예약어===
  
this() 생성자 : 같은 클래스내에 다른 생성자 호출시 사용
                호출하는 생성자에 첫번쨰 줄에 구현해야함
  
this 참조변수 : 자기참조 변수. 자신의 객체를 참조하는 참조값을 저장하는 변수
                  클래스멤버에서 사용불가.(예시 - static에서 사용불가)
                  인스턴스메서드에 지역변수로 자동 설정됨.
                  지역변수와 멤버변수의 구분시 사용됨.
                  this.인스턴스멤버명

 

 📌

package chap6;

class Car2{
	String color; //멤버변수
	int number; //멤버변수
	Car2(String color,int number){ //지역변수
		System.out.println("(String,int) 생성자");
		this.color = color; //멤버 변수와 지역변수가 같은 이름일 때 구분시 사용.
		this.number = number;
	}
	Car2(){
		this("white",1); //다른 생성자 호출. (String,int) 생성자 호출됨
		System.out.println("() 생성자");
	}
	Car2(String color){
		this(color,1); //매개변수
		System.out.println("(String) 생성자");
	}
	Car2(int number){
		this("white",number);
		System.out.println("(int) 생성자");
	}
	Car2(Car2 c){
		this(c.color,c.number);
		System.out.println("(Car2) 생성자");
	}
	public String toString() { //인스턴스 메서드
		return color+":"+number;
	}
//	static void method() { // 클래스 멤버에서는 this 사용 불가
//		System.out.println(this);
//	}
	 void method1() { //인스턴스 멤버에서는 this 사용 가능
		 //this 참조변수는 인스턴스 메서드의 지역변수로 자동 설정 됨
		System.out.println(this);
	}
}
public class TishEx1 {
	public static void main(String[] args) {
	  Car2 c1 = new Car2();
	  Car2 c2 = new Car2("blue",3456);
	  Car2 c3 = new Car2("red");
	  Car2 c4 = new Car2(5678);
	  Car2 c5 = new Car2(c1);
	  System.out.println(c1);
	  System.out.println(c2);
	  System.out.println(c3);
	  System.out.println(c4);
	  System.out.println(c5);
	}

}

📌 This 예약어 Exam6

package chap6;
/*
 * 원(Circle2) 클래스 구현하기
 *  1. 멤버변수
 *       반지름(r),x좌표(x),y좌표(y), 원의번호(no)
 *       원의번호 생성 변수 count
 *  2. 생성자 : 구동 클래스에 맞도록 설정하기     
 *  3. 멤버메서드
 *     (1) double area()  : 원의 넓이 리턴. Math.PI 상수 사용
 *     (2) double length(): 원의 둘레 리턴. Math.PI 상수 사용
 *     (3) void move(int a, int b): x,y좌표를 x+a, y+b로 이동.
 *     (4) void scale(double m): 반지름을 m배 확대/축소. r=r*m
 *     (5) String toString() :
 *         1번원 : 반지름:10, 좌표:(10,10), 넓이:314.xxx, 둘레:xxx.xxx
 *         ... */
class Circle2{
	double r; //반지름
	int x,y; //좌표
	int no; //원의 번호
	static int count; //원의 번호 생성
	Circle2(double r,int x,int y){ //1.
		this.r = r;
		this.x = x;
		this.y = y;
		no = ++count;
	}
	Circle2(int x,int y){ //2.
		this(1,x,y);
	
	}
	Circle2(double r){ //3.
		this(r,0,0);
		
	}
	double area() { //원의 넓이 리턴
		return r*r*Math.PI;
	}
	double length() { //원의 둘레 리턴
		return r*2*Math.PI;
	}
	void move(int a,int b) {
		this.x += a;
		this.y += b;
	}
	void scale(double m) {
	        this.r *= m;
	}
	public String toString() {
//		return no+"번원 :"+"반지름"+r+", 좌표:("+x+","+y+"), 넓이:"+area()+", 둘레:"+length();
		//String.format() : 문자열에서 서식문자 사용함수
		//System.out.printf() 함수의 사용방법과 동일
		return String.format("%d번원: 반지름:%.0f, 좌표(%d,%d), 넓이:%.2f, 둘레:%.2f",no,r,x,y,area(),length());
	}
}
public class Exam6 {
	public static void main(String[] args) {
		Circle2[] carr = new Circle2[3];
		//carr[0] : Circle2타입의 참조변수
		carr[0] = new Circle2(10,10,10); //반지름 : 10,(10,10) 좌표
		carr[1] = new Circle2(20,20);    //반지름 : 1, (20,20) 좌표
		carr[2] = new Circle2(100);      //반지름 : 100, (0,0) 좌표
		for(Circle2 c : carr) {
			System.out.println(c);
			c.move(10,10);
			System.out.println(c);
			c.scale(3);
			System.out.println(c);
		}
	}
}