码迷,mamicode.com
首页 > 编程语言 > 详细

java 第39节 接口的应用

时间:2016-06-29 20:17:38      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

2016-06-29

1 方法的修饰符
接口中变量的修饰符:public static final
接口中方法的修饰符:public abstract

package com.java1995;
/**
 * 1.定义接口:打印
 * @author Administrator
 *
 */
public interface Modifier {
    /**
     * 均等价于public abstract void 方法名
     */
    void print();
    public void print1();
    abstract void print2();
    public abstract void print3();
    abstract public String print4();
    
}

 

package com.java1995;
/**
 * 2.实现接口:打印
 * @author Administrator
 *
 */
public class Modifier2 implements Modifier{

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

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

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

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

    @Override
    public String print4() {
        // TODO Auto-generated method stub
        System.out.println("abstract public String");
        return null;
        
    }

}

 

package com.java1995;
/**
 * 3.调用接口:打印
 * @author Administrator
 *
 */
public class ModifierTest {
    
    public static void main(String[] args) {
        //多态
        Modifier m=new Modifier2();
        m.print();
        m.print1();
        m.print2();
        m.print3();
        m.print4();
    }

}

技术分享

2 接口类型引用变量
重点!

package com.java1995;
/**
 * 2.实现接口:加减乘除
 * @author Administrator
 *
 */
public class MTest implements Jia,Jian,Cheng,Chu {

    @Override
    public int chu(int a, int b) {
        // TODO Auto-generated method stub
        return a/b;
    }

    @Override
    public int cheng(int a, int b) {
        // TODO Auto-generated method stub
        return a*b;
    }

    @Override
    public int jian(int a, int b) {
        // TODO Auto-generated method stub
        return a-b;
    }

    @Override
    public int jia(int a, int b) {
        // TODO Auto-generated method stub
        return a+b;
    }

}

 

package com.java1995;
/**
 * 3.调用接口:加减乘除
 * @author Administrator
 *
 */
public class MathTest {
    
    public static void main(String[] args) {
        MTest mt=new MTest();
        System.out.println("使用引用对象调用方法");
        System.out.println("a+b="+mt.jia(15, 3));
        System.out.println("a-b="+mt.jian(15, 3));
        System.out.println("a*b="+mt.cheng(15, 3));
        System.out.println("a/b="+mt.chu(15, 3));
        
        /**
         * 接口类型引用变量,重点!
         */
        Jia mJia=mt;
        Jian mJian=mt;
        Cheng mCheng=mt;
        Chu mChu=mt;
        System.out.println("使用接口类型引用调用");
        System.out.println("a+b="+mJia.jia(15,3));
        System.out.println("a-b="+mJian.jian(15, 3));
        System.out.println("a*b="+mCheng.cheng(15,3));
        System.out.println("a/b="+mChu.chu(15, 3));
    }

}

/**
 * 1.定义接口:加减乘除
 */

//加法接口
interface Jia{
    int jia(int a,int b);
}
//减法接口
interface Jian{
    int jian(int a,int b);
}
//乘法接口
interface Cheng{
    int cheng(int a,int b);
}
//除法接口
interface Chu{
    int chu(int a,int b);
}

技术分享

 

【参考资料】

[1] Java轻松入门经典教程【完整版】

 

java 第39节 接口的应用

标签:

原文地址:http://www.cnblogs.com/cenliang/p/5627926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!