국비학원/공부

19일차 java(7)

mikan- 2023. 7. 2. 23:44

# CallByRef

/*
 * CallByRef(주소에 의한 전달방법) : 매개변수에 전달(객체(주소))해서 메소드를 호출하는 방법(원본의 값을 전달)
 * 									 내용을 변경O -> 원본의 내용도 변경O
 * 									 객체를 전달 -> 객체의 주소값을 전달
 * 									 주소(저장할 위치) = 포인터(pointer)
 * 
 */

class RGBColor {
	int r, g, b;
	
	public RGBColor(int r, int g, int b) {
		this.r = r;
		this.g = g;
		this.b = b;
	}
}

public class CallByRef {

	public static void main(String[] args) {
		
		RGBColor color = new RGBColor(-1, -1, -1);
		System.out.println("color : " + color);
		//int r = -1, g = -1, b = -1; // css (rgb(0 ~ 255)) -> 255 * 255 * 255
		
		System.out.println("before rgb : " + color.r + " " + color.g + " " + color.b);
		
		// 중간에 메소드를 이용 -> 전달 -> 색깔이 변경
		change_color(color);
		System.out.println("after rgb : " + color.r + " " + color.g + " " + color.b);
	}
	
	// 색깔을 변경 work method(RGBColor(자료형) color2(객체명)
	static void change_color(RGBColor color2) {
		System.out.println("color2 : " + color2);
		color2.r += 10; // r = r + 10 = 9
		color2.g += 50; // 49
		color2.b += 100; // 99
		System.out.println("메소드 내부 rgb : " + color2.r + " " + color2.g + " " + color2.b);
	}
}

 

# CallByValue

/*
 * CallByValue(값에 의한 전달방법) : 매개변수에 전달(기본자료형 8가지)
 *                                   전달해서 메소드를 호출하는 방법(원본의 값을 복사해서 전달)
 * 									 내용은 변경 되더라도 원본의 내용은 변경 안됨
 * 
 */

public class CallByValue {

	public static void main(String[] args) {
		
		int r = -1, g = -1, b = -1; // css (rgb(0 ~ 255)) -> 255 * 255 * 255
		
		System.out.println("before rgb : " + r + " " + g + " " + b);
		
		// 중간에 메소드를 이용 -> 전달 -> 색깔이 변경
		change_color(r, g, b);
		System.out.println("after rgb : " + r + " " + g + " " + b);
	}
	
	// 색깔을 변경
	static void change_color(int r, int g, int b) {
		r += 10; // r = r + 10 = 9
		g += 50; // 49
		b += 100; // 99
		System.out.println("메소드 내부 rgb : " + r + " " + g + " " + b);
	}
}

 

# 상속관계 및 생성자 (1)

// 상속과 생성자
public class SuperTest {

	public static void main(String[] args) {
		
		C c = new C(); // (1) 객체 c를 생성하면서 문자열처리 생성자호출
		
	}
}

// A
// 자바의 모든 클래스는 기본적으로 Object 클래스의 자식임
// extends Object 는 기본적으로 생략
class A extends Object{
	public A() {
		super(); // (4)
		System.out.println("난 A의 생성자, 호출됨"); // (5)
	}
	
}

// B
// class B extends Object
class B extends A {
	public B() {
		super(); // (3)
		System.out.println("난 B의 생성자, 호출됨"); // (6)
	}
	
	//추가(인수가 있는 생성자를 호출하는 경우에는 부모클래스에 반드시
	//     인수에 따라 처리해주는 생성자가 반드시 있어야 호출이 가능하다.
	public B(String s) {
		System.out.println("s 호출됨, " + s);
	}
}

//C
class C extends B { // 상속관계
	public C() {
		System.out.println("난 C의 생성자, 호출됨");
	}
	
	public C(String s) { 
		// super(s) s 는 나중에 추가됨 -> //추가 확인
		super(s); // (2) 부모클래스의 기본생성자를 호출
		System.out.println(s); // (7)
	}
}

 

# 상속관계 및 생성자 (2)

// super 키워드 <-> this 키워드(자식클래스의 객체를 가리키는 예약어)
// 부모클래스의 객체를 가리키는 예약어
class Person{
	String name; // null 상태
	int age = 20;
	
	void print() {
		System.out.println("Person 메소드(name) : " + name);
	}
}

// 기능을 Person 에게서 물려받음(상속)
class Man extends Person {
	
	// public Man() {} 없으면 만들어줆(생략되서 보임)
	
	// 상속을 받은 멤버변수와 이름이 같은 변수를 자식클래스에 또 선언이 가능
	int age = 40;
	/*-
	 * String name; // null 상태
		int age = 20;
	
	 	void print() {
			System.out.println("Person 메소드(name) : " + name);
		} 
		
		자식클래스에 부모와 동일한 멤버변수가 있는 경우
		super.멤버변수
		super.부모의 메소드를 호출
	 */
	
	// 오버라이딩(자식에게 맞게 내용을 수정)
	void print() {
		System.out.println("자식클래스의 age : " + age); // 40
		System.out.println("부모클래스의 age : " + super.age); // 20
		//System.out.println("Person 메소드(name) : " + name);
		super.print();
	}
}

