導航:首頁 > 文件處理 > 有人給動物文件夾

有人給動物文件夾

發布時間:2023-10-12 04:05:44

java設計一個動物類,包括動物的基本屬性。例如名稱,大小,重量等,並設計相應的動作例如跑,跳,走等。

/**
* Animal.java
*/
package animal;

public class Animal {
private String name; //名稱
private int bodySize; //大小
private float weight; //體重

public Animal (String name, int bodySize, float weight) {
this.name = name;
this.bodySize = bodySize;
this.weight = weight;
}

public void run () { //跑
System.out.println("I'm running!");
}

public void jump () { //跳
System.out.println("I'm jumping!");
}

public void walk () { //走
System.out.println("I'm walking!");
}

public String getName () {
return name;
}

public int getBodySize () {
return bodySize;
}

public int getWeight () {
return weight;
}

public String toString () {
return ("I'm a " + name +
", my body size is " + bodySize +
", and my weight is " + weight + ".");
}
}

/**
* 這是一個測試類: TestAnimal.java
*/
package animal;

public class TestAnimal {
public static void main (String args[]) {
Animal ani = new Animal("tiger", 5, 10.5);
System.out.println(ani.toString());
ani.run();
ani.jump();
ani.walk();
}
}

------------------------------------
輸出:
I'm a tiger, my body size is 5, my weight is 10.5.
I'm running!
I'm jumping!
I'm walking!

------------------------------------
不會測試就不測試唄,我輸出都幫你寫出來了。

------------------------------------
測試結果就是上面的內容。把以上兩個程序保存成 Animal.java 和 TestAnimal.java。放入一個新建名為 animal 的文件夾。比如路徑是 C:\animal\,運行 cmd,輸入:cd\ 回車,
再輸入:C: 回車,
然後輸入:javac animal\Animal.java animal\TestAnimal.java 回車,
如果沒有什麼錯誤提示,再輸入:java animal.TestAnimal 回車。
---------------就可以了!

閱讀全文

與有人給動物文件夾相關的資料

熱點內容
程序員審核職位 瀏覽:385
德龍空調壓縮機 瀏覽:780
紅旗app如何注冊新賬戶 瀏覽:360
慣導pdf 瀏覽:606
c程序員的平均工資 瀏覽:58
微小店源碼 瀏覽:801
編譯原理答題題庫 瀏覽:169
ubuntu編程入門 瀏覽:301
antbuild命令 瀏覽:771
怎麼訂閱伺服器 瀏覽:593
視頻專用加密器哪個好用 瀏覽:295
app無法使用網路哪裡設置 瀏覽:847
紅旗linux怎麼安裝 瀏覽:136
各種破車窗工具測試解壓 瀏覽:933
優格手錶app如何使用 瀏覽:716
docker進入容器命令 瀏覽:498
先河私有雲伺服器價格 瀏覽:459
java字體代碼 瀏覽:829
linux前台運行 瀏覽:618
0到99c語言編程 瀏覽:874