[英]Java inheritance ( local variable/ boolean in if)
我正在研究繼承(Java),並編寫了以下代碼。 第一部分是CarBase
,然后我創建了一個子類1,稱為Bus
。
我的想法是,首先要判斷它是否是總線,並且這樣做,我需要一個布爾值[ 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;
}
}
使用繼承的目的是抽象一個對象是汽車還是公共汽車,並編寫無論傳遞什么都可以工作的代碼。 為此,您使用抽象方法。 考慮
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中抽象聲明函數。 因此, getOnBus()
將不是一個好主意
對於未聲明的第一點“ isBus”,確定是正確的,因為您已經知道您正在擴展CarBase,所以我看不到該方法的檢查點,但是如果您需要檢查,可以這樣進行
if(this instanceof CarBase)
對於第二點,更改實際上沒有影響
int temp=0; // <===
temp= current_Passenger+p_amount; // <===
首先使用0進行初始化,然后為其分配新值
int temp=current_Passenger+p_amount;
在這里,您使用值初始化溫度
您不需要檢查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;
}
isBus
未聲明為導致您收到此錯誤的原因 Bus
類聲明的,並且您確定它是Bus
而不是父CarBase
類(請使用Vechicle而不是CarBase,根據我的觀點,這要好得多) 所以你可以像這樣簡化getOnBus()
public boolean getOnBus (int p_amount) {
int temp = current_Passenger + p_amount;
if (temp > max_Passenger) return false;
current_Passenger = temp;
return true;
}
要測試對象是否是類的實例,可以使用variable instanceof YourClass
,其結果為布爾值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.