java 接口使用

来源:互联网 发布:手机淘宝店装修教程 编辑:程序博客网 时间:2024/05/16 23:36

java 基础的几个重要的概念

数据:一些不变量与变量

方法:函数,一种操作

类:包含成员数据,成员方法(成员函数),至少有一个默认的构造函数,用于初始化类

对象:实例化的类

接口:只对操作进行声明,但是不定义具体的操作是怎样的


在后面的spring mvc 中,接口对分层起了很大作用;是一种思想的扩展;


面向对象编程:变量和操作都放在类中操作,可以构造多个构造函数,可以对结构性很好的类继承,可以通过接口,将对象与操作分开

面向对象编程的三大特性:封装性,多态性,继承性,

容器可以管理对象的生命周期、对象与对象之间的依赖关系,您可以使用一个配置文件(通常是XML),在上面定义好对象的名称、如何产生(Prototype 方式或Singleton 方式)、哪个对象产生之后必须设定成为某个对象的属性等,在启动容器之后,所有的对象都可以直接取用,不用编写任何一行程序代码来产生对象,或是建立对象与对象之间的依赖关系。

来自百度百科

/* * 主要是为了理解接口的使用方法; 接口是一系列方法的集合,这些方法不提供方法的实体; * 接口中的所有方法都是public的?数据成员必须是public,static,final类型,必须初始化 * 接口中的所有方法都必须重写吗? */package myProPer;interface   Flyanimal{    String animal = "can fly in the sky!";   double speed = 1.00;   static int testnum = 12;   final double number = (1 + 1/2 + 1/3 + 1/4 +1/5 );      void fly();       public int wing();       //String feather(String color);   //返回类型可以是字符串吗?}interface Flybird extends Flyanimal {String bird = "has a beautiful swing!";void flying() ;}class   Insect {      int  legnum=6;  protected boolean feather = false;}class  Bird {     int  legnum=2;protected boolean feather = true;  void egg(){};}class Bee extends Insect implements  Flyanimal {     public String name = "Bee";     public String color = "silver";     public void fly() {       System.out.println(this.name+" can fly");         if (feather) {System.out.println(this.name+"has not feather!");    }     }public int wing() {// TODO Auto-generated method stubSystem.out.println(this.name + " has a beautiful"+ color +" swing!");return 0;}} class Pigeon  extends Bird implements  Flybird { public String name = "Pigeon";     public String color = "white";     public void fly() {       System.out.println("pigeon  can fly");       if (feather) {System.out.println(this.name+" has feather!");    }     }     public void egg(){       System.out.println("pigeon  can lay  eggs ");     }public int wing() {// TODO Auto-generated method stubSystem.out.println(this.name + " has a beautiful "+ color +" swing!");return 0;}@Overridepublic void flying() {// TODO Auto-generated method stubSystem.out.println("Bird is flying!");}} public class InterfaceDemo{  public static void main(String[] args) {      Bee b=new Bee();      b.fly();      b.wing();      System.out.println("Bee's legs are "+ b.legnum);      Pigeon p= new Pigeon();       p.fly();      p.egg();      p.wing();      p.flying();  }}


0 0
原创粉丝点击