public class SuperTest2 {
	
	public static void main(String[] args) {
		Man m = new Man();
		m.name = "홍길동";
		m.print();
	}
}

 

# 상속관계 (3)

// 부서의 팀장정보 저장 -> 중복코딩이 필요(업무상)
// 형식) class 자식클래스명 extends 부모클래스명
class Manager extends Employee {

	// 부모한테 물려받은 멤버변수
	/*-
	String name;
	int age;
	String sung;
	String address;
	long salary;
	*/

	// 부서명 -> 개발팀
	String department;

	// 객체배열 : 객체만 저장이 되는 배열(직원의 정보만)
	Employee sub[]; // new Employee[3]; int sub[] 직원만 저장

	public Manager() {
	}

	public Manager(String name, int age, String sung, long salary, String address, String department) {
		
		//super(); // Employe의 기본생성자를 호출(super는 원래 생략됨)
		super(name, age, sung, address, salary);
		/*
			1. 중복코딩이 생기면 부모의 생성자가 대신 저장시켜주는것으로 처리(재사용성)
		this.name = name;
		this.age = age;
		this.sung = sung;
		this.salary = salary;
		this.address = address;
		*/
		this.department = department;
	}
	
	// 오버라이딩 : 자식의 입장에서 맞게 내용을 다시 수정해주는 행위
	double bonus() {
		return salary * 3.0;
	}
	
	//------------추가----------------
	void display() {
		
		/* 중복코딩을 제거(부모코딩을 빌려와서 사용)
		System.out.println("====== 직원의 정보 ======");
		System.out.println("이름 : " + name); // this.getName()
		System.out.println("나이 : " + age);
		System.out.println("성별 : " + sung);
		System.out.println("주소 : " + address);
		System.out.println("급여 : " + salary);
		System.out.println("보너스 : " + this.bonus());
		*/
		super.display();
		
		// 추가
		System.out.println("담당부서명 : " + department);
		System.out.println("부하 직원의 수 : " + sub.length);
	}

}

public class Company {

	public static void main(String[] args) {

		// 1. 신입사원 3명 배정
		Employee e1 = new Employee("홍길동", 23, "남", "서울시 논현구", 1500);
		Employee e2 = new Employee("임꺽정", 35, "남", "대전시 중구", 1700);
		Employee e3 = new Employee("초선", 30, "여", "부산시", 1800);

		// 2. 부서배치(개발부)
		Manager m1 = new Manager("임시테스트", 42, "남", 3500, "서울시 강남구", "개발 1과");

		m1.sub = new Employee[3]; // Employee 데이터 저장공간 3개 생성
		m1.sub[0] = e1; // 홍길동의 정보가 들어감
		m1.sub[1] = e2;
		m1.sub[2] = e3;
		
		// 3. 부하직원의 정보, 팀장 출력
		for (int i = 0; i < m1.sub.length; i++) {
			System.out.println("직원명 : " + m1.sub[i].name);
			System.out.println("직원의 나이 : " + m1.sub[i].age);
			System.out.println("================================");
			m1.sub[i].display();
		}
		m1.display();
	}
}


// 신입사원에 대한 정보를 저장할 목적(추상화)(공기업)
public class Employee {

	// 멤버변수
	String name; // 이름
	int age; // 나이
	String sung; // 성별
	String address; // 주소
	long salary; // 급여 -> 공기업(보너스) -> 직원(150%), 팀장(300%)반영
	
	// source -> generate getter and setter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSung() {
		return sung;
	}

	public void setSung(String sung) {
		this.sung = sung;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public long getSalary() {
		return salary;
	}

	public void setSalary(long salary) {
		this.salary = salary;
	}
	
	// 2. 생성자 오버로딩 -> generate constructor using fields
	public Employee() {}
	public Employee(String name, int age, String sung, String address, long salary) {
		super();
		this.name = name;
		this.age = age;
		this.sung = sung;
		this.address = address;
		this.salary = salary;
	}
	
	// 보너스를 구해주는 메소드 -> 급여 * 1.5, 팀장급여 * 3.0
	double bonus() {
		return salary * 1.5;
	}
	
	// 3. 멤버변수의 값 출력
	// 하나의 클래스 내부에서 일반메소드에서 다른 일반메소드 호출O
	//                                           객체명.일반메소드명(~)
	void display() {
		System.out.println("====== 직원의 정보 ======");
		System.out.println("이름 : " + name); // this.getName()
		System.out.println("나이 : " + age);
		System.out.println("성별 : " + sung);
		System.out.println("주소 : " + address);
		System.out.println("급여 : " + salary);
		System.out.println("보너스 : " + this.bonus());
	}
	
}

'국비학원 > 공부' 카테고리의 다른 글

21일차 java(9)  (0) 2023.07.03
20일차 java(8)  (0) 2023.07.02
18일차 java(6)  (0) 2023.05.16
17일차 java(5)  (1) 2023.05.15
16일차 java(4)  (1) 2023.05.15