نمایش نتایج 1 تا 3 از 3

نام تاپیک: کلمه کلیدی override برای چیست؟

  1. #1
    کاربر دائمی آواتار alexmcse
    تاریخ عضویت
    فروردین 1391
    محل زندگی
    شیراز
    پست
    860

    Smile کلمه کلیدی override برای چیست؟

    سلام

    کلمه کلیدی override برای چیست؟
    لطفا مثال بزنید
    تشکر

  2. #2

    نقل قول: کلمه کلیدی override برای چیست؟

    سلام
    وجود override modifier برای بسط یا تغییر پیاده سازی Virtualیا Abstract یک method, property, indexer, event مشتق شده الزامی است.
    مثال
    در این مثال کلاس Square باید یک پیاده سازی Override شده Area مهیا کند چون Square از کلاس Abstract ShapesClass مشتق شده است.

    abstractclass ShapesClass

    {
    abstractpublicint Area();
    }
    class Square : ShapesClass
    {
    int side = 0;

    public Square(int n)
    {
    side = n;
    }
    // Area method is required to avoid
    // a compile-time error.
    publicoverrideint Area()
    {
    return side * side;
    }

    staticvoid Main()
    {
    Square sq = new Square(12);
    Console.WriteLine("Area of the square = {0}", sq.Area());
    }

    interface I
    {
    void M();
    }
    abstractclass C : I
    {
    publicabstractvoid M();
    }

    }
    // Output: Area of the square = 144



    ترجمه بقیش با خودت:
    An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. For information about inheritance, see Inheritance (C#‎‎ Programming Guide).
    You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
    An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.
    You cannot use the new, static, or virtual modifiers to modify an override method.
    An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.
    For more information about how to use the override keyword, see Versioning with the Override and New Keywords (C#‎‎ Programming Guide) and Knowing when to use Override and New Keywords.
    This example defines a base class named Employee, and a derived class named SalesEmployee. The SalesEmployee class includes an extra property, salesbonus, and overrides the method CalculatePay in order to take it into account.
    class TestOverride
    {
    public class Employee
    {
    public string name;

    // Basepay is defined as protected, so that it may be
    // accessed only by this class and derrived classes.
    protected decimal basepay;

    // Constructor to set the name and basepay values.
    public Employee(string name, decimal basepay)
    {
    this.name = name;
    this.basepay = basepay;
    }

    // Declared virtual so it can be overridden.
    public virtual decimal CalculatePay()
    {
    return basepay;
    }
    }

    // Derive a new class from Employee.
    public class SalesEmployee : Employee
    {
    // New field that will affect the base pay.
    private decimal salesbonus;

    // The constructor calls the base-class version, and
    // initializes the salesbonus field.
    public SalesEmployee(string name, decimal basepay,
    decimal salesbonus) : base(name, basepay)
    {
    this.salesbonus = salesbonus;
    }

    // Override the CalculatePay method
    // to take bonus into account.
    public override decimal CalculatePay()
    {
    return basepay + salesbonus;
    }
    }

    static void Main()
    {
    // Create some new employees.
    SalesEmployee employee1 = new SalesEmployee("Alice",
    1000, 500);
    Employee employee2 = new Employee("Bob", 1200);

    Console.WriteLine("Employee4 " + employee1.name +
    " earned: " + employee1.CalculatePay());
    Console.WriteLine("Employee4 " + employee2.name +
    " earned: " + employee2.CalculatePay());
    }
    }
    /*
    Output:
    Employee4 Alice earned: 1500
    Employee4 Bob earned: 1200
    */






  3. #3
    کاربر دائمی آواتار فرید نجفلو
    تاریخ عضویت
    بهمن 1390
    محل زندگی
    تبریز
    پست
    1,189

    نقل قول: کلمه کلیدی override برای چیست؟

    سلام
    دوست عزیز override در واقع بازنویسی یا رو نویسی میشه
    اگه کلاس شما از یه کلاس دیگه ارث بری داشته شما می تونید اون رو برای خودتون تغییر (سافرشی) کنید
    معمولا همیشه متد ToString رو برای override مثال می زنن
    یه مثال دیگه که می تونید تست کنید براتون می زنم
    همون طور که می دونید همه فرم ها یه متد به نام Refresh دارن
    حالا اگه شما کد پایین رو تو یه فرم بزارید و در جای دیکه Refresh رو صدا بزنید متوجه می شید که به جای متد اصلی کد شما اجرا میشه

             public override void Refresh()
    {
    MessageBox.Show("this is My Override Code");
    base.Refresh();
    }


    توجه کنید که معمولا وقتی چیزی رو overrid می کنن متد اصلی رو هم صدا می زنن (برای دسترسی به متد اصلی از base استفاده می کنن)

تاپیک های مشابه

  1. معنی کلمه کلیدی Cast چیست؟
    نوشته شده توسط منصور بزرگمهر در بخش VB.NET
    پاسخ: 1
    آخرین پست: دوشنبه 07 مرداد 1392, 15:00 عصر
  2. این error در بانک اطلاعاتی برای چیست؟
    نوشته شده توسط eskimo در بخش PHP
    پاسخ: 1
    آخرین پست: دوشنبه 07 اسفند 1385, 18:05 عصر
  3. کلمه کلیدی Partial در تعریف Class یا Structure چه نقشی دارد؟
    نوشته شده توسط محمد عرب در بخش VB.NET
    پاسخ: 5
    آخرین پست: جمعه 18 فروردین 1385, 00:10 صبح
  4. مشکل در استفاده از کلمه کلیدی WinEvents
    نوشته شده توسط mr_esmaily در بخش VB.NET
    پاسخ: 4
    آخرین پست: سه شنبه 06 مرداد 1383, 11:11 صبح

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •