黑马程序员----接口

 

-------android培训java培训、期待与您交流! ----------

1.class 用于定义类
2.interface 用于定义接口
3.定义格式如下:
    常量:public  static  final  大写 
    方法:public abstract
4.接口中的成员都是 public
5.接口不可以创建对象,需要被子类实行
6.接口可以被类多实现


interface Inter
{  
    public static final int NUM = 5;
    public abstract void show();
}

class   InterfaceDemo
{
    pulbic static void main(String args[])
    {
       System.out.println();
       Test t = new Test();
       System.out.println(t.NUM);
       System.out.println(Test.NUM);

    }
}


class  Test  implements  inter,InerA
{
    public  void show()
    {
         System.out.println();
    }
    public abstract void max()
    {
         System.out.println();
    }
}


interface  InerA
{
     public abstract void max();
}

 

引用类型:      接口--------------类-------------------数组


<----------------多态-------------------->


abstract class  Animal
{
   public abstract void eat();
}
class Cat extends Animal()
{
   public  void eat()
   {
        System.out.println("吃鱼");
   }
   public void catcth()
   {
         System.out.println("抓老鼠");
   }
}
class  Dog extends Animal
{
   public  void eat()
   {
       System.out.println("骨头");
   }
}

public class  Demo
{
  public static void main()
  {
      Cat  c  = new Cat();
      Dog   d =  new Dog();
      c.eat();
      d.eat();
  }
 
  public static void  function(Cat c )
  {
       c.eat();
     
  } //作用封装方法<*********************************>
    public static void  function(Dog d  )
  {
       d.eat();
     
  } //作用封装方法
}

<-----------------多态 转型-------------------->

class DuoTaiDemo
{
 public static void main(String args[])
 {
     Animal  a = new Cat ();    //类型提升,向上转型    
     a.eat();

    // 想用子类的方法,将父类的引用转换成子类

     Animal  a = new Cat ();
            Cat c  = (Cat) a ;
     c. catcth();
 }
}

function(new Cat());
function(new Dog());
public static void function(Animal a)
{
    if(a instanceof Cat)
    {
       
    }
    if(a instanceof Dog)
    {
        
    }
}


面向对象多态实例


interface  PCI
{
    public void  open();
    public void  close();
}

class MainBoard
{
    public void run()
    {
        System.out.println(" run ");
    }
    public void userPCI(PCI P)    //PCI p  =  new NetCard()
    {
         if(p!=null)
  {
      p.open();
      p.close();
  }
    }

}

class NetCard implements PCI
{
    public void  open()
    {
       System.out.println(" open ");
    }
    public void  close ()
    {
         System.out.println(" close ");
    }
}

class Demo
{
     public static void main(String args[])
     {
         MainBoard  mb = new MainBoard();
  mb.run();
         mb.userPCI(null);
  mb.userPCI(new NetCard() );
        
     }
   
}

 

-------android培训java培训、期待与您交流! ----------

详细请查看:http://edu.youkuaiyun.com/heima/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值