Java는 기본적으로 C++과 같은 컴파일 -> exe파일생성 과 같은 언어가 아닌, 클래스 파일(C++의 Obj와 비슷)을 생성한다.
그래서 Java로 만든 프로그램은 윈도우, 맥, 리눅스 등 어디든 자바 가상 머신이 설치되어 있다면 실행시킬 수 있음!
Super는 부모의 클래스.
This는 자신 클래스의 참조값.
오버로딩 Overloading 오버라이딩 Overriding의 차이.
오버로드는 다형성, 같은 함수의 인자만 다른 함수들을 만들때 오버로딩한다고 함.
오버라이드는 부모의 함수를 덮어씌워, 나만의 클래스를 만들때 오버라이드(덮어쓰기)한다고 함.
자바의 클래스와 상속 기초 예제
Bicycle.java
public class Bicycle {
int id;
String brand;
public Bicycle(int id, String brand){
this.id = id;
this.brand = brand;
}
public Bicycle(){
this.id = 0;
this.brand = "초기값";
}
public void prtInfo(){
System.out.println("id = " + this.id);
System.out.println("brand = " + this.brand);
}
}
MountaionBike.java
public class MountaionBike extends Bicycle{
String frame;
boolean suspension;
public MountaionBike(String frame, boolean suspension){
super(123, "삼천리");
this.frame = frame;
this.suspension = suspension;
}
public void prtInfo(){
System.out.println("id = " + id);
System.out.println("brand = " + brand);
System.out.println("frame = " + frame);
System.out.println("suspension = " + suspension);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MountaionBike mtb = new MountaionBike("카본", true);
mtb.prtInfo();
System.out.println("");
Bicycle bicycle = new Bicycle(789, "트랙");
bicycle.prtInfo();
}
}