كيف التحميلات الزائدة JScript وظائف

When الثاني أو المزيد JScript الأعضاء (دالات أو خصائص) في a فئة have the same اسم but different signatures, they are referred إلى كـ "overloaded" دالات (خصائص). The "توقيع" of a دالة هو based تشغيل the رقم, نوع, و ترتيب of معلمات it accepts. الثاني دالات have the same توقيع if they قبول the same رقم of الوسيطات, مع the same نوع, في the same ترتيب. دالات that قبول the same أنواع of الوسيطات في a different ترتيب, أو دالات that have a different رقم of الوسيطات أو الوسيطات of different أنواع have different signatures. (ملاحظة that the أسماء of the الوسيطات have بلا تأثير تشغيل the توقيع). ثابت دالات may also participate في overloading, but كـ مع return أنواع, the ثابت حالة of a أسلوب does not affect its توقيع. Therefore, a ثابت أسلوب مع the same اسم كـ an مثيل أسلوب must have a different معلمة قائمة.

Processing Logic

When an overloaded دالة هو called, the overloaded دالة whose الوسيطات most closely مطابقة the passed الوسيطات هو called, depending تشغيل the الفعلي أنواع of الوسيطات passed إلى the دالة. إذا كانت أنواع الوسيطة تتطابق التحميل الزائد محدد تماما، ثم يتم استدعاء هذا التحميل الزائد. If the وسيطة أنواع do not مطابقة أي التحميل الزائد exactly, a عملية of elimination determines which التحميل الزائد هو called. The عملية of elimination هو based تشغيل how بسهولة الفعلي أنواع can be محول إلى the أنواع في the متوفر التحميلات الزائدة. لمزيد من المعلومات، راجع قسرية في JScript. في this مثال, فئة MethodOverload has three overloaded وظائف named Greetings. The أول التحميل الزائد takes لا معلمات, the ثانية التحميل الزائد takes واحد معلمة of a String نوع, و the third التحميل الزائد takes الثاني معلمات: a String نوع و an int نوع.

var methodOverload = new MethodOverload();
methodOverload.Greetings();
methodOverload.Greetings("Mr. Brown");
methodOverload.Greetings(97, "Mr. Brown");

class MethodOverload
{
   function Greetings()
   {
     print("Hello, and welcome!");
   }
   function Greetings(name : String)
   {
     print("Hello, " + name + "!");
   }
   function Greetings(ticket : int, name : String)
   {
     print("Hello, " + name + "! Your ticket number is " + ticket + ".");
   }
}

The إخراج of this برنامج هو:

Hello, and welcome!
Hello, Mr.Brown!
Hello, Mr.Brown! Your ticket number is 97.

راجع أيضًا:

موارد أخرى

جولة JScript اللغة

أنواع بيانات JScript