Java 和 C# 处理方法绑定方式的显着差异背后的解释是什么?

发布于 2024-10-12 03:19:30 字数 172 浏览 10 评论 0原文

在Java中,对象的方法调用仅取决于对象类型,而不取决于引用类型。

在 C# 中,引用类型在根据方法是否为虚方法来确定是调用父类的方法还是调用子类的方法方面发挥着重要作用。

  1. 上述说法准确吗?
  2. 为什么Java要限制这一点,并且它不会剥夺程序员的一些权力吗?

In Java, the method call of an object depends solely on the object type, and not the reference type.

In C#, the reference type plays an important role in determining whether the method from the parent class is called or the method from the child class is called, based on whether the method is virtual or not.

  1. Is the above statement accurate?
  2. Why does Java restrict this, and does it not take away some power from the programmer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2024-10-19 03:19:30

你的定义不正确。在 C# 中,它不仅仅基于方法是否是虚拟的。即使虚拟方法也可能根据引用类型而表现不同。请参阅下面的示例并查看关键字new

Java 语言很简单。它试图保持简单。是的,它确实剥夺了您所写的“一些权力”,但作为补偿,您可以获得易于理解的干净代码。

C# 语言有更多功能,但有时您需要努力理解所有功能。这个关于同名方法继承的特定示例是一个很好的例子。我们可以用 C# 创建更多有趣的东西......但没有人会理解。

通常我们无法选择语言,因为我们受到系统平台、库和其他技术的限制。

class A {
  public virtual void M1() {
    Console.WriteLine("A.M1()");
  }
}

class B : A {
  public new virtual void M1() {
    Console.WriteLine("B.M1()");
  }
}

class Program {
  static void Main(string[] args) {
    B b = new B();
    b.M1();
    A a = b;
    a.M1();
  }
}

在 C# 中,尽管有相同的对象,但会得到两个不同的结果。

Your definition is incorrect. In C# it is not based just on whether method is virtual. Even virtual methods can behave differently based on type of reference. See example below and watch at keyword new.

Java language is simple. It tries to stay simple. Yes, it does take away "some power" as you wrote, but in compensation you get clean code easy to understand.

C# language has more features, but sometimes you need to try hard to understand all of them. This particular example on inheritance of methods with same name is a nice example. We can create more interesting things in C#.... which nobody will understand.

Usually we cannot choose language because we are constrained by system platform, libraries and other technologies.

class A {
  public virtual void M1() {
    Console.WriteLine("A.M1()");
  }
}

class B : A {
  public new virtual void M1() {
    Console.WriteLine("B.M1()");
  }
}

class Program {
  static void Main(string[] args) {
    B b = new B();
    b.M1();
    A a = b;
    a.M1();
  }
}

In C# you get two different results, although you have the same object.

独自←快乐 2024-10-19 03:19:30

在 Java 中,所有非静态方法都是虚拟的,但是对于静态方法来说,引用的类型才是最重要的。因此,使用非虚拟静态方法的引用被认为是不好的做法,而最好使用类。

例如,您可以编写

Integer i = null;
int j = i.parseInt("1");

This 不会引发异常,因为不仅 i 的实际类型被忽略,而且它的值也被忽略。

您希望调用父级方法而不是子级方法的情况极为罕见。子类的开发人员出于某种原因重写了该方法。必须想象如果任何被重写方法的组合都可以被重写,你的类将如何表现,这是一个令人头痛的问题,我宁愿不这样做。

In Java all non-static methods are virtual, however for the static methods, the type of the reference is all that matters. For this reason it is considered bad practice to use a reference for a non-virtual static method and using the class instead is preferred.

e.g. you can write

Integer i = null;
int j = i.parseInt("1");

This won't throw an exception because not only is actual type of i ignored but so is its value.

It is extremely rare that you would want to call a parent's method instead of the child's method. The developer of the child class overrode the method for a reason. Having to imagine how your class would behave if any combination of overridden methods could be un-overriden is a headache I would rather do without.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文