【问题标题】:The use of override and virtual in C# vs. Java method overriding在 C# 中使用 override 和 virtual 与 Java 方法覆盖
【发布时间】:2011-03-18 03:06:14
【问题描述】:

为什么我们需要将一个方法显式定义为 virtual,然后在 C# 中指定 override 来完成方法覆盖,而在 Java 中不使用这两个关键字就可以实现同样的事情。它有什么用途?

【问题讨论】:

标签: c# java inheritance syntax


【解决方案1】:

在java中,不需要添加任何关键字来覆盖方法。但有一些规则适用:

  • 不能将重写的方法声明为比超类方法更私有。
  • 在重写方法中声明的任何异常都必须与超类或该类型的子类引发的异常类型相同。
  • 声明为 final 的方法不能被覆盖。
  • 可以将覆盖方法声明为 final,因为关键字 final 仅表明该方法不能被进一步覆盖。
  • 声明为私有的方法不能被覆盖,因为它们在类外不可见。

font

【讨论】:

    【解决方案2】:

    通过这种方式,您可以更严格地控​​制可覆盖或不可覆盖的内容。这与访问权限相同 - 您是默认授予用户所有权限并删除权限,还是不授予用户然后添加所需的权限。

    【讨论】:

      【解决方案3】:

      函数覆盖意味着“具有相同名称和相同参数的不同方法”。 这里附上一段关于覆盖的小代码。

      我在这里使用“虚拟”关键字作为基类。 如果我们想调用派生类,那么我们必须使用“Override”关键字。

      调用基类:

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

      【讨论】:

        猜你喜欢
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        相关资源
        最近更新 更多