Latest web development tutorials
×

Ruby หลักสูตร

Ruby หลักสูตร Ruby แนะนำโดยย่อ Ruby สิ่งแวดล้อม Ruby ติดตั้ง Linux Ruby ติดตั้ง Windows Ruby การเข้ารหัสจีน Ruby เลือกบรรทัดคำสั่ง Ruby ตัวแปรสภาพแวดล้อม Ruby ไวยากรณ์ Ruby ชนิดข้อมูล Ruby คลาสและวัตถุ Ruby กรณีระดับ Ruby ตัวแปร Ruby ผู้ประกอบการ Ruby หมายเหตุ Ruby เงื่อนไข Ruby การไหลเวียน Ruby ทาง Ruby กลุ่ม Ruby โมดูล Ruby เชือก Ruby แถว Ruby กัญชา Ruby วันที่และเวลา Ruby ขอบเขต Ruby iterator Ruby input และ output ไฟล์ Ruby File ชั้นเรียนและวิธี Ruby Dir ชั้นเรียนและวิธี Ruby ผิดปกติ

Ruby การสอนที่ทันสมัย

Ruby เชิงวัตถุ Ruby นิพจน์ปกติ Ruby การเข้าถึงฐานข้อมูล - DBI หลักสูตร Ruby MySQL Ruby CGI โครงการ Ruby CGI ทาง Ruby CGI Cookies Ruby CGI Sessions Ruby ส่ง Email SMTP Ruby Socket โครงการ Ruby XML, XSLT, XPath Ruby Web Services Ruby multithreading Ruby JSON Ruby RubyGems

ทับทิมกรณีคลาส

ต่อไปนี้จะสร้างชื่อลูกค้าระดับทับทิมประกาศสองวิธี:

  • 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 "Customer id #@cust_id"

รหัสลูกค้าแสดงค่าข้อความและตัวแปร @cust_id บนบรรทัดเดียว

เมื่อคุณต้องการที่จะแสดงข้อความและเช่นค่าตัวแปรในบรรทัดเดียว, คุณจำเป็นต้องวางสัญลักษณ์ (#) ในด้านหน้าของคำสั่งที่ทำให้ชื่อตัวแปร ข้อความและอินสแตนซ์ตัวแปรที่มีสัญลักษณ์ (#) ควรใช้เครื่องหมายอัญประกาศ

วิธีที่สอง total_no_of_customers มีตัวแปรระดับ @@ no_of_customers การแสดงออก @@ no_of_ ลูกค้า + = 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")

ที่นี่เราสร้างสองชั้นของวัตถุลูกค้า cust1 และ cust2 และส่งผ่านพารามิเตอร์ที่จำเป็นวิธีการใหม่ เมื่อวิธีการเริ่มต้นจะเรียกที่คุณลักษณะที่จำเป็นของวัตถุที่จะเริ่มต้น

เมื่อวัตถุถูกสร้างขึ้นคุณจำเป็นต้องใช้วัตถุทั้งสองจะเรียกวิธีการเรียนที่ หากคุณต้องการที่จะเรียกวิธีการหรือข้อมูลใด ๆ ที่สมาชิกคุณสามารถเขียนโค้ดดังนี้

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()

จากนั้นเรียกใช้โปรแกรมดังต่อไปนี้:

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