[자바기초] 6. 클래스

★★★★★ 클래스 / 객체 ★★★★★

@ 클래스

Rectangle이라는 class 생성 (함수생성)

public class Rectangle { / int height; int width;

public int getHeight() { // getter함수: get변수명 값을 가져오는것 
	return height;
}

public void setHeight(int height) { // setter함수: get변수명 파라미터를 통해 함수의 값을 전해주는것
	this.height = height; // this는 자기자신을 가리킨다
}

public int getWidth() {
	return width;
	
}
public void setWidth(int width) {
	this.width = width;
}

/* get&set은 변수마다 늘 만들어주는 짝꿍같은것 
[alt + shife + s -> generate getters and stters (함수 생성 단축키) or 
우클릭 -> source -> generate getters and stters ( ) ] */

public int getArea() {
	return height * width;
} }

main 함수에서 Rectangle class 호출 (함수사용)

public class RecMain {

public static void main(String[] args) {
	Rectangle rec = new Rectangle();
	rec.setHeight(10); // rec이 참조하고 있는 클래스 찾아서 set함수를 실행시켜라
	rec.setWidth(10);
	
	System.out.println("사각형 넓이는: " + rec.getArea());

	rec.setHeight(5);
	rec.setWidth(5); ========================================================================== ▶ 사각형 넓이는: 100  ```


@ 객체


>> Circle class

public class Circle {
	
	int radius;
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(int radius) {
		this.radius = radius;
	}
	
	public double getCirclaArea() {
		return radius*radius*Math.PI;
	}

}

>> Main class

public static void main(String[] args) {

    Circle circle = new Circle();

    circle.setRadius(7); // 객체의 함수접근은 .으로 접근한다 
		
	System.out.println("원의 넓이는: " +circle.getCirclaArea());
}

▶ 위 코드 실행원리 객체원리 그림01

### 위 과정을 객체라 한다
객체생성(=인스턴스 생성) ; .clss를 메모리로 올리는 행위
  • 함수는 기본 크기를 4byte로 잡기 때문에 위의 사진에서 4byte로 잡혀있는 것
 Circle circle1 = new Circle();
 Circle circle2 = new Circle();
 Circle circle3 = new Circle();
 Circle circle4 = new Circle();
 Circle circle5 = new Circle();
 /* .class를 여러개 만드는 것이 아니라 하나의 .class를 복사해서 사용하는것 (주소를 참고하는 것 뿐이니까) 
 단! 각각의 주소는 다 다르게 방을 잡아서 설정 */

>> BankAccount.class 생성

 public class BankAccount {
	
	int balance = 0; // 계좌를 담는데 int면 큰일남(long으로) 근데 연습문제니까 int로
	
	public int deposit(int amount) {
		balance += amount;
		return balance;
	}
	
	public int withdraw(int amount) {
		balance -= amount;
		return balance;
	}
	
	public int checkMyBalance() {
		System.out.println("잔액 : " + balance);
		return balance;
	}

>> BankAccount.class 호출

public static void main(String[] args) {
		
		BankAccount ref1 = new BankAccount();
		BankAccount ref2 = ref1   // ref2는 ref1이 참조하는 주소를 참조
		
		ref1.deposit(3000);
		ref2.deposit(2000);
		ref1.withdraw(400);
		ref2.withdraw(300);
		ref1.checkMyBalance(); 
		ref2.checkMyBalance();
=====================================================================
 
잔액 : 4300
잔액 : 4300		

▶ 위 코드 실행원리 객체원리 그림02

// 실행되고나면 ref2는 방이 사라짐(OS에서 메모리 관리) 위에서 보듯이 주소를 같은 주소를 참고하기 때문에 결과값에 영향을 미친다