2016-05-17 267 views
1

我正在研究继承(Java),并且我写了下面的代码。第一部分是CarBase,然后我创建了一个名为Bus的子类1。Java继承(局部变量/布尔if)

我的想法是,先做个判断,如果它是一个总线,并且通过这样做,我需要一个布尔[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; 
    } 
} 
+4

'getOnBus'是一种方法,巴士班,那么为什么你认为你必须“作出判断,如果它是公共汽车”。你肯定知道这是一辆公共汽车。对于第二个问题, – Eran

+0

,没有区别。另外''getOnBus'方法在'Car'类中对我没有意义,因为它似乎仅限于'Bus'。 – SomeJavaGuy

+0

请一次提出一个问题。 – EJP

回答

-1

要测试对象是否是一个实例您可以使用variable instanceof YourClass,其中评估为布尔值

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 
} 

注意,这样的函数声明,你应该设计你的代码,在车辆的抽象,对于汽车和公交车。因此getOnBus()将是一个坏主意

0

确定第一个点“isBus”没有声明,我看不到在这种方法检查点,因为你已经知道你扩展了CarBase,但如果你需要检查你能做到这样

if(this instanceof CarBase) 

第二点其实有你有0初始化,然后分配新的值给它

的变化

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

第一无影响

int temp=current_Passenger+p_amount; 

在这里你初始化值临时

0

你不需要检查总线对象isBus()“......这是一个总线,因为您所定义的类作为总线!

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

Bus BigYellowBus0001 = new Bus(); 

,如果你要那么说:

BigYellowBus0001.getOnBus(10); 

你不会需要检查BigYellowBus0001是否是巴士....对不对?

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

我想也许你已经错误地判断出巴士是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; 
} 
+0

非常感谢您的回复。对不起,当你说“我认为也许你是通过判断公交车是轿车的一个子类而走错了路”的时候,我并不完全明白。我确实认为巴士是Car/CarBase(超级)的子类(儿童类)。我明白错了吗?我是java的开始,请指导我,谢谢! –

+0

是的,你的代码是好的......而且你正确地做了你的课程,但是我要说的是在真实世界里,也许巴士不是一种汽车......所以你可能会让事情变得困难你自己。如果您可以更改它,以便您的超类是VEHICLE ...并且CAR *和* BUS是VEHICLE的子类。合理?只是一个建议,不用担心。 –

0
  1. isBus没有宣布的原因你有这个错误
  2. 你不需要这个检查,因为这个方法声明为Bus类,你可以肯定它是什么Bus不是父母CarBase类(请使用Vechicle而不是CarBase,这对我的意见更好)
  3. 在Java 0是整数默认值,所以你并不需要初始化变量之前分配新值

因此可以简化getOnBus()

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