abstract class Animal{
創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、東營區(qū)網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為東營區(qū)等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
String type;
public Animal(String type){
this.type=type;
}
public abstract void Talk();
public void showInfo(){Talk();}
}
class Dog extends Animal{
String name;
public Dog(String type,String name){
super(type);
this.name=name;
}
public void Talk(){
System.out.println("此動物是:"+name+",此動物屬于:"+type);
}
}
class Cat extends Animal{
String name;
public Cat(String type,String name){
super(type);
this.name=name;
}
public void Talk(){
System.out.println("此動物是:"+name+",此動物屬于:"+type);
}
}
public class Diy_5_2_1{
public static void main(String[] args){
Dog doggie=new Dog("犬科動物","德國黑貝");
Cat kitty=new Cat("貓科動物","波斯貓");
doggie.showInfo();
kitty.showInfo();
}
}
首先,手動畫一個小烏龜,如下:
然后,按照J(rèn)ava繪圖基本步驟一步步來。
swing 編程步驟:
1. 繼承JFrame
2. 定義組件
3.創(chuàng)建組件(構(gòu)造函數(shù))
4.添加組件
5.對窗體設(shè)置
6.顯示窗體
最終效果如下:
代碼如下:
/**?
*?功能:畫一個烏龜?
*/??
package?com.test1;??
import?java.awt.*;??
import?javax.swing.*;??
public?class?MyTortoise??extends?JFrame{??
MyPanel2?mp?=?null;??
//構(gòu)造函數(shù)??
public?MyTortoise(){??
mp?=?new?MyPanel2();??
this.add(mp);??
this.setTitle("小烏龜,丑丑噠");??
this.setSize(400,300);??
this.setVisible(true);??
this.setLocation(300,200);??
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
}??
public?static?void?main(String[]?args)?{??
MyTortoise?mtg?=?new?MyTortoise();??
}?????
}??
//我的面板。只有JPanel有畫圖方法,JFrame沒有,故必須在JFrame中添加JPanel??
class?MyPanel2?extends?JPanel{??
//定義一個烏龜??
Tortoise?t?=?null;??
//構(gòu)造函數(shù)??
public?MyPanel2(){????
t?=?new??Tortoise(100,100);??
}??
//畫烏龜??
public?void?drawTortoise(int?x,?int?y,?Graphics?g){??
//1.畫臉??
g.setColor(Color.green);??
g.fillOval(x+60,?y,?30,?15);??
//2.畫左眼??
g.setColor(Color.black);??
g.fillOval(x+65,?y+3,?5,?5);??
//3.畫右眼??
g.fillOval(x+78,?y+3,?5,?5);??
//4.畫脖子??
g.setColor(Color.green);??
g.fillOval(x+70,?y,?10,?42);??
//5.畫烏龜殼??
g.setColor(Color.red);??
g.fillOval(x+40,?y+40,?70,?100);??
//6.畫左上腳??
g.setColor(Color.green);??
g.fillOval(x+15,?y+60,?30,?10);??
//7.畫右上腳??
g.fillOval(x+105,?y+60,?30,?10);??
//8.畫左下腳??
g.fillOval(x+15,?y+110,?30,?10);??
//9.畫右下腳??
g.fillOval(x+105,?y+110,?30,?10);??
//10.畫尾巴??
g.setColor(Color.black);??
g.drawLine(x+70,y+140,x+130,y+210);??
g.drawOval(x+95,?y+150,?30,?30);??
}??
//覆蓋JPanel的paint方法??
//Graphics?是繪圖的重要類。你可以把他理解成一只畫筆??
public?void?paint(Graphics?g){??
//1.調(diào)用父類函數(shù)完成初始化任務(wù)??
//這句話不能少??
super.paint(g);??
//2.畫烏龜,調(diào)用方法即可??
this.drawTortoise(50,?50,?g);??
}??
}??
//定義一個烏龜類??
class?Tortoise?{??
//表示烏龜?shù)臋M坐標(biāo)??
int?x?=?0;??
//表示烏龜?shù)目v坐標(biāo)??
int?y?=?0;??
public?int?getX()?{??
return?x;??
}??
public?void?setX(int?x)?{??
this.x?=?x;??
}??
public?int?getY()?{??
return?y;??
}??
public?void?setY(int?y)?{??
this.y?=?y;??
}??
public?Tortoise(int?x,?int?y){??
this.x?=?x;??
this.y?=?y;??
}??
}
/**
* 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!
------------------------------------
不會測試就不測試唄,我輸出都幫你寫出來了。
------------------------------------
測試結(jié)果就是上面的內(nèi)容。把以上兩個程序保存成 Animal.java 和 TestAnimal.java。放入一個新建名為 animal 的文件夾。比如路徑是 C:\animal\,運(yùn)行 cmd,輸入:cd\ 回車,
再輸入:C: 回車,
然后輸入:javac animal\Animal.java animal\TestAnimal.java 回車,
如果沒有什么錯誤提示,再輸入:java animal.TestAnimal 回車。
---------------就可以了!