简体   繁体   English

在C#与Java方法覆盖中使用override和virtual

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

Why do we need to explicitly define a method as virtual and then also specify override in C# to accomplish method overriding whereas the same thing is achieved without using both of these in keywords in Java. 为什么我们需要将方法显式定义为虚拟,然后在C#中指定override以完成方法重写,而在Java中不使用这两个关键字时实现相同的操作。 What purpose does it serve? 它有什么用途?

in java, there's no need to add any keyword to override a method. 在java中,没有必要添加任何关键字来覆盖方法。 But some rules apply: 但有些规则适用:

  • Methods overriding cannot be declared more private than the super class method. 方法覆盖不能被声明为超类方法更私有。
  • Any exceptions declared in overriding method must be of the same type as those thrown by the super class, or a subclass of that type. 在重写方法中声明的任何异常必须与超类或该类型的子类抛出的异常类型相同。
  • Methods declared as final cannot be overridden. 声明为final的方法不能被覆盖。
  • An overriding method can be declared as final as the keyword final only suggests that this method cannot be further overridden. 可以将覆盖方法声明为final,因为关键字final仅表示此方法无法进一步覆盖。
  • Methods declared as private cannot be overridden as they are not visible outside the class. 声明为private的方法无法覆盖,因为它们在类外部不可见。

font 字形

This way you have tighter control over what's overrideable or not. 这样你就可以更严格地控​​制什么是可重写的。 It's the same as in access permissions - do you give a user all rights by default and remove permissions, or do you give none and then add what's required. 它与访问权限相同 - 您是默认情况下为用户授予所有权限并删除权限,还是不提供任何权限,然后添加所需内容。

Function Overriding Means "Different Methods with the Same Name with the same arguments". 函数重写意味着“具有相同名称且具有相同参数的不同方法”。 Here i attach a small code about overriding. 在这里,我附上一个关于覆盖的小代码。

Here i am using "Virtual" Keyword for Base class. 这里我使用基类的“虚拟”关键字。 If we want to invoke derived class then we have to use "Override" Keyword. 如果我们想要调用派生类,那么我们必须使用“覆盖”关键字。

To Invoke Base Class: 要调用基类:

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

To Invoke Derived Class 调用派生类

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