问题:

如何编写接口?

public interface 接口名{

}

接口有哪些特性?

在面向接口的开发中,有利于代码的可扩展性和可维护性。

什么是接口?

是种规范和标准:一种能力,一种约定!

面向接口编程意味着什么?

开发系统时,主体构架使用接口吗,接口构成系统的骨架,这样就可以通过更换实现接口的类来实现更换系统。

类如何实现接口?

public 类 implements 接口{}

示例:

public interface Animal{   //定义接口

void shout();

}

class Cat implements Animal{   //实现接口

            public void shout(){

               system.out.print("Shout");

                  }  

}

 

class Dog implements Animal{    //实现接口

 

            public void shout(){

 

               system.out.print("Dog");

 

                  }  

}

class Store{

public static Animal get(String choice){

        if(choice.equalsIgnoreCase("dog")){

          return new Dog();

     }else{

          return new Cat();

             }

       }

}

public static void main(String[] args) {

    Animal al = Store.get("dog");

     al.shout();

}

posted on 2016-06-19 14:45  张张张宝  阅读(131)  评论(0)    收藏  举报