<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Ruby 類案例 下面將創建一個名為 Customer 的 Ruby 類,您將聲明兩個方法: * _display_details_:該方法用于顯示客戶的詳細信息。 * _total_no_of_customers_:該方法用于顯示在系統中創建的客戶總數量。 ``` #!/usr/bin/ruby class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end ``` _display_details_ 方法包含了三個 puts 語句,顯示了客戶 ID、客戶名字和客戶地址。其中,puts 語句: ``` puts "Customer id #@cust_id" ``` 將在一個單行上顯示文本 Customer id,后跟變量 @cust_id 的值。 當您想要在一個單行上顯示實例變量的文本和值時,您需要在 puts 語句的變量名前面放置符號(#)。文本和帶有符號(#)的實例變量應使用雙引號標記。 第二個方法,total_no_of_customers,包含了類變量 @@no_of_customers。表達式 @@no_of_ customers+=1 在每次調用方法 total_no_of_customers 時,把變量 no_of_customers 加 1。通過這種方式,您將得到類變量中的客戶總數量。 現在創建兩個客戶,如下所示: ``` cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") ``` 在這里,我們創建了 Customer 類的兩個對象,cust1 和 cust2,并向 new 方法傳遞必要的參數。當 initialize 方法被調用時,對象的必要屬性被初始化。 一旦對象被創建,您需要使用兩個對象來調用類的方法。如果您想要調用方法或任何數據成員,您可以編寫代碼,如下所示: ``` cust1.display_details() cust1.total_no_of_customers() ``` 對象名稱后總是跟著一個點號,接著是方法名稱或數據成員。我們已經看到如何使用 cust1 對象調用兩個方法。使用 cust2 對象,您也可以調用兩個方法,如下所示: ``` cust2.display_details() cust2.total_no_of_customers() ``` ## 保存并執行代碼 現在,把所有的源代碼放在 main.rb 文件中,如下所示: ``` #!/usr/bin/ruby class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end # 創建對象 cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # 調用方法 cust1.display_details() cust1.total_no_of_customers() cust2.display_details() cust2.total_no_of_customers() ``` 接著,運行程序,如下所示: ``` $ ruby main.rb ``` 這將產生以下結果: ``` Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Total number of customers: 1 Customer id 2 Customer name Poul Customer address New Empire road, Khandala Total number of customers: 2 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看