package com.test.interfacetest;
/**
* 利用接口和类实现函数指针的功能
* @author Administrator
*
*/
interface Compare{
public int cmp(int a , int b );
}
class Compare1 implements Compare {
@Override
public int cmp(int a, int b) {
return a;
}
}
class Compare2 implements Compare {
@Override
public int cmp(int a, int b) {
return b;
}
}
public class InterfaceTest {
public InterfaceTest(Compare com){
System.out.println(com.cmp(1, 2));
}
public static void main(String[] args) {
Compare com = new Compare1();
Compare com1 = new Compare2();
new InterfaceTest(com);
new InterfaceTest(com1);
}
}
/**
* 利用接口和类实现函数指针的功能
* @author Administrator
*
*/
interface Compare{
public int cmp(int a , int b );
}
class Compare1 implements Compare {
@Override
public int cmp(int a, int b) {
return a;
}
}
class Compare2 implements Compare {
@Override
public int cmp(int a, int b) {
return b;
}
}
public class InterfaceTest {
public InterfaceTest(Compare com){
System.out.println(com.cmp(1, 2));
}
public static void main(String[] args) {
Compare com = new Compare1();
Compare com1 = new Compare2();
new InterfaceTest(com);
new InterfaceTest(com1);
}
}