Car car = new Car();
System.out.println(car.color);
System.out.println(car.tire);
Car(){
color = "하얀색";
tire = 4;
}
Car2 car2 = new Car2("노란색",3);
System.out.println(car2.color);
System.out.println(car2.tire);
Car2(String c, int t){
color = c;
tire = t;
}
Car3 car3_1 = new Car3();
System.out.println(car3_1.color);
System.out.println(car3_1.tire);
Car3 car3_2 = new Car3("검정색");
System.out.println(car3_2.color);
System.out.println(car3_2.tire);
Car3 car3_3 = new Car3("하얀색",8);
System.out.println(car3_3.color);
System.out.println(car3_3.tire);
public class Car3 {
String color;
int tire;
Car3(){
color = "파란색"; // color, tire변수를 임의 값으로 초기화
tire = 4;
}
// 매개변수 1개(color)인 생성자 생성
// 나머지(tire)는 임의 값으로 초기화
Car3(String c) {
color = c;
tire = 4;
}
// 매개변수 2개인 생성자 생성
// color, tire를 매개변수를 이용해 초기화
Car3(String c, int t) {
color = c;
tire = t;
}
public class Car {
int tire = 4;
int door = 2;
public Car() {
System.out.println("Car 객체 생성");
}
public void move() {
System.out.println("이동");
}
// getter, setter 메서드 자동생성
// alt + shift + s -> generate getter and setter 메뉴
// select all 클릭해서 전체변수 선택 후에 generate 버튼 클릭 하면 자동 생성.
public int getTire() {
return tire;
}
public void setTire(int tire) {
this.tire = tire;
}
public int getDoor() {
return door;
}
public void setDoor(int door) {
this.door = door;
}
}
// Car클래스를 상속받는 SportsCar클래스 생성
// 클래스명 뒤에 extends 클래스이름
public class SportsCar extends Car{
int coupeDoor = 2;
String brand = "porsche";
SportsCar(){
System.out.println("SportsCar 객체 생성");
}
void booster() {
System.out.println("부스터 사용");
}
public void openSunloof() {
System.out.println("선루프 오픈");
}
}
public class CarMain {
public static void main(String[] args) {
// Car클래스를 상속받는 SportsCar클래스를 객체로 생성
SportsCar sCar = new SportsCar();
sCar.move();
sCar.setTire(4);
System.out.println(sCar.getTire());
sCar.booster();
// 업캐스팅 & 다운캐스팅
SportsCar sc = new SportsCar(); // 자식객체를 먼저 생성
Car c = null;
c = (Car) sc; // 업캐스팅을 사용해서 Car타입의 c객체를 생성 : ()로 어느 타입인지 지정
c.move();
SportsCar sc2 = (SportsCar) c; // 다운캐스팅
sc2.openSunloof();
sc2.booster();
System.out.println("==========");
Car c2 = new Car(); // 처음부터 Car클래스를 객체로 생성
SportsCar sc3 = null; // 자식 클래스 객체 생성이 아님
sc3 = (SportsCar) c2; // Car 객체로 생성되었기 때문에 오류 - 자식클래스를 객체로 만든 후 업캐스팅 선행 필요
if (c2 instanceof SportsCar) {
sc3 = (SportsCar) c2;
sc3.openSunloof();
} else {
System.out.println("캐스팅 불가!");
}
}
}
public class Ramen {
String taste;
String name;
public String getTaste() {
return "라면맛";
}
public class SpicyRamen extends Ramen {
public SpicyRamen(String name) {
super.name = name;
}
@Override
public String getTaste() {
return super.name + " => 매운 라면맛";
}
}
public class SaltyRamen extends Ramen {
public SaltyRamen(String name) {
super.name = name;
}
@Override
public String getTaste() {
return super.name + " => 짠 라면맛";
}
}
public class RamenMain {
public static void main(String[] args) {
Ramen ramen1 = new SpicyRamen("불 라면");
String taste1 = ramen1.getTaste();
System.out.println(taste1);
Ramen ramen2 = new SaltyRamen("소금 라면");
String taste2 = ramen2.getTaste();
System.out.println(taste2);
}
}
-Ramen을 SpicyRamen과 SaltyRamen에 오버로딩. 출력은 RamenMain
public class Ex01_ArticleVO {
// 게시글의 정보를 취급하는 클래스
private int articleId;
private String title;
private String content;
public int getArticleId() {
return articleId;
}
public void setArticleId(int articleId) {
this.articleId = articleId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
public class Ex01_AccesMain {
public static void main(String[] args) {
//게시글 객체 생성
Ex01_ArticleVO article = new Ex01_ArticleVO();
article.setArticleId(1);
article.setTitle("게시글 1번");
article.setContent("게시글 테스트 입니다.");
System.out.println(article.getArticleId());
System.out.println(article.toString());
}
}
// 인터페이스 생성 : class 키워드 대신 interface 키워드를 사용
public interface Sound {
String sound(); // 메소드를 선언할때 중괄호{}는 생략한다.
}
ublic class Piano implements Sound{
@Override
public String sound() {
return "도레미";
}
}
public class Guitar implements Sound {
@Override
public String sound() {
return "팅";
}
}
public class SoundMain {
public static void main(String[] args) {
Sound piano = new Piano();
System.out.println(piano.sound());
Sound guitar = new Guitar();
System.out.println(guitar.sound());
}
}