Java继承与C#继承 [英] Java inheritance vs. C# inheritance

查看:227
本文介绍了Java继承与C#继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说Java有这些层次的类:

Let's say Java has these hierarchical classes:

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

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

This is the (blind) duplication of the same code in 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();
    }
}

当我执行Java代码,我得到了 CCC ,而C#返回 BCC

When I execute the Java code, I got C-C-C whereas C# returns B-C-C.

要我丙的#结果令更有意义,作为参考b调用它自己的方法。

To me C#'s result makes more sense, as reference B invokes its own method.


  • 什么是Java的设计者决定打印 CCC背后的逻辑而不是 BCC ?我的意思是,为什么参照B使用在C重写的方法?什么是这种方法的优势在哪里?

  • 如何更改Java代码来打印出 B-C-C 就像C#呢?我的意思是,我怎么能教的java调用它采用了精确的参考值的方法?

  • 我怎样才能改变C#代码打印出来 CCC ?我的意思是,我怎么能教C#调用覆盖方法?

  • What's the logic behind the Java designer's decision to print C-C-C instead of B-C-C? I mean, why reference B uses the overriding method in C? What's the advantage of this approach?
  • 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?
  • How can I change C# code to print out C-C-C? I mean, how can I teach C# to invoke the overriding method?

推荐答案

这对虚函数定义:

一个虚函数或虚方法是一个函数或方法,其行为可以继承类内的函数具有相同签名的覆盖。这个概念是面向对象编程(OOP)的多态性部分的一个非常重要的组成部分。

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).

在C#中,你应该申报方法以虚拟被重写,如图MSDN:

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

  • 10.5.3 Virtual methods (it's from VS 2003, but it's up to date)
  • C# Virtual

由于在 M 方法不是虚拟的,它会执行 BM()即使 b 变量实际上是一个 D 实例。

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

在Java中,每一个方法是通过默认虚拟,所以,当你覆盖方法(即使没有 @覆盖注释)的行为 BM()将在 DM()继承了 CM()法的行为。

In Java, every 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.

如何更改Java代码打印出来BCC就像C#呢?我的意思是,我怎么能教的java调用它采用了精确的参考值的方法?

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?

您根本无法做到这一点的Java的。在 C中的 M 类将重写 M 方法在 B 。添加最后修改为 B#m的将只是使 C 或其他 b 儿童不能覆盖 M()方法。

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.

我怎样才能改变C#代码打印出来CCC?我的意思是,我怎么能教C#调用覆盖方法?

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

更改 M 方法 B 虚拟 C 类:

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

这篇关于Java继承与C#继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