黑马程序员——JAVA接口和内部类

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


接口定义
  • 格式

interface()

  • 接口的成员修饰符固定

    • 成员常量:public static final

    • 成员方法:public abstract


接口用“多实现”实现“多继承”。接口不可以创建对象,需要被子类实现,子类对接口中的抽象方法全部覆盖后,子类才可以实例化。方法修饰符必须为public


public interface Interface1 {

    int a=1;

    void show();



}

//上面的等于下面的,编译自动加上修饰符

public interface Interface1 {

    public static final int a=1;

    abstract void show();



}



public class Test implements Interface1 {

    public static void main(String []args){

        Test t=new Test();

        System.out.println(t.NUM);

        System.out.println(Test.NUM);

        System.out.println(Interface1.NUM);



    }



    @Override

    public void show() {

        System.out.println("TEST");

    }

}





接口之间的继承

interface A{

    abstract void showA();



}

//接口与接口之间支持多继承

interface B extends A,C{

    abstract void showB();



}



接口特点
  1. 接口是对外暴露的规则。

  2. 接口是程序的功能扩展。

  3. 接口可以用来多实现。

  4. 类与接口之间是实现关系。

  5. 接口与接口之间可以有继承关系。


Object类

所有对象的父类。

equals()

对象的比较。


equals(Object obj)
toString()

类名@hashcode()

Class类描述class文件


        Test t = new Test();

        Class c =t.getClass();//对象对应class文件

        System.out.println(c.getName());

        getClass.getName()+"@"+Integer.toHexString(hashCode())


内部类
内部类访问规则

作用:内部类可以方便访问外部类成员,包括私有。外部类访问内部类,需要建立内部类对象。之所以可以直接访问外部类的成员,因为直接持有外部类引用,格式:Outer.this

访问格式:

  • 当内部类定义在外部类成员位置上,而非私有,可以在外部其他类忠,直接建立内部类对象

格式:

外部类名.内部类名 变量名= 外部类对象.内部类对象;

Outer.Inner i = new Outer().new Inner();

  • 当内部类定义在外部类成员位置上,就可以被成员修饰符修饰。比如:private ,内部类在外部类中封装。

static ,内部类具有static特性。当内部类被static修饰后,只能访问外部类的静态成员。出现访问局限。

new Outer.Inner().function()(直接访问内部类的非静态成员)

Outer.Inner.function()(直接访问内部类的静态成员)

注意:当内部类定义了静态成员,内部类必须时静态的。当外部类的静态方法访问内部类时,内部类必须是静态的。


/**

 * Created by root on 15-6-14.

 */

public class Outer {

    int x;

    class Inner{

    int x;

        void function(){

        this.x;

        Outer.this.x;//外部类的变量

        }



    }

    void method(){

        Inner inner=new Inner();

        inner.function();

    }



    public static void main(String[]agrgs){

        //内部类全名

        Outer.Inner i = new Outer().new Inner();



    }

}


内部类定义规则

什么时候使用内部类?

当描述事物时,事物中含有事物,该事物使用内部类描述。因为该事物使用外部事物。


public class Outer {



    private class Inner{//封装内部类

        void function(){}

    }

    void method(){

        new Inner().function();



    }
匿名内部类

内部类定义在局部时,不能被成员修饰符访问。可以访问外部类的成员,因为持有外部类的引用。但是不可访问它所在局部的变量。只能访问被final修饰的成员变量。


public class Outer {





    void method(final int x){

         class Inner{

            void function(){

                System.out.println(x);

            }

        }

        new Inner().function();



    }



    public static void main(String[]agrgs){

        //

        Outer i = new Outer();

        i.method(1);

        i.method(2);



    }

}


定义匿名内部类前提,内部类必须继承一个类,或者实现接口。

格式:new 父类或接口 {定义子类的内容}

其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖。可以理解为带内容的对象。

匿名内部类中定义方法最好不要超过3个。


public class Outer {

    int x;



    void method(){

//

        new AbsDemo(){



            @Override

            void show(int x) {

                System.out.print("x="+x);

            }

        }.show(x);

    }



    public static void main(String []args){

        Outer o= new Outer();

        o.x=1;

        o.method();

    }



}



abstract class AbsDemo{

    abstract void  show(int x);

}



//练习

public class Outer {

    public static Inner function(){

        return new Inner() {

            @Override

            public void method() {

                System.out.print("返回匿名内部类");

            }

        };

    }

    public static void main(String[]args){

    //function()运算后是一个对象,而且是一个Inner类对象。所以才调用method()方法

        Outer.function().method();





    }

}



interface Inner{

    void method();

}

匿名内部类作为方法的参数


    public static void show(Inner inner){

    //

    }

        Outer.show(new Inner() {



            @Override

            public void method() {



            }

        });


匿名内部类父类为Object


new Object(){

            public void method(){}

        }.method();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值