Java-第10章に連れて行きます.インタフェース(下).抽象クラスとインタフェースの違い、デフォルトアダプタ

19455 ワード

インタフェース(下)
    ( )
     :
1.    (       )
2.    
3.                   static  
4.    (     )    default  
(                  )
                    default  
                

//        
  .super.     
//   :   

interface Inter{
    //     
    public abstract void abstractfun();
    //     
    public static void staticfun(){
        System.out.println("           ");
    }
    //     
    public default void defaultfun(){
        System.out.println("           ");
    }
}

//     
class Interimpl implements Inter{
    //       
    @Override
    public void abstractfun(){
        System.out.println("               ");
    }
    //         
    @Override
    public  void  defaultfun() {
        System.out.println("              ");
    }
    //     
    public static void staticfun() {
        System.out.println("            ");
    }
    //            
    public void test() {
        //           
        Inter.staticfun();
        //           
        Inter.super.defaultfun();
    }
}

抽象クラスとインタフェースの違い
1.    :
     :     extends
     :      implements
2.    
      :        
      :       (  public static final   )
3.    
      :            
      :       (           )
4.    
      :     
      :      

        
      (    ,  , , )

         
      (         [  ] )

          
         
//           
interface InterA{
    public abstract void fun1();
}

interface InterB{
    public abstract void fun2();
}

class ClassA implements InterA,InterB{

    @Override
    public void fun2() {
        // TODO Auto-generated method stub

    }

    @Override
    public void fun1() {
        // TODO Auto-generated method stub

    }

}
//             
interface InterC{
    public abstract void fun1();
}
interface InterD extends InterC{

}
class ClassB implements InterD{

    @Override
    public void fun1() {
        // TODO Auto-generated method stub

    }

}// 

デフォルトアダプタモード(選択的実装インタフェースでの抽象的な方法)

/* 
 *        
 *  1.                          
 *           
 *  2.               
 *  3.          
 * 
 *    (  )     
 *    
 *   
 *   
 * 
 *        
 *   
 */

//   
interface JSF{
    public abstract void woTui();
    public abstract void running();
    public abstract void swimming();
}
//     Adapter,    
//         ,           
//         
abstract class Adapter implements JSF{
    public void woTui() {};
    public void running() {};
    public void swimming() {};
}
//    
class WangJun extends Adapter{
    @Override
    public void running() {
        // TODO Auto-generated method stub
        System.out.println("  10  ");
    }
}
    //      
    /* 
     *              
    *                        (     )
    *                 
    * 
    *              
    *     (    )
    *     (        )
    *         
    *           
    */

//     
interface UserCard{
    //       
    public abstract void userCard(Card card);
}

//      
class MainBoardImpl implements UserCard{

    @Override
    public void userCard(Card card) {
        // TODO Auto-generated method stub
        card.start();
        card.stop();

    }   

}


//         
abstract class Card{

    public abstract void start();
    public abstract void stop();
}


//        
class SongCard extends Card{

    @Override
    public void start() {
        // TODO Auto-generated method stub
        System.out.println("    ");
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
        System.out.println("    ");
    }

}
//      (    ,                      )
/*    :
 *  1.             
 *  2.                   (                  )
 *  3.                   (                  )
 *  4.                     
 *  5.                  
 *  (      /set/get  )
 *  6.      
 */

// 1.             
//   
abstract class Person{
    private String name;
    private String gender;
    //       
    public Person() {
        super();
    }
    //       
    public Person(String name, String gender) {
        super();
        this.name = name;
        this.gender = gender;
    }

    // set/get  
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    //       
    public abstract void eat();
}

// 2.                   
// (                  )

//   
public class Man extends Person implements Cost{
    //         ,           
    // (   )
    private Women gF ;

    //        
    public Man() {

    }
    //       
    public Man(String name,String gender,Women gF) {
        //             
        super(name,gender);
        //              
        this.gF = gF;
    }

    // set/get  
    public Women getgF() {
        return gF;
    }
    public void setgF(Women gF) {
        this.gF = gF;
        // return  this,              
    }
    //        
    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("   ");
    }
    //     
    public void sayHi() {
        //               .get        
        System.out.println("      :" + this.getgF().getName());
        System.out.println("  :" + this.getgF().getGender());
        System.out.println("  " + this.getName());
    }
    //         
    @Override
    public void makeMoney() {
        // TODO Auto-generated method stub
        System.out.println("       ");
    }
}

// 3.                   
// (                  )
//   
class Women extends Person{
    //         ,         
    //    
    private Man bF;
    //       
    public Women() {
        super();
        // TODO Auto-generated constructor stub
    }
    //       
    public Women(String name, String gender,Man bF) {
        super(name, gender);
        this.bF = bF;
    }
    // set/get  
    public Man getbF() {
        return bF;
    }

    public void setbF(Man bF) {
        this.bF = bF;
    }

    //        
    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("  ");
    }
    //     
    public void changeFace(String body) {
        System.out.println("     :" + body);
    }
    //     
    public void sayHi() {
        System.out.println("      " + this.getbF().getName());
        System.out.println("  :" + this.getbF().getGender());
        System.out.println("  " + this.getName());
    }
}

// 4.     
public interface Cost {
    public void makeMoney();
}
// 5.    
public class Test {
    public static void main(String[] args) {
        //         
        Man man = new Man();
        Women women = new Women();
        //     
        man.setName("  ");
        man.setGender(" ");
        man.setgF(women);
        //     
        women.setName("  ");
        women.setGender(" ");
        women.setbF(man);
        man.sayHi();
        women.sayHi();
        //                   
        //    get             
        //                     

        //     women
        man.getgF().changeFace("    ");
        //     man
        women.getbF().makeMoney();
    }
}