在這里,當我們列印陣列元素時,它始終顯示空值,例如“[nil,nil,nil,nil]”值沒有存盤在陣列中。
class Flight
def initilize(flight_id, flight_num, flight_orgin, flight_destination)
@id= flight_id
@number = flight_number
@origin = flight_origin
@destination = flight_destination
end
def read_flight()
puts "enter flight id"
flight_id = gets.chomp
puts "enter flight number"
flight_number = gets.chomp
puts "enter flight origin location"
flight_origin = gets.chomp
puts "enter destination"
flight_destination = gets.chomp
end
def print_flight(id, number, orgin, destination)
puts "_____Flight details______"
puts "Flight_id :#{id}"
puts "Flight_number :#{number}"
puts "Flight_orgin :#{orgin}"
puts "Flight_destination:#{destination}"
end
def read_flights(id, number, orgin, destination)
puts "_______Array of flights______"
flightid = Array.new
flightid.push(id, number, orgin, destination)
puts "#{flightid}"
end
end
input_flight = Flight.new
input_flight.read_flight
input_flight.print_flight(@id, @num, @orgin, @destination)
input_flight.read_flights(@id, @num, @orgin, @destination)
不使用類或實體變數,我們想這樣做
用戶輸入
輸入航班號
2
輸入航班號
2342
輸入航班始發地點
科欽
輸入目的地
電視
輸出
航班詳情_
航班號:
航班號 :
Flight_orgin :
航班目的地:
_一系列航班
[無,無,無,無]
uj5u.com熱心網友回復:
的@id,@num,@orgin,@destination引數將是零,如果你不設定它們的任何地方。
因此,當您撥打這兩個電話時:
input_flight.print_flight(@id, @num, @orgin, @destination)
input_flight.read_flights(@id, @num, @orgin, @destination)
您基本上只需將nils發送到函式中:
input_flight.print_flight(nil, nil, nil, nil)
input_flight.read_flights(nil, nil, nil, nil)
如果要訪問從輸入讀取的變數:
- 首先,您需要將它們存盤在某個地方。例如:在
read_flight呼叫函式時將它們存盤在實體變數中。 - 然后,當您想在陣列中推送值時參考實體變數。
前任:
def read_flight
puts "enter flight id"
@id = gets.chomp # store inside instance variable
puts "enter flight number"
@number = gets.chomp
puts "enter flight origin location"
@origin = gets.chomp
puts "enter destination"
@destination = gets.chomp
end
def read_flights
...
flightid.push(@id, @number, @origin, @destination) # access instance variables here
...
end
您可以在此處了解有關 Ruby 變數范圍(實體變數、全域變數等)的更多資訊:https : //www.techotopia.com/index.php/Ruby_Variable_Scope
uj5u.com熱心網友回復:
這是我的調整版本:
class Flight
attr_reader :id, :number, :origin, :destination
def read_flight
puts "enter flight id"
@id = gets.chomp
puts "enter flight number"
@number = gets.chomp
puts "enter flight origin location"
@origin = gets.chomp
puts "enter destination"
@destination = gets.chomp
end
def print_flight
puts "_____Flight details______"
puts "Flight_id :#{id}"
puts "Flight_number :#{number}"
puts "Flight_orgin :#{origin}"
puts "Flight_destination:#{destination}"
end
def read_flights
puts "_______Array of flights______"
flightid = [id, number, origin, destination]
puts "#{flightid}"
end
end
input_flight = Flight.new
input_flight.read_flight
input_flight.print_flight
input_flight.read_flights
解釋:
ruby 類的每個實體都可以有盡可能多的實體變數(以 開頭@)。這些實體變數存在于一個實體中,因此它們在方法中保持它們的值。
所以你應該為實體變數分配你想要的值,例如:
@id = gets.chomp
然后在另一種方法中使用它:
def print_flight
puts "_____Flight details______"
puts "Flight_id :#{@id}"
end
但是,@每次我們要使用實體變數時都添加是相當乏味的。這就是為什么attr_reader進來。當你寫attr_reader:
attr_reader :id, :number, :origin, :destination
您實際上在 Flight 中宣告了 4 個方法:
def id
@id
end
def number
@number
end
def origin
@origin
end
def destination
@destination
end
然后你可以在id, number, origin, destination沒有前導@`的情況下使用
uj5u.com熱心網友回復:
您正在使用nil建構式 ( def initialize) 中的值進行初始化,以解決您可以將值傳遞給.new或 更改的問題read_flight,如下所示:
def read_flight()
puts "enter flight id"
@flight_id = gets.chomp
puts "enter flight number"
@flight_number = gets.chomp
puts "enter flight origin location"
@flight_origin = gets.chomp
puts "enter destination"
@flight_destination = gets.chomp
end
這將修改類范圍的變數。
或者,您可以使用||運算子在建構式中使用默認值(不推薦):
def initilize(flight_id, flight_num, flight_orgin, flight_destination)
@id= flight_id || 0
@number = flight_number || 0
@origin = flight_origin || ""
@destination = flight_destination || ""
end
uj5u.com熱心網友回復:
首先,要小心,因為你犯了很多小而重要的錯誤。沒關系,我們都是這樣開始的)例如,你的'初始化'方法名稱不正確!
您的:'initilize'
正確:'initialize'
正確命名默認方法很重要。此外,當您使用方法引數初始化變數時:
def initilize(flight_id, flight_num, flight_orgin, flight_destination)
@id= flight_id
@number = flight_num #you have to name it just like argument in method not flight_number, because it does not exist at all
@origin = flight_origin #same here, you forgot one letter
@destination = flight_destination
end
如果您希望用戶初始化您的實體,則不要自己初始化它們,請洗掉 initialize 方法中的引數。此外,您可以在整個類中使用實體變數,這真的很有幫助!
所以,我更正了一點:
class Flight
def read_flight
puts "enter flight id"
@id = gets.chomp
puts "enter flight number"
@number = gets.chomp
puts "enter flight origin location"
@origin = gets.chomp
puts "enter destination"
@destination = gets.chomp
end
def print_flight
puts "_____Flight details______"
puts "Flight_id : " @id.to_s
puts "Flight_number : " @number.to_s
puts "Flight_origin : " @origin
puts "Flight_destination: " @destination
end
def read_flights
puts "_______Array of flights______"
flightid = Array.new
flightid.push({ @id,@number,@origin,@destination })
puts "#{flightid}"
end
end
查看:
input_flight = Flight.new
input_flight.read_flight
input_flight.print_flight
input_flight.read_flights
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/369791.html
