Java继承(if中的局部变量/ bolean)

时间:2016-05-17 06:07:13

标签: java variables if-statement local

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

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

另一个问题是关于局部变量的声明。 在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;
    }
}

5 个答案:

答案 0 :(得分:0)

使用inherance的要点是抽象一个对象是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
}

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

答案 1 :(得分:0)

确定第一个点“isBus”未声明,我无法看到检查此方法的点,因为您已经知道你正在扩展CarBase但是如果你需要检查你可以这样做

    if(this instanceof CarBase)

对于第二点,实际上对改变没有影响

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

首先用0初始化,然后为其分配新值

   int temp=current_Passenger+p_amount;

这里用值

初始化temp

答案 2 :(得分:0)

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

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

Bus BigYellowBus0001 = new Bus();

如果你那么说:

BigYellowBus0001.getOnBus(10);

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

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

我认为通过判断Bus是汽车的子类,你可能已经走错了路。

对于局部变量,这只是意味着在方法内部开始和结束的变量......所以你很好地使用了'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;
}

答案 3 :(得分:0)

  1. isBus未声明您收到此错误的原因
  2. 您不需要此检查,因为此方法已为Bus类声明,并且您确定它是Bus而不是父CarBase类(请使用Vechicle代替CarBase,我的观点要好得多)
  3. 在Java 0中,int是默认值,因此在分配新值
  4. 之前不需要init变量

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

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

答案 4 :(得分:-1)

要测试对象是否是类的实例,您可以使用variable instanceof YourClass来计算布尔值