---
title: "java静态分派"
author: "lichaozhangobj"
date: "2011-02-15T15:15:27Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-02-16T10:32:03Z"
id: 906899
url: "https://lichaozhangobj.iteye.com/blog/906899"
---

静态分派发生在编译时期，分派根据静态类型信息发生。方法重载就是静态分派

 

```
public class BlackHorse implements Horse{

}
```

 

```
public class BlackHorse implements Horse{

}
```

 

```
public class WhiteHorse implements Horse {

}
```

 

```
public class Mozi {

    public void ride(Horse h){
        System.out.println("Riding a horse");
    }
    public void ride(WhiteHorse h){
        System.out.println("Riding a white horse");
    }
    public void ride(BlackHorse h){
        System.out.println("Riding a black horse");
    }
}
```

 

```
public class Client {

    public static void main(String[] args) {
        Horse horse = new BlackHorse();
        Horse horse2 = new WhiteHorse();
        
        Mozi mozi = new Mozi();
        mozi.ride(horse);
        mozi.ride(horse2);
    }
}
```

  运行结果：

```
Riding a horse
Riding a horse
```