# JavaScript的实现

<script type="text/javascript">

//定义一个Person类
function Person(_age, _weight, _height)
{
this.age = _age;
this.weight = _weight;
this.height = _height;
}

//定义Person类的一个方法
Person.prototype.showInfo = function() {
document.write("age: " + this.age + "  weight: " + this.weight + "  heigt:" + this.height);
};

//persons为Array数组
function sortPerson(persons, funcCompare)
{
var flag = false;
//判断persons是否为数组，判断funcCompare是否为回调 函数
flag = (persons instanceof Array) && (funcCompare instanceof Function);
if(flag)
{
var n = persons.length;
for(var i = 0; i < n-1; i ++)
{
for(var j = 0; j < n-i-1; j ++)
{
if(funcCompare(persons[j], persons[j+1]) > 0)
{
var temp = persons[j];
persons[j] = persons[j+1];
persons[j+1] = temp;
}
}
}
flag = true;
}
return flag;
}

//根据年龄比较两个Person对象
function compareByAge(person1, person2)
{
return person1.age - person2.age;
}

//根据身高比较两个Person对象
function compareByHeight(person1, person2)
{
return person1.height - person2.height;
}

//在页面上显示Person数组的信息
function showPersonArray(persons)
{
var size = persons.length;
for(i = 0; i < size; i ++)
{
persons[i].showInfo();
document.write("<br>");
}
}

</script>

<script type="text/javascript">
var persons = new Array();
persons[0] = new Person(2, 54.5, 0.82);
persons[1] = new Person(31, 74.5, 1.80);
persons[2] = new Person(54, 44.5, 1.59);
persons[3] = new Person(23, 62.0, 1.78);
persons[4] = new Person(16, 45.7, 1.60);
document.writeln("before sort:<br>");
showPersonArray(persons);
sortPerson(persons, compareByAge);
document.writeln("finished sort by age:<br>");
showPersonArray(persons);
sortPerson(persons, compareByHeight);
document.writeln("finished sort by Height:<br>");
showPersonArray(persons);
</script>

//根据年龄排序
sortPerson(persons, function(person1, person2){
return person1.age - person2.age;
});
//根据身高排序
sortPerson(persons, function(person1, person2){
return person1.height - person2.height;
});

JavaScript中Array的sort方法应用的就是这个原理：

arrayObject.sort(sortby)

 参数 描述 sortby 可选。规定排序顺序。必须是函数。

# Java的实现

Java中没有像C++这样的函数指针，所以不能将一个函数通过参数传递给另一个函数。因此Java中没有回调函数，但仍然可以有回调的机制。Java是一种纯面向对象的语言，它的回调可以通过面向对象的特性实现。

ICompare接口：

public interface ICompare {
public int comparable(ICompare obj);
}


Person类：

public class Person implements ICompare {

private int age;
private float height;
private float weight;

public Person(int age, float height, float weight) {
super();
this.age = age;
this.height = height;
this.weight = weight;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}

public float getWeight() {
return weight;
}

public void setWeight(float weight) {
this.weight = weight;
}

@Override
public int comparable(ICompare obj) {
Person p = (Person)obj;
return this.age - p.age;
}

}

public class CallBackTest {

public static void Sort(ICompare[] objs) {
int n = objs.length;
for(int i = 0; i < n-1; i ++) {
for(int j = 0; j < n-i-1; j ++) {
if(objs[j].comparable(objs[j + 1]) > 0)
{
ICompare temp = objs[j];
objs[j] = objs[j + 1];
objs[j + 1] = temp;
}
}
}
}

public static void showArray(Person persons[]){
for(int i = 0; i < persons.length; i ++) {
System.out.println("age:" + persons[i].getAge() + "  weight:" + persons[i].getWeight() + "  height:" + persons[i].getWeight());
}
}

/**
* @param args
*/
public static void main(String[] args) {
Person[] persons = new Person[]{
new Person(2, 54.5f, 0.82f),
new Person(31, 74.5f, 1.80f),
new Person(54, 44.5f, 1.59f),
new Person(23, 62.0f, 1.78f),
new Person(16, 45.7f, 1.60f)
};
System.out.println("before sort:");
showArray(persons);
Sort(persons);
System.out.println("after sort:");
showArray(persons);
}

}

