20220314 º¹½À
< »ó¼Ó >
Servlet = HttpServletÀ» »ó¼Ó¹Þ¾Æ ¸¸µç Ŭ·¡½º
extends = ºÎ¸ðŬ·¡½º¸¦ °¡Á®´Ù ¾¸.
implements = Ãß»óŬ·¡½ºÀÎ ÀÎÅÍÆäÀ̽º (ºÒ¾ÈÁ¤ÇÑ Å¬·¡½º = ¸Þ¼µå ¸Ó¸®¸¸ ÀÖÀ½)¿¡ ¿¬°á.
super.º¯¼ö¸í = ºÎ¸ðÀÇ ¸â¹öº¯¼ö Á¢±Ù
super(); = ºÎ¸ðÀÇ »ý¼ºÀÚ È£Ãâ
¿À¹ö¶óÀ̵ù = ºÎ¸ðŬ·¡½ºÀÇ ¸Þ¼µå¸¦ ÀÚ½ÄŬ·¡½º¿¡¼ º¯ÇüÇÏ¿© »ç¿ë.
¿À¹ö·Îµù = µ¿ÀÏÇÑ À̸§ÀÇ ¸Þ¼µå(»ý¼ºÀÚ)ÀÌÁö¸¸, ¸Å°³º¯¼ö ŸÀÔ°ú °³¼ö µîÀÌ ´Ù¸¥ ¸Þ¼µå »ý¼º.
CarMain Ŭ·¡½º
public class CarMain {
public static void main(String[] args) {
Genesis gs = new Genesis("white", 200); // °´Ã¼ ¸¸µé ¶§ ¸¶´Ù ºÎ¸ðŬ·¡½ºCarÀÇ »ý¼ºÀÚ ½ÇÇà.
Car c= new Car();
System.out.println(gs.color+", "+gs.price);
System.out.println(c.color+", "+c.price);
gs.upSpeed(); //CarŬ·¡½ºÀÇ ¸Þ¼µåÀÌÁö¸¸, GenesisŬ·¡½º¿¡¼ OverridingÇÑ ¸Þ¼µå
}
}
-------------Ãâ·Â³»¿ë-----------------
Car »ý¼ºÀÚ name: Genesis
----------------------
Genesis »ý¼ºÀÚ
ºÎ¸ð»ö»ó: Black
ÀڽĻö»ó: white
----------------------
Car »ý¼ºÀÚ
white, 200
Black, 100
!!!Speed Up!!!
Car Ŭ·¡½º (ºÎ¸ðŬ·¡½º)
public class Car {
String color="Black"; // protected¸¦ °°Àº ÆÐÅ°Áö + ÀÚ½ÄŬ·¡½º±îÁö »ç¿ë°¡´É.
protected int price=100; // privateÀº Car Ŭ·¡½º¿¡¼ ¹Û¿¡ »ç¿ë ºÒ°¡´É.
int speed;
//»ý¼ºÀÚ ¿©·¯°³ ¸¸µé±â (¿À¹ö·Îµù) ½Ç¹«¿¡¼ Àß ¾È¾²ÀÌÁö¸¸, [»ó¼Ó]À» ÀÌÇØÇϱâ À§ÇØ °³³ä È®ÀÎ
//WebÀ¸·Î ³Ñ¾î°¡¸é ÁÖ·Î (¿À¹ö¶óÀ̵ù)À» ÀÌ¿ëÇÏ¿© ÀÎÅÍÆäÀ̽º È°¿ë
Car(){
System.out.println("Car »ý¼ºÀÚ");
}
//»ý¼ºÀÚ ¿©·¯°³ »ý¼º °¡´É. ¸Å°³º¯¼öÀÇ À¯¹«, °¹¼ö, ŸÀÔ¿¡ µû¶ó ¼±ÅÃµÇ¾î ½ÇÇàµÊ.
Car(String name){
System.out.println("Car »ý¼ºÀÚ name: "+name);
}
Car(int carNum){
System.out.println("Car »ý¼ºÀÚ speed: "+carNum);
}
void upSpeed() {
System.out.println("Speed Up!!!");
}
void downSpeed() {
System.out.println("Speed Down...");
}
}
Genesis Ŭ·¡½º (ÀÚ½ÄŬ·¡½º)
public class Genesis extends Car{
String color="";
int price;
Genesis(String color, int price){
super("Genesis"); // ºÎ¸ðŬ·¡½ºÀÇ »ý¼ºÀÚ¿¡ ¸Å°³º¯¼ö°¡ ÇÊ¿äÇÑ °æ¿ì °ª ¼³Á¤.
this.color=color;
this.price=price;
System.out.println("----------------------");
System.out.println("Genesis »ý¼ºÀÚ");
System.out.println("ºÎ¸ð»ö»ó: "+super.color);
System.out.println("ÀڽĻö»ó: "+color);
System.out.println("----------------------");
}
@Override
void upSpeed() {
System.out.println("!!!Speed Up!!!");
}
}