클래스의 구성 요소는 아래 세 가지로 이루어져 있다.
이번 글에서는 생성자와 메소드에 대해 깊이 알아보자.
class Animal {
String name;
void setName(String name) {
this.name = name;
}
}
class Dog extends Animal {
void sleep() {
System.out.println(this.name + " zzz");
}
}
class HouseDog extends Dog {
HouseDog(String name) {
this.setName(name);
} // 생성자 생성
void sleep() {
System.out.println(this.name + " zzz in house");
}
void sleep(int hour) {
System.out.println(this.name + " zzz in house for " + hour + " hours");
}
}
public class Sample {
public static void main(String[] args) {
HouseDog dog = new HouseDog("happy");
// 만약 HouseDog dog = new HouseDog();라고 쓰게 되면 컴파일 오류가 발생.
// 생성자를 사용하면 setName("happy")와 같은 필수적인 행동을 객체 생성시 제어할 수 있다.
System.out.println(dog.name);
}
}
class Animal {
String name;
void setName(String name) {
this.name = name;
}
}
class Dog extends Animal {
void sleep() {
System.out.println(this.name + " zzz");
}
}
class HouseDog extends Dog {
HouseDog(String name) {
this.setName(name);
}
HouseDog(int type) {
if (type == 1) {
this.setName("yorkshire");
} else if (type == 2) {
this.setName("bulldog");
}
}
void sleep() {
System.out.println(this.name + " zzz in house");
}
void sleep(int hour) {
System.out.println(this.name + " zzz in house for " + hour + " hours");
}
}
public class Sample {
public static void main(String[] args) {
HouseDog happy = new HouseDog("happy");
HouseDog yorkshire = new HouseDog(1);
System.out.println(happy.name); // happy 출력
System.out.println(yorkshire.name); // yorkshire 출력
}
}
생성자의 예시
기본 생성자(디폴트 생성자) : 정의할 게 없는 생성자
class Calculator {
**//생성자 정의 - 기본 생성자(디폴트 생성자)
public Calculator(){
}**
public static void main(String[] args){
System.out.println("Calculator start");
Calculator calc = new Calculator();
int a = 10;
int b = 15;
System.out.println(a + " + " + b + " = " + calc.add(a, b));
System.out.println(a + " - " + b + " = " + calc.subtract(a, b));
System.out.println(a + " * " + b + " = " + calc.multiply(a, b));
System.out.println(a + " / " + b + " = " + calc.divide(a, b));
}
// 메소드 정의
public int add(int x, int y){
return x + y;
}
public int subtract(int x, int y){
return x - y;
}
public int multiply(int x, int y){
return x * y;
}
public int divide(int x, int y){
return x / y;
}
}
매개변수가 있는 생성자 = 명시적 생성자 / 매개변수가 없는 생성자 = 묵시적 생성자
class Calculator2 {
int int1, int2;
// 매개변수를 받는 생성자 생성
public Calculator2(int num1, int num2){
int1 = num1;
int2 = num2;
}
public static void main(String[] args){
System.out.println("Calculator start");
int a = 10;
int b = 15;
// 객체정의
Calculator2 calc = new Calculator2(a, b);
System.out.println(a + " + " + b + " = " + calc.add());
System.out.println(a + " - " + b + " = " + calc.subtract());
System.out.println(a + " * " + b + " = " + calc.multiply());
System.out.println(a + " / " + b + " = " + calc.divide());
}
public int add(){
return int1 + int2;
}
public int subtract(){
return int1 - int2;
}
public int multiply(){
return int1 * int2;
}
public int divide(){
return int1 / int2;
}
접근제어자 반환타입 메소드이름(매개변수목록) { // 선언부
// 구현부
}
//메소드 예시
class Car {
//변수
private int currentSpeed;
private int accelerationTime;
...
//메소드
public void accelerate(int speed, int second) { // 선언부
// 구현부
System.out.println(second + "초간 속도를 시속 " + speed + "(으)로 가속함!!");
}
...
}