java.util.Arrays中的Sort就用到了这种技术，从Java的API中可以看出：

public static void sort(Object[] a)

# 回调之编程思想

package callback;

/**
* 接口 魔法(神通)
* @author luoweifu
*
*/
interface Magic {
public void doMagic();
}

/**
* 神仙类
* @author luoweifu
*
*/
class Immortal {
private String name;
private Magic magic;

public Immortal(String name) {
super();
this.name = name;
}

public Immortal(String name, Magic magic) {
super();
this.name = name;
this.magic = magic;
}

public void SetMagic(Magic magic) {
this.magic = magic;
}

public String getName() {
return name;
}

public void crossingSea(Magic magic) {
this.magic = magic;
System.out.print(name);
magic.doMagic();
System.out.println("过海...");
}
}

public class crossingSea {

/**
* 八仙过海，各显神通
* @param args
*/
public static void main(String[] args) {
Immortal tieGuaiLi = new Immortal("铁拐李");
tieGuaiLi.crossingSea(new Magic() {
public void doMagic() {
System.out.print("将葫芦变成条葫芦大船");
}
});

Immortal zhangGuoLao = new Immortal("张果老");
zhangGuoLao.crossingSea(new Magic() {
public void doMagic() {
System.out.print("将毛驴变成一只小黑驴");
}
});

Immortal heXianGu = new Immortal("何仙姑");
heXianGu.crossingSea(new Magic() {
public void doMagic() {
System.out.print("将荷叶往海面上一铺，躬身往上一跃，漂浮而去");
}
});

Immortal hanXiangzi = new Immortal("韩湘子");
hanXiangzi.crossingSea(new Magic() {
public void doMagic() {
System.out.print("将玉箫往水中一掷，化作一根浮木，站在上面");
}
});

Immortal liDongBin = new Immortal("吕洞宾");
liDongBin.crossingSea(new Magic() {
public void doMagic() {
System.out.print("把黄龙宝剑投入海中，宝剑化作一只小船载着他");
}
});

Immortal lanCaiHe = new Immortal("蓝采和");
lanCaiHe.crossingSea(new Magic() {
public void doMagic() {
System.out.print("将百花篮变成五彩船悠哉游哉坐上彩船");
}
});

Immortal hanZhongLi = new Immortal("汉钟离");
hanZhongLi.crossingSea(new Magic() {
public void doMagic() {
System.out.print("有一把芭蕉扇，他来了一个鲤鱼大翻 身，躺在扇子上悠闲自得地随波飘荡");
}
});

Immortal caoGuoJiu = new Immortal("曹国舅");
caoGuoJiu.crossingSea(new Magic() {
public void doMagic() {
System.out.print("把阴阳玉板在海面上一字儿排开，犹如脚踏竹排迎激流穿巨浪");
}
});
}

}


避免重复代码

在你需要更多的通用功能的地方更好地实现抽象(可处理各种类型的对象和变量)。

增强代码的可维护性

增强代码的可读性

有更多定制的功能

luoweifu

#### javascript面向对象和回调的一个示例

2013-09-04 19:24:26

#### JavaScript初学者的困惑——回调对象的方法

2009-07-07 17:09:00

#### JavaScript面向对象学习笔记——函数、匿名函数、回调函数、自调函数

2017-04-10 14:55:22

#### Js面向对象学习之function

2015-10-07 19:44:48

#### js 彻底理解回调函数

2017-02-10 13:53:58

#### JS面向对象编程--第三章 函数

2017-03-21 09:43:36

#### JS回调函数全解析教程

2013-01-11 16:43:38

#### 理解和使用 JavaScript 中的回调函数

2014-11-25 01:20:46

#### JavaScript回调函数

2015-06-18 10:30:34

#### WEB入门之十一 JS面向对象

2017-02-21 19:40:21