继承代码

package duixiang;

public class jicheng extends Car {
    public jicheng()
    {
        System.out.println("调用自己的构造方法");
    }
    
    public jicheng(String pinpai,double zaizhong)
    {
        //this.setPinpai(pinpai);
        super(pinpai);//super 代表父类
        this.zaizhong=zaizhong;
    }
    
    //载重量
    private double zaizhong;
    public double getZaizhong() {
        return zaizhong;
    }
    
    //货物重量
    private double huozhong;
    public double getHuozhong() {
        return huozhong;
    }
    //发动
    public void fadong()
    {
        System.out.println("子类吧车发动了");
        if(this.getYouliang()!=this.getYouxiang())
        {
            System.out.println("加满油再发动车");
        }
        else
        {
            System.out.println("子类调用父类发动了汽车");
            super.fadong();
        }
    }
    //装货
    public void zhuanghuo(double huo)
    {
        if(huo>zaizhong)
        {
            System.out.println("不能再装了");
            huozhong = zaizhong;
            return;
        }
        else
        {
            huozhong=huo;
        }
        System.out.println("这次装了"+huo);
    }

}
package duixiang;

public class Test3 {

    public static void main(String[] args) {
        jicheng kache=new jicheng("解放",50);
        kache.setYouxiang(60);
        kache.jiayou(40);
        kache.zhuanghuo(50);
        kache.fadong();
        kache.jiayou(20);
        kache.fadong();
        System.out.println("载重量="+kache.getZaizhong()+"油量");
}
}

 

posted @ 2015-12-14 08:55  琦仔  阅读(353)  评论(0编辑  收藏  举报