将这个类作为Java学习的第一个类,简单易懂易上手。

/**
 * 正方体类
  */
public class Cube {
    private int length;// 正方体边长

    private static final float DENSITY=19.3f;// 黄金密度/比重

    /**
     * 构造函数
     * @param length
     */
    public Cube(int length){
        this.length=length;
    }

    /**
     * 得到体积
     * @return
     */
    public float getVolume(){
        return (float)(length*length*length);
    }

    /**
     * 得到重量
     * @return
     */
    public float getWeight(){
        return DENSITY*getVolume();
    }

    /**
     * 程序入口
     * @param args
     */
    public static void main(String[] args){
        Cube c=new Cube(10);
        System.out.println("Volume="+c.getVolume());
        System.out.println("Weight="+c.getWeight());
    }
}

需要的MyEclipse可以从这里下载:

http://pan.baidu.com/s/1CBA9a

http://pan.baidu.com/s/144fTk

先灌输Java语法,再教编程;有如先讲格律,再教写诗,吓得未来之李白杜甫都一脑浆糊,不敢动手了。

所以,先教编程,缓教语法是上策。

05-15 04:49