Java继承与C#继承

pro*_*eek 14 c# java inheritance hierarchy

假设Java有这些分层类:

class A 
{
}
class B extends A
{
    public void m()
    {
        System.out.println("B\n");
    }
}
class C extends B
{
    public void m()
    {
        System.out.println("C\n");
    }
}
class D extends C
{
    public static void main(String[] args)
    {
        A a = new D();
        // a.m(); // doesn't work
        B b = new D();
        b.m();
        C c = new D();
        c.m();
        D d = new D();
        d.m();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是C#中相同代码的(盲目)重复:

using System;
class A 
{   
}
class B : A
{
    public void M()
    {
        Console.WriteLine("B");
    }
}
class C : B
{
    public void M() // I need to use public new void M() to avoid the warning
    {
        Console.WriteLine("C");
    }
}
class D : C
{
    public static void Main(String[] args)
    {
        A a = new D();
        // a.M(); // doesn't work       
        B b = new D();
        b.M();
        C c = new D();
        c.M();
        D d = new D();
        d.M();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我执行Java代码时,我得到了C-C-CC#返回B-C-C.

对我来说,C#的结果更有意义,因为引用B调用它自己的方法.

  • Java设计师决定打印的背后的逻辑C-C-CB-C-C什么?我的意思是,为什么参考B使用C中的重写方法?这种方法有什么优势?
  • 如何B-C-C像C#一样更改Java代码以进行打印?我的意思是,我如何教java调用它使用的确切参考方法?
  • 如何更改C#代码以进行打印C-C-C?我的意思是,我如何教C#调用重写方法?

Lui*_*oza 12

It's for the virtual function definition:

a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).

In C#,you should declare the method as virtual in order to be overriden, as shown in MSDN:

Since the M method is not virtual, it will execute b.M() even if b variable is actually a D instance.

In Java, every non-static method is virtual by default, so you when you override a method (even without the @Override annotation) the behavior of the b.M() will be the d.M() that inherits the c.M() method behavior.

How can I change Java code to print out B-C-C just like C# does? I mean, how can I teach java to invoke the method of the exact reference it uses?

You simply can't do this in Java. The M method in C class would override the M method in B. Adding the final modifier to B#M will just make that C or other B children can't override the M() method.

How can I change C# code to print out C-C-C? I mean, how can I teach C# to invoke the overriding method?

Change the M method in B class to virtual and override it in C class:

class B : A
{
    public virtual void M()
    {
        Console.WriteLine("B");
    }
}
class C : B
{
    public override void M() // I need to use public new void M() to avoid the warning
    {
        Console.WriteLine("C");
    }
}
Run Code Online (Sandbox Code Playgroud)


tuk*_*aef 7

  1. 在java中,默认情况下所有方法都是虚拟的.派生类中的方法会覆盖base的方法.在C#中他们不是.

  2. 好像你不能这样做.但您可以通过将其声明为覆盖来阻止派生类重写此方法final.

  3. 使用virtual基类中的关键字和override派生中的关键字声明此方法.

  • 你根本无法将Bm()声明为final,因为它会阻止Cm()覆盖它. (3认同)