---
# System prepended metadata

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

---

Java — Class(四) — Project(2)
===

## Class總結 -- Project(2)

### 動作要求:設計可互動之class,關係圖如下,其中class animal已完成,針對abstract method **work** 要求為
* ### class people :work()執行後會使 HP下降
* ### class cat    :work()執行後會使people的HP上升
* ### class lion   :work()執行後會使people的HP下降


![class關係圖](https://i.imgur.com/65YwbeN.jpg)

## class animal : 
```java=
package animal;
public abstract class animal
{
	public String myname ;
	private static int HP;
	
	public animal(String name)
	{
		this.myname = name;
	}
	
	public animal(String name,int en)
	{
		this.myname = name;
		this.HP =en ;
	}
	
	public abstract void shout();
	
	public abstract void work();
	
	public int get_HP()
	{
		return	this.HP;
	}
	
	public void adjust_HP(int value)
	{
		HP += value ;
	}
	
}
```
*Note* : `private static int HP;` HP設為static才可以供不同class互動.

### ==Point== :static用法,abstract使用,封裝等級,繼承,this與super使用,Override,Overload.

---

## class people : 完成初始化,需要Override之method完成即可

```java=
package TwoFoot;
import animal.*;
import FourFoot.*;
public class people extends animal 
{
	public people(String name,int en)
	{
		super(name,en);
		System.out.println("I am a people and my name is "+ this.myname);
	}
	final int workload = -10 ;
	
	@Override
	public void shout()
	{
		System.out.println("RRRRRRRRRRRRRR~~ ");
	}
	@Override
	public void work()
	{
		System.out.println("Coding Coding ");
		this.adjust_HP(workload);
	}
}
```

*Note* : `final int workload = -10 ; ` 設定為final以免被竄改.

## class cat : 完成初始化,完成Override即可

```java=
package FourFoot;
import animal.*;
public class cat extends animal 
{
	public cat(String name)
	{
		super(name);
		System.out.println("I am a cat and my name is "+ this.myname);
	}
	final int health = 15 ;
	
	@Override
	public void shout()
	{
		System.out.println("meow meow~~~ ");
	}
	@Override
	public void work()
	{
		this.adjust_HP(health);
	}
	
}
```

## class lion :因為繼承cat,注意this,super的用法

```java=
package FourFoot;
import animal.*;
public class lion extends cat
{
	final int Damagement;
	
	public lion(String name)
	{
		super(name);
		this.Damagement = -(super.health);
	}
	
	@Override
	public void work()
	{
		this.adjust_HP(Damagement);
		System.out.println("Lion is attacking");
	}
	
	@Override 
	public void shout()
	{
		System.out.println("I am an animal KING");
	}
	
}
```

## Test code: 

```java=
import TwoFoot.*;
import FourFoot.*;
public class Test
{
	public static void main (String arg[])
	{
		cat c1 = new cat("mao");
		people p1 = new people("yuan",100);
		lion l1 = new lion("The King");
		
		
		System.out.println("HP  :" + p1.get_HP());
		
		for(int i=0;i<5;i++)
		{
			p1.work();
			p1.shout();
		}
		
		System.out.println("HP  :" + p1.get_HP());
		
		System.out.println("OK! My job has been finished.");
		
		for(int i=0;i<2;i++)
		{
			c1.shout();
			c1.work();
			System.out.println("HP  :" + p1.get_HP());
		}
		
		System.out.println("When the cat has already grown up.");
		
		l1.shout();
		l1.work();		
		p1.shout();
		
		System.out.println("HP  :" + p1.get_HP());
	}
}
```

## Test Output : 

```
I am a cat and my name is mao
I am a people and my name is yuan
I am a cat and my name is The King
HP  :100
Coding Coding
RRRRRRRRRRRRRR~~
Coding Coding
RRRRRRRRRRRRRR~~
Coding Coding
RRRRRRRRRRRRRR~~
Coding Coding
RRRRRRRRRRRRRR~~
Coding Coding
RRRRRRRRRRRRRR~~
HP  :50
OK! My job has been finished.
meow meow~~~
HP  :65
meow meow~~~
HP  :80
When the cat has already grown up.
I am an animal KING
Lion is attacking
RRRRRRRRRRRRRR~~
HP  :65
```

## 資料夾檔案位子配置 : 

* **Project2**
    * *animal.java*
    * *cat.java*
    * *lion.java*
    * *people.java*
    * *Test.java*
    * **classes**
        * *Test.class*
        * **animal**
            * *animal.class*
        * **FourFoot**
            * *cat.class*
            * *lion.class*
        * **TwoFoot**
            * *people.class*

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