【问题标题】:Java inheritance ( local variable/ boolean in if)Java继承(局部变量/ if中的布尔值)
【发布时间】:2016-09-13 02:17:34
【问题描述】:

我正在研究继承(Java),并编写了以下代码。第一部分是CarBase,然后我创建了一个子类1,称为Bus

我的想法是先判断是否是总线,通过这样做,我需要一个布尔值[if(isBus)],但是当我在Eclipse中编写这段代码时,有一个错误消息,说'@ 987654324@ 无法解析为变量'。 有人可以告诉我如何解决这个问题吗?我需要先声明布尔变量吗?

另一个问题是关于局部变量的声明。 在 getOnBus(0 方法中,我有一个名为 temp 的局部变量,我被告知,每当在方法中使用局部变量时,我需要先声明它,然后才能使用它,但我看到有人像下面这样直接使用,我在徘徊这两者有什么区别?

public class Bus extends CarBase {
    //Unique bus properties

    public int max_Passenger = 35;
    public int current_Passenger = 0;

    // unique bus method
    public boolean getOnBus(int p_amount) {
        if(isBus) {
            int temp = 0; // <===
            temp = current_Passenger + p_amount; // <===

            if( temp > max_Passenger) {
                return false;
            } else {
                current_Passenger = temp;
                return true;
            }
        }
        return false;
    }
}

或者如果我使用它而不声明它是否有区别?

    public class Bus extends CarBase {
    //Unique bus properties

    public int max_Passenger = 35;
    public int current_Passenger = 0;

    // unique bus method
    public boolean getOnBus (int p_amount) {
        if(isBus) {
            int temp=current_Passenger+p_amount;  // <====

            if( temp > max_Passenger) {
                return false;
            } else {
                current_Passenger = temp;
                return true;
            }

        }
        return false;
    }
}

代码如下

第一部分 CarBase(父)

public class CarBase {
    public int speed;
    public String name;
    public String color;
    public int maxSpeed = 90;

    // Method
    public void speedUp(int p_speed) {
        int tempSpeed = 0;
        if (p_speed > 0) {
            tempSpeed = speed + p_speed;
        }
        if (tempSpeed <= maxSpeed) {
            speed =tempSpeed;
        }
    }
}

第二部分总线(Child1)

public class Bus extends CarBase {
    //Unique bus properties

    public int max_Passenger = 35;
    public int current_Passenger = 0;

    // unique bus method
    public boolean getOnBus (int p_amount) {
        if (isBus) {
            int temp = 0;
            temp = current_Passenger + p_amount;

            if (temp > max_Passenger) {
                return false;
            } else {
                current_Passenger = temp;
                return true;
            }
        }
        return false;
    }
}

【问题讨论】:

  • getOnBusBus 类的一个方法,那你为什么认为必须“判断是否是总线”。你肯定知道这是一辆公共汽车。
  • 对于第二个问题,没有区别。此外,getOnBus 方法在 Car 类中对我来说没有意义,因为它似乎仅限于 Bus
  • 请一次一个问题。
  • @Eran 非常感谢您的帮助。 “isBus”对我来说成为问题的原因是
  • @Eran 非常感谢您的评论。如果我有一个子类2,即SportsCar,“getOnBus”方法对SportsCar没有任何意义,但对于Bus,在这种情况下,是否需要“判断是否是公共汽车”?

标签: java variables if-statement local


【解决方案1】:

要测试一个对象是否是一个类的实例,你可以使用variable instanceof YourClass,它的计算结果是一个布尔值

【讨论】:

    【解决方案2】:

    使用继承的关键在于抽象出一个对象是 Car 还是 Bus,并编写不管传递什么都可以工作的代码。为此,您使用抽象方法。考虑

    abstract class Vehicle {
        private int occupied;
    
        public Vehicle() {
            occupied = 0;
        }
    
        public abstract int getCapacity();   // number of passengers
    
        public boolean board(int howmany) {
            if (occupied+howmany <= capacity) {
                occupied += howmany;
                return true;
            }
            else
                return false;
        }
    
        public void unboard(int howmany) {
            occupied -= howmany;
        }
    };
    
    class Car extends Vehicle {
        public Car ()                { super(); } // just call the Vehicle() constructor
        public int getCapacity()    { return 5; }
    }
    
    
    class Bus extends Vehicle {
        public Bus()                { super(); } // just call the Vehicle() constructor
        public int getCapacity()    { return 32; }
    }
    

    您将编写每个函数来接受车辆,并处理它而不需要知道它是公共汽车还是汽车。 (以下为哑函数,仅举个例子)

    void board_on_first_avaible(Vehicle[] x, int n) {
       for (int i=0; i<x.length; x++)
           if (x.board(n))
               return true; // board ok
    
       return false;  // couldn't board on anything
    }
    

    请注意,您应该设计代码,以便在 Vehicle 中为 Car 和 Bus 声明函数抽象。因此getOnBus() 是个坏主意

    【讨论】:

      【解决方案3】:

      好的,第一点“isBus”没有声明,我看不到检查这个方法的意义,因为你已经知道你正在扩展 CarBase,但如果你需要检查,你可以这样做

          if(this instanceof CarBase)
      

      对于第二点,更改实际上没有效果

         int temp=0; // <===
         temp= current_Passenger+p_amount; // <===
      

      首先使用 0 进行初始化,然后将新值分配给它

         int temp=current_Passenger+p_amount;
      

      在这里你用值初始化温度

      【讨论】:

        【解决方案4】:

        您不需要检查 Bus 对象 'isBus()' .... 它是否是 Bus,因为您将类定义为 Bus!

        所以...如果你要创建一个新的 Bus 对象,你会这样说:

        Bus BigYellowBus0001 = new Bus();
        

        如果你说:

        BigYellowBus0001.getOnBus(10);
        

        您不需要检查 BigYellowBus0001 是否是公共汽车......对吗?

        实际上,您甚至不需要将方法命名为 getOnBus().... 它可以只是 getOn。

        我认为您可能因为认为 Bus 是 Car 的子类而走错了路。

        至于局部变量,这只是意味着在方法内部开始和结束的变量......所以你用你的'temp'变量做得很好。

        为了表明您了解如何从子类访问超类的变量,您可以在让人们上车之前检查总线的速度:

        public boolean getOnBus (int p_amount){
            if(speed = 0){
                int temp=0;
                temp= current_Passenger+p_amount;
        
                if( temp > max_Passenger){
                    return false;
                } else{
                    current_Passenger = temp;
                    return true;
                }
        
            }
            return false;
        }
        

        【讨论】:

        • 非常感谢您的回复。抱歉,当您说“我认为您可能错误地认为 Bus 是 Car 的子类”时,我不太明白。我确实认为总线是 Car/CarBase(超类)的子类(子类)。我理解错了吗?我是java初学者,请指导一下,谢谢!
        • 是的,你的代码很好......你的课程是正确的,但我要说的是,在现实世界中,也许巴士不是一种汽车......所以你可能会为自己制造困难。如果您被允许更改它,以便您的超类是 VEHICLE……那么 CAR AND BUS 是 VEHICLE 的子类。有道理?只是一个建议,不用担心。
        【解决方案5】:
        1. isBus 未声明您收到此错误的原因
        2. 您不需要此检查,因为此方法为 Bus 类声明,并且您确定它是 Bus 不是父类 CarBase 类(请使用 Vechicle 而不是 CarBase,它在我的意见)
        3. 在 Java 0 中是 int 的默认值,因此在分配新值之前不需要初始化变量

        所以你可以像这样简化getOnBus()

        public boolean getOnBus (int p_amount) {
            int temp = current_Passenger + p_amount;
            if (temp > max_Passenger) return false;
            current_Passenger = temp;
            return true;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多