返回列表 发帖

java入门123-一个老鸟的java学习心得280页疑问

package com.javaeasy.override;
import com.javaeasy.override.ElectronicBus;
import com.javaeasy.override.CarBase;
public class UseCar {
        public CarBase car;
        public void setCar(CarBase car){
                System.out.println("setCar(CarBase)方法被调用了。");
                this.car = car;
        }
       
        public void setCar(ElectronicBus bus){
                System.out.println("setCar(ElectronicBus)方法被调用了。");
                this.car = bus;
        }
}

其中
this.Car =bus;
一句提示错误cann't convert  ElectronicBus to CarBase。
ElectronicBus 是继承的 CarBase类。
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

建议你把类的定义代码贴出来。这样才能判断。
在网上购买本书的读者,请给本书发一个好评。谢谢大家。

TOP

本帖最后由 bytwohitsnow 于 2014-3-31 12:01 编辑

回复 2# fwbook


    package com.javaeasy.override;

import com.javaeasy.learnextends.Bus;

public class ElectronicBus extends Bus {
        int carriageNumber = 2;
        public ElectronicBus(){
                System.out.println("Electronic类的构造方法被调用了!");
        }
}

突然发现这段代码的第二行的import语句不对劲儿,然后删除了,就不提示错误了。。。谢啦!猜测因为有了这句,然后Bus的跳到Package com.javaeasy.learnextends中去了。

TOP

回复 2# fwbook


    package com.javaeasy.override;

public class Bus extends CarBase {
        public int max_Passenger = 35;
        public int current_Passenger = 0;
        public int max_slow = 27;
       
        boolean isBus = true;
       
        public Bus(){
                System.out.println("Bus类的构造方法被调用了!");
        }
       
        public void slowDown(int p_speed){
                System.out.println("Bus类中定义的speedDown(int)方法被调用了。");
                if(p_speed > max_slow){
                        p_speed = max_slow;
                }
                if(p_speed > 0){
                        int tempSpeed = speed - p_speed;
                        if(tempSpeed >= 0){
                                speed = tempSpeed;
                        }
                }
        }
       
        public Bus(String color, int maxSpeed, String name, int speed, int current_Passenger, int max_Passenger){
                super(color, maxSpeed, name, speed);
                this.current_Passenger = current_Passenger;
                this.max_Passenger = max_Passenger;
                System.out.println("Bus类有参数的构造方法被调用了!");
        }
       
        public boolean getOnBus(int p_amount){
                if(isBus){
                        int temp = current_Passenger + p_amount;
                        if(temp > max_Passenger){
                                System.out.println("temp > max_Passenger!");
                                return false;
                        }else{
                                current_Passenger = temp;
                                return true;
                        }
                }
                return false;
        }
       
        public boolean getDownBus(int p_amount){
                if(isBus){
                        int temp = current_Passenger - p_amount;
                        if(temp < 0){
                                System.out.println("temp < 0!");
                                return false;
                        }else{
                                current_Passenger = temp;
                                return true;
                        }
                }
                return false;
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub

        }

}

TOP

回复 2# fwbook


    package com.javaeasy.override;

public class CarBase {
        public int speed;
        public String name;
        public String color;
        public int maxSpeed = 90;
       
        public CarBase(String color, int maxSpeed, String name, int speed){
                this.color = color;
                this.maxSpeed = maxSpeed;
                this.name = name;
                this.speed = speed;
                System.out.println("CarBase类有参数的构造方法被调用了!");
        }
       
        public CarBase(){
                System.out.println("CarBase类的构造方法被调用了!");
        }

        public void followSpeed(CarBase car){
                int newSpeed = car.speed;
                if(newSpeed > speed){
                        this.speedUp(newSpeed - this.speed);
                        //调用自己i的方法,this可以省略
                }else{
                        slowDown(this.speed - newSpeed);
                }
        }
        public void speedUp(int p_speed){
                System.out.println("CarBase类中定义的speedUp(int)方法被调用了。");
                int tempSpeed = 0;
                if(p_speed > 0){
                        tempSpeed = speed + p_speed;
                }
                if(tempSpeed <= maxSpeed){
                        speed = tempSpeed;
                }
        }
        public void slowDown(int p_speed){
                System.out.println("CarBase类中定义的speedDown(int)方法被调用了。");
                int tempSpeed = speed - p_speed;
                if(tempSpeed >= 0){
                        speed = tempSpeed;
                }
        }
}

TOP

问题解决了?
在网上购买本书的读者,请给本书发一个好评。谢谢大家。

TOP

返回列表