/*객체지향프로그래밍?
* 객체 - 눈에보이는 모든것, 프로그래밍에서 속성과 기능을 가지는 프로그램의 단위
* 객체(인간세계) ==>사람(속성: 키,몸무게 / 기능 : 의사) TV(속성 : 채널, 사운드/기능 : 미디어방출) 승용차(속성 : 바퀴, 엔진/기능 : 이동수단) 등
* 객체(프로그램)==>날씨 프로그램(속성 : 온도,미세먼지/기능:날씨예보), 사칙연산프로그램(속성 : -*+/ / 기능:연산기능) 등
*
* 클래스 - 객체를 생성하기위한 툴로 모든 객체는 클래스로부터 생성
* 날씨 프로그램(속성 : 온도,미세먼지/기능:날씨예보) -복제 - (속성 : 온도(썹시),미세먼지/기능:날씨예보)
* - (속성 : 온도(화시),지진정보/기능:날씨예보)
* 위의 식으로 기본적인 틀(클래스)를 가지고 다른 내용의 객체를 생성
*
* 클래스의 구성요소
* 클래스는 속성(맴버변수)와 기능(메서드)로 구성된다.
* 자전거를 만들다고할때
*
* 속성(맴버변수)
* 안장,핸들,바구니,기어,패달,바퀴
*
*
* 기능(메서드)
* 기어 변속,가속,브레이크
*/
//Grandeur.java
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 stop() { //매서드(기능)
System.out.println("--stop--");
}
public void info() { //매서드(기능)
System.out.println("--info--"); //속성 출력
System.out.println("color : "+ color);
System.out.println("gear : " + gear);
System.out.println("price : " + price);
}
}
//Bicycle.java
public class Bicycle {
public String color;
int price;
public Bicycle() {
System.out.println("Bicycle constructor - I");
}
public Bicycle(String c, int p) {
System.out.println("Bicycle constructor - II");
color = c;
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) {
Grandeur myCar1 = new Grandeur(); //Grandeur 객체 를 생성한다.
myCar1.color = "red"; //속성(멤버 변수)를 지정해준다.
myCar1.gear = "auto";
myCar1.price = 500;
myCar1.run();
myCar1.stop();
myCar1.info();
System.out.println();
Grandeur myCar2 = new Grandeur();
myCar2.color = "blue";
myCar2.gear = "manual";
myCar2.price = 2555500;
myCar2.run();
myCar2.stop();
myCar2.info();
System.out.println();
Bicycle myBicy1 = new Bicycle();
myBicy1.color = "red";
myBicy1.price = 500;
myBicy1.info();
System.out.println();
Bicycle myBicy2 = new Bicycle("blue", 100000);
myBicy2.info();
}
}
--결과창--
Grandeur constructor
--run--
--stop--
--info--
color : red
gear : auto
price : 500
Grandeur constructor
--run--
--stop--
--info--
color : blue
gear : manual
price : 2555500
Bicycle constructor - I
-- info() --
color : red
price : 500
Bicycle constructor - II
-- info() --
color : blue
price : 100000
'Develop > JAVA' 카테고리의 다른 글
JAVA 객체와 메모리(레퍼넌스) (0) | 2023.10.21 |
---|---|
JAVA 메서드(선언, 호출, 중복) (0) | 2023.10.21 |
JAVA Collections(List,Map) (0) | 2023.10.17 |
JAVA 문자열클래스(String,StringBuffer, StringBuilder) (0) | 2023.10.17 |
JAVA 람다식 (0) | 2023.10.17 |