//type casting(강제/자동)은 inheritance관계(부모<->자식)에서만 possible하다. //형제끼린 X
//< Human>
class Human {
String state = "인간";
public void state() {
System.out.println( "인간이다" );
}
}
//< Woman>
class Woman extends Human {
String name = "gemini";
// namevariable의 값을 리턴
public String getName() {
return name;
}
// 상위 클래스의 method overriding
public void state() {
System.out.println( name + "은 여자이다" );
}
}
//< Man>
class Man extends Human {
String name = "johnharu";
// namevariable의 값을 리턴
public String getName() {
return name;
}
// 상위 클래스의 method overriding
public void state() {
System.out.println( name + "는 남자다" );
}
}
//< ClassCast>
public class ClassCast {
public static void main( String[] args ) {
/*
//:error:JVM이 자동으로 디폴트 error 메시징
// Womanobject creation
Woman gemini = new Woman();
// Polymorphism
Human wo = new Woman();
// name을 리턴시키는 method를 call
System.out.println( gemini.getName() );
String wName = ((Woman)wo).getName();
System.out.println( wName );
// 부모 클래스를 자식 클래스로 casting
//Man john = (Man)gemini;
Man john = (Man)wo;
// 이름을 output
String mName = john.getName();
System.out.println( mName );
**/
// Womanobject creation
Woman gemini = new Woman();
// Polymorphism
Human wo = new Woman();
// name을 리턴시키는 method를 call
System.out.println( gemini.getName() );
String wName = ((Woman)wo).getName();
System.out.println( wName );
// 부모 클래스를 자식 클래스로 casting
//Man john = (Man)gemini;
try {
Man john = (Man)wo;
// 이름을 output
String mName = john.getName();
System.out.println( mName );
} catch ( ClassCastException cc ) {
System.out.println( "Message1 : " + cc.getMessage() );
cc.printStackTrace();
System.out.println( "Message2 : " + cc.toString() );
} finally {
System.out.println( "남자는 여자가 될 수 없다" );
}
}
}
- Open in Google Docs Viewer
- Open link in new tab
- Open link in new window
- Open link in new incognito window
- Download file
- Copy link address
- Edit PDF File on PDFescape.com
element
| Font | |
|---|---|
| font-family | |
| font-size | |
| font-style | |
| font-variant | |
| font-weight | |
| letter-spacing | |
| line-height | |
| text-decoration | |
| text-align | |
| text-indent | |
| text-transform | |
| white-space | |
| word-spacing | |
| color | |
| Background | |
| bg-attachment | |
| bg-color | |
| bg-image | |
| bg-position | |
| bg-repeat | |
| Box | |
| width | |
| height | |
| border-top | |
| border-right | |
| border-bottom | |
| border-left | |
| margin | |
| padding | |
| max-height | |
| min-height | |
| max-width | |
| min-width | |
| outline-color | |
| outline-style | |
| outline-width | |
| Positioning | |
| position | |
| top | |
| bottom | |
| right | |
| left | |
| float | |
| display | |
| clear | |
| z-index | |
| List | |
| list-style-image | |
| list-style-type | |
| list-style-position | |
| Table | |
| vertical-align | |
| border-collapse | |
| border-spacing | |
| caption-side | |
| empty-cells | |
| table-layout | |
| Effects | |
| text-shadow | |
| -webkit-box-shadow | |
| border-radius | |
| Other | |
| overflow | |
| cursor | |
| visibility |
