속성(멤버 변수)
기능(메서드)
예) 자전거 클래스
public class Grandeur { //클래스 이름 : 일반적으로 첫글자는 대문자로 한다
// 이하 멤버 변수 (속성)
public String color;
public String gear;
public int price;
// 이하 생성자
// 클래스의 이름과 동일한 메서드
// 반환형 없음
public Grandeur() {
System.out.println("grandeur constructor"); // 생성할 때 필요한 기능 입력, 없으면 안적어도 됨
}
// 이하 메서드
public void run() {
System.out.println("--run--"); //메서드의 기능
}
// public -> 접근제한자
// void -> 반환형, void의 경우 반환값이 없다. 만약 반환값이 String이면
// public String 메서드명 .. return 리턴값
public void stop() {
System.out.println("--stop--");
}
}
new
라는 키워드를 이용해 객체를 생성// MainClass
// 모든 실행은 MainClass에서 시작한다.
public class MainClass {
// 객체 생성, 클래스의 생성자 실행
// 클래스명, 객체명 = new 클래스생성자
public static void main(String[] args) {
Grandeur myCar1 = new Grandeur();
// 만든 객체의 멤버 변수 설정
myCar1.color = "red";
myCar1.gear = "auto";
myCar1.price = 30000000;
// 만든 객체로 메서드 사용 가능
myCar1.run();
myCar1.stop();
myCar1.info();
// 같은 클래스로 다른 객체 생성
Grandeur myCar2 = new Grandeur();
myCar2.color = "blue";
myCar2.gear = "manual";
myCar2.price = 25000000;
myCar2.run();
myCar2.stop();
myCar2.info();
}
}
public class Bicycle {
public String color;
public int price;
public Bicycle() { // 이렇게 괄호 안에 아무것도 안넣을 수도 있고
System.out.println("Bicycle constructor 1");
}
public Bicycle(String c, int p) { // 이렇게 생성자를 만들 때 받을 인자를 넣을 수도 있음
System.out.println("Bicycle constructor 2");
//this -> 내가 갖고 있는 객체를 의미
this.color = c; // 이렇게 생성할때 받아서 멤버변수에 할당 가능
// color = c; 이렇게 this 없이도 가능하다
price = p; // 이렇게 생성할때 받아서 멤버변수에 할당 가능
}
public void info() {
System.out.println("--info--");
System.out.println("color : " + color);
System.out.println("price : " + price);
}
}
public class MainClass {
public static void main(String[] args) {
// 객체 생성자에 이렇게 인자를 넣어서 생성하면 됨
Bicycle myBicycle = new Bicycle("red", 100);
myBicycle.info();
}
}
// 실행결과
// Bicycle constructor 2
// --info--
// color : red
// price : 100
public class MainClass {
public static void main(String[] args) {
// 객체 생성자에 이렇게 인자를 넣어서 생성하면 됨
Bicycle myBicycle = new Bicycle("red", 100);
myBicycle.color = "yellow";
myBicycle.info();
}
}
// 실행결과
// Bicycle constructor 2
// --info--
// color : yellow
// price : 100