ExMethod
// 리턴값이 없는 메소드 public void method01() { System.out.println("method01() called!"); int num = this.method02(); // 이 클래스의 method02 호출 // method02를 호출하여 받아온 수를 출력 System.out.println("num = " + num); // method03을 호출했을 때, 매개변수로 넘긴 수가 2배가 되도록 할 것. int result = method03(num); System.out.println("result = " + result); } // return값이 10인 메소드 public int method02() { System.out.println("method02() called!"); return 10; } // 위 method01에서 매개변수를 2배로 반환받으려고 했으므로 public int method03(int num) { // 2배를 해서 반환 return num * 2; }
ExMethodMain
// 클래스 객체 선언 ExMethod ex = new ExMethod(); /* * method01()만 호출해서, * method02()또한 호출할 수 있도록 수정해보자. * 단, method02()를 ExMethodMain에서 직접 호출하면 안된다. */ ex.method01();
→
method01() called!
method02() called!
num = 10
result = 20
Calculator
/* * excute -> average -> sum * -> average -> excute */ public void excute() { System.out.println("excute method called!"); // average 함수로부터 매개변수 두 개의 평균을 받는다. int result = this.average(20, 20); System.out.println("두 수의 평균 : " + result); } // 매개 변수 두 개의 평균을 구해 반환한다. public int average(int num1, int num2) { System.out.println("average method called!"); // 단, 매개 변수 두 개의 합을 구하는 메소드로부터 값을 반환받고 2로 나눈다. int result = this.sum(num1, num2) / 2; // sum 메소드로부터 두 수의 합을 받아 저장한 값에서 2를 나눈 값을 반환. return result; } public int sum(int num1, int num2) { System.out.println("sum method called!"); // 두 수의 합을 반환 return num1 + num2; }
CalculatorMain
Calculator calc = new Calculator(); calc.excute();
→
excute method called!
average method called!
sum method called!
두 수의 평균 : 20
Time
/* * 기능을 한 메소드에 다 때려박기보다는, * 각 기능을 각 메소드로 세분화시키는 것이 낫다. */ // 매개변수로 시간을 받아 몇 초인가를 계산해 출력 public void hourToSecond(int hour) { // 시간을 분으로 전환 후, 분을 초로 전환하여 받는다. int minutes = this.toMinute(hour); int seconds = this.toSecond(minutes); // 계산된 초 출력 System.out.println(hour + "시간은 " + seconds + "초 입니다."); } // 전달받은 시간을, 분으로 전환 후 반환 public int toMinute(int hour) { int minutes = hour * 60; return minutes; } // 전달받은 분을, 초로 전환 후 반 public int toSecond(int minute) { int seconds = minute * 60; return seconds; }
TimeMain
Scanner sc = new Scanner(System.in); Time time = new Time(); System.out.print("시간 입력 : "); int hour = sc.nextInt(); // 입력한 시간을 초로 바꾸어주는 메소드 호출 time.hourToSecond(hour);
→
시간 입력 : 24
24시간은 86400초 입니다.
접근 제한자로는 private
, protected
, public
, default
등이 있다.
private
: 같은 클래스 내부에서만 접근 가능
protected
: 같은 패키지와, 상속 관계의 클래스에서만 접근 가능
public
: 외부 클래스 어디에서나 접근 가능
default
: 아무것도 쓰지 않은 경우 default
로 취급하며, 같은 패키지 내부에서만 접근 가능.
Access_Modifier
private int private01; protected int protected01; int default01; public int public01;
AccessMain
Access_Modifier acc = new Access_Modifier(); // 외부 클래스 아무곳에서나 접근 가능하므로 접근 가능 acc.public01 = 10; // 접근 가능 // 같은 클래스 내부에서만 접근 가능하므로 접근 불가 // acc.private01 = 10; 접근 불가! // 같은 패키지 내이므로 접근 가능 acc.protected01 = 10; // 접근 가능 // 같은 패키지 내이므로 접근 가능 acc.default01 = 10; // 접근 가능
getter, setter 예시
private int var; public void setVar(int var) { this.var = var; } public int getVar() { return this.var; }
String
으로 된 id
, password
, name
, email
등의 필드 값의 접근 제한자를 private
로 정한다..getter
, setter
가 존재한다.Member
private String id; private String password; private String name; private String email; public Member(String id, String password, String name, String email) { this.id = id; this.password = password; this.name = name; this.email = email; } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; }
MemberMain
Member[] memberArr = new Member[3]; Member mem1 = new Member("mem1", "123", "회원1", "회원1이메일"); Member mem2 = new Member("mem2", "456", "회원2", "회원2이메일"); Member mem3 = new Member("mem3", "789", "회원3", "회원3이메일"); memberArr[0] = mem1; memberArr[1] = mem2; memberArr[2] = mem3; for (int i = 0; i < memberArr.length; i++) { System.out.println("이름 : " + memberArr[i].getName()); System.out.println("이메일 : " + memberArr[i].getEmail()); System.out.println(); }
→
이름 : 회원1
이메일 : 회원1이메일이름 : 회원2
이메일 : 회원2이메일이름 : 회원3
이메일 : 회원3이메일
String
자료형인 color
, company
를 가지고, int
형으로 된 size
, price
를 접근 제한자 private
로 갖는다.getter
, setter
가 존재한다.Phone
private String color; // 색상 private String company; // 제조사 private int size; // 크기 private int price; // 가격 public Phone() { } public Phone (String color, String company, int size, int price) { this.color = color; this.company = company; this.size = size; this.price = price; } public void setColor(String color) { this.color = color; } public String getColor() { return this.color; } public void setCompany(String company) { this.company = company; } public String getCompany() { return this.company; } public void setSize(int size) { this.size = size; } public int getSize() { return this.size; } public void setPrice(int price) { this.price = price; } public int getPrice() { return this.price; }
PhoneMain
Phone[] phList = new Phone[2]; Phone ph1 = new Phone("Silver", "Apple", 61, 999); Phone ph2 = new Phone(); ph2.setCompany("Samsung"); ph2.setSize(68); ph2.setColor("Violet"); ph2.setPrice(999); phList[0] = ph1; phList[1] = ph2; for (int i = 0; i < phList.length; i++) { System.out.println("제조사 : " + phList[i].getCompany()); System.out.println("크기 : " + phList[i].getSize()); System.out.println("색상 : " + phList[i].getColor()); System.out.println("가격 : " + phList[i].getPrice()); System.out.println(); }
→
제조사 : Apple
크기 : 61
색상 : Silver
가격 : 999
제조사 : Samsung
크기 : 68
색상 : Violet
가격 : 999