Interface Usage:
1. Use the interface with Polymorphism: It is divided into the status at compile time and runtime.
2. Pay attention to the consistency of objects.
3. Forced conversion.
- Package com;
- Public interface animal {
- }
- Package com;
- /***
- *
- * Birds
- *
- * @ Author Administrator
- *
- */
- Public class bird implements animal {
- Public bird (){
- }
- Public String color;
- Private int age;
- Public int getage (){
- Return age;
- }
- Public void setage (INT age ){
- This. Age = age;
- }
- Public String getcolor (){
- Return color;
- }
- Public void setcolor (string color ){
- This. Color = color;
- }
- }
- Package com;
- Public class samllbird extends bird {
- }
- Package com;
- Import java. Lang. Reflect. invocationtargetexception;
- Public class test {
- Public static void main (string [] ARGs) throws classnotfoundexception, securityexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception, nosuchmethodexception, invocationtargetexception {
- Animal B = new bird ();
- B. tostring ();
- Bird = (BIRD) B;
- Bird. setcolor ("red ");
- System. Out. println (bird. getcolor ());
- System. Out. println (B = bird );
- Animal sb = new samllbird ();
- Bird BB = (BIRD) Sb;
- System. Out. println (SB = bb );
- System. Out. println (B instanceof animal );
- System. Out. println (bird instanceof animal );
- System. Out. println (SB instanceof animal );
- }
- }
Running result:
Red
True
True
True
True
True |