---
# System prepended metadata

title: Java — Class(二) — Project(2)
tags: [教學, java]

---

Java — Class(二) — Project(2)
===
## Overload / Override 

### **Overload**
對於一個method可以給予多個實作方法,差別在於**輸入(input arguments)不同**
For instance:
```java
//Example 1
public class cat
{
    public String name;
    public int weight;
    
    public cat(String name)
    {
        this.name = name;
    }
    public cat(String name,int weight)
    {
        this.name = name;
        this.weight = weight;
    }
}
```
```java
//Example 2
public class cat
{
    public String name;
    public int weight;
    
    public void work()
    {
        //Do nothing
    }
    public void work(String name)
    {
        System.out.println(name + "was eating");
    }
}
```

### **Override**
繼承者可以直接**變更** ==父class的函式的動作== ,或必須給予實作當父class的函式為abstract method時

```java
@Override
public void foo()
{
    ...
}
```

*Note* : @Override用來告知編譯器下面的函式將被Override,可加可不加,加了可以增加可讀性.

---

## 補充:參數的傳遞

* 傳陣列或自訂物件為 call by variable
* 其他都為 call by value

```java
// Call by variable
public void (int[] k)
{
...
}
public void (cat mao)
{
//cat 為自己設立的物件
...
}
```
```java
// Call by value
public void (int abc)
{
...
}
public void (String mom)
{
...
}
```

###### tags: `java` `教學` 