JAVA

6. 0712_ try-catch절, Getter and Setter

55yudi 2021. 10. 24. 12:13

- try-catch절로 어떤 코드를 잡아두면 에러가 생기더라도 비정상종료되지 않고 다음 코드를 실행함

  (단 결과는 이상하게 나올 수 있다.)

- try-catch { } 안에 들어간 변수는 { } 밖에서 쓰지 못하므로 미리 위에서 적는다.

  { } 안에는 변수타입 안적고 변수만 적으면 됨

try {
    a = Integer.parseInt(inputA);
    b = Integer.parseInt(inputB);
} catch (Exception e) {
    e.printStackTrace();
}

 

[Test1]

import com.lx.person.Baby;
import com.lx.person.Leg;
import com.lx.person.Person;

public class Test1 {

	public static void main(String[] args) {
	
		//1. add함수 호출
		int result1 = add(10, 10);
		System.out.println("더하기 결과1 : " + result1);
		
		//1. int가 아닌 String으로 함수 만들기
        //   add2함수 안에 변수를 만들어 넣고 호출하기
		String inputA = "가"; //2.
		String inputB = "10";
		int result2 = add2(inputA, inputB);
		System.out.println("더하기 결과2 : " + result2);
		
        
		//3. 매개변수가 비어있는 생성자함수
		Person person1 = new Person();
		person1.name = "홍길동1";
		person1.age = 20;
		System.out.println("첫번째 사람 이름 : " + person1.name);
		
		//3. 매개변수 들어있는 함수로 객체 생성
		String inputName = "홍길동2";
		int inputAge = 21;
		String inputMobile = "010-1234-5678";
		
		Person person2 = new Person(inputName, inputAge, inputMobile);
		System.out.println("두번째 사람 이름 : " + person2.name);
		
		person2.walk(1); //walk함수 호출
		
        
		//4. Person 안에 leg 넣기
		Leg leg1 = new Leg("왼쪽다리", 80);
		//person2.leg = leg1;
		person2.setLeg(leg1); //setLeg
		//System.out.println("다리 이름 : " + person2.leg.name);
		System.out.println("다리 이름 : " + person2.getLeg().name); //getLeg
		
		//5. Baby객체 생성
		Baby baby1 = new Baby();
		baby1.walk(2);
		
		
	}

//2. "가"로 했을 때 숫자변환 못해서 에러나므로 try-catch절이 필요

 

  --> try-catch절 코드 추가

	static int add(int a, int b) {
		int result = a + b;
		return result;
	}

	// (변형)try/catch --> 구멍에 문자열을 넣고 값은 숫자형으로 return되게 하기
	static int add2(String inputA, String inputB) {
		int a = 0;
		int b = 0;
		try {
			a = Integer.parseInt(inputA);
			b = Integer.parseInt(inputB);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int result = a + b;
		return result;
	}

 

//4. Person 안에 leg 넣기

      person1.leg.name

      인스턴스 객체 = 실제 객체

 

c.f. 인스턴스 vs. 객체

https://javakid.tistory.com/8

 

 

 

[Leg]

package com.lx.person;

public class Leg {
	
	public String name;
	int length;
	
	public Leg () {
		
	}
	
	public Leg (String name, int length) {
		this.name = name;
		this.length = length;
	}

}

 

[Person class]

package com.lx.person;

public class Person {
	public String name;
	public int age;
	public String mobile;
	
	public Leg leg;
    
	//4. setLeg, getLeg
	public void setLeg(Leg leg) {
		this.leg = leg;
	}
	public Leg getLeg() {
		return this.leg;
	}
	
	public Person () {
		
	}
	
	public Person(String name, int age, String mobile) {
		this.name = name;
		this.age = age;
		this.mobile = mobile;
		
		System.out.println("Person 생성자함수 호출됨");
	}
	
	public void walk(int speed) {
		System.out.println("사람 " + this.name + "이 " + speed + "km의 속도로 걸어갑니다.");
	}

}

//4. setLeg, getLeg

 

[Baby]

package com.lx.person;

public class Baby extends Person {
	
	//5.
	public void walk(int speed) {
		super.walk(speed); //6.
		System.out.println("사람 " + super.name + "이 " + speed + "km의 속도로 걸어갑니다.");
	}
}

//5. 재정의(다시 만듦)

//6. 부모가 갖고 있는 것도 실행하면서, 나(자식)는 또다른 것도 별로도 실행하겠다는 의미

      test1에서 실행하면 부모클래스의 walk함수도 같이 출력된다.