簡體   English   中英

在C#與Java方法覆蓋中使用override和virtual

[英]The use of override and virtual in C# vs. Java method overriding

為什么我們需要將方法顯式定義為虛擬,然后在C#中指定override以完成方法重寫,而在Java中不使用這兩個關鍵字時實現相同的操作。 它有什么用途?

在java中,沒有必要添加任何關鍵字來覆蓋方法。 但有些規則適用:

  • 方法覆蓋不能被聲明為超類方法更私有。
  • 在重寫方法中聲明的任何異常必須與超類或該類型的子類拋出的異常類型相同。
  • 聲明為final的方法不能被覆蓋。
  • 可以將覆蓋方法聲明為final,因為關鍵字final僅表示此方法無法進一步覆蓋。
  • 聲明為private的方法無法覆蓋,因為它們在類外部不可見。

字形

這樣你就可以更嚴格地控​​制什么是可重寫的。 它與訪問權限相同 - 您是默認情況下為用戶授予所有權限並刪除權限,還是不提供任何權限,然后添加所需內容。

函數重寫意味着“具有相同名稱且具有相同參數的不同方法”。 在這里,我附上一個關於覆蓋的小代碼。

這里我使用基類的“虛擬”關鍵字。 如果我們想要調用派生類,那么我們必須使用“覆蓋”關鍵字。

要調用基類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Function_Overriding
{
    public class Program
    {
        public virtual void display()
        {
            Console.WriteLine("This is Function Overriding");
        }
        public virtual void rnreddy()
        {
            Console.WriteLine("This is Possible because of RN Reddy");
        }

        static void Main(string[] args)
        {
            Program dc = new Program();
            dc.display();
            dc.rnreddy();
            Console.ReadLine();
        }
    }
}

調用派生類

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
*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM