2011-03-18 4 views
4

메서드를 가상으로 명시 적으로 정의한 다음 메서드 오버라이드를 수행하기 위해 C#에서 오버라이드를 지정해야하는 이유는 무엇입니까? Java의 키워드. 어떤 목적을 위해 봉사합니까?C#과 Java 메서드 오버라이드 오버라이드 및 가상의 사용

+0

가능한 중복 [C 번호는 기본적으로 가상이 아닌 등의 방법을 구현하는 이유는 무엇입니까? (HTTP ://stackoverflow.com/questions/814934/why-c-implements-methods-as-non-virtual-by-default) –

답변

5

자바에서는 메소드를 재정의하기 위해 키워드를 추가 할 필요가 없습니다. 그러나 몇 가지 규칙이 적용됩니다 :

  • 메소드 오버라이드는 수퍼 클래스 메소드보다 더 비공식적으로 선언 할 수 없습니다.
  • 오버라이드 메소드에서 선언 된 예외는 수퍼 클래스가 던진 예외와 동일한 유형이거나 해당 유형의 서브 클래스 여야합니다.
  • final로 선언 된 메서드는 재정의 할 수 없습니다.
  • 최종 메서드 키워드는이 메서드를 더 이상 재정의 할 수 없다는 것을 나타내는 경우에만 재정의 메서드를 final 메서드로 선언 할 수 있습니다.
  • private로 선언 된 메서드는 클래스 외부에서 볼 수 없으므로 재정의 할 수 없습니다.

font

3

이 방법을 사용하면 재정의 할 수있는 항목을 제어 할 수 있습니다. 액세스 권한과 동일합니다. 기본적으로 사용자에게 모든 권한을 부여하고 권한을 제거합니까? 아니면 아무 것도 지정하지 않고 필요한 것을 추가합니까?

0

함수의 오버라이드는 "같은 인수와 같은 이름을 가진 다른 방법"을 의미합니다. 여기에서는 재정의에 대한 간단한 코드를 첨부합니다.

여기서는 Base 클래스에 "가상"키워드를 사용하고 있습니다. 파생 클래스를 호출하려면 "재정의"키워드를 사용해야합니다.

이 기본 클래스를 호출하려면 :

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