什么是VB的“阴影”或C#“新”关键字相当于Java?(What is the VB “Shadow” or C# “new” keyword Equivalent in Java?)

编程入门 行业动态 更新时间:2024-10-26 09:28:29
什么是VB的“阴影”或C#“新”关键字相当于Java?(What is the VB “Shadow” or C# “new” keyword Equivalent in Java?)

当我想定义非虚拟方法的新实现时,我可以在C#中使用new关键字或在VB中使用shadow关键字。 例如:

C#代码:

public class Animal { public void MyMethod() { //Do some thing } } public class Cat : Animal { public new void MyMethod() { //Do some other thing } }

VB代码:

Public Class Animal Public Sub MyMethod() 'Do some thing End Sub End Class Public Class Cat Inherits Animal Public Shadows Sub MyMethod() 'Do some other thing End Sub End Class

现在,我的问题是:

在Java中,什么是VB Shadow (或C# new )等效关键字?

When I want to define a new implementation of a non virtual method, then I could to use new keyword in C# or shadow keyword in VB. For example:

C# code:

public class Animal { public void MyMethod() { //Do some thing } } public class Cat : Animal { public new void MyMethod() { //Do some other thing } }

VB code:

Public Class Animal Public Sub MyMethod() 'Do some thing End Sub End Class Public Class Cat Inherits Animal Public Shadows Sub MyMethod() 'Do some other thing End Sub End Class

Now, my question is:

What is the VB Shadow (or C# new) keyword equivalent in Java ?

最满意答案

你的问题的主要答案可能是“没有。” 细节:

Java的方法默认是“虚拟的”(在C#术语中); 它们可以在子类中重写。 原始方法不需要特殊关键字。 定义重写时,使用@Override注释是有用的(但不是必需的 ),以便编译器会在你不认为自己是重写时警告你,因此读取代码(和JavaDoc)的人知道你'干嘛。

例如:

class Parent { public void method() { } } class Child extends Parent { @Override public void method() { } }

请注意, Parent method没有特殊关键字。

如果Java方法被标记为final (非虚拟的,C#默认值),则根本无法覆盖它。 在这种情况下,没有相当于C#的new 。 例如:

class Parent { public final void method() { } } class Child extends Parent { @Override // <== Won't compile, you simply can't override public void method() { // <== final methods at all (even if you added // <== "final" to the declaration) } }

The primary answer to your question is probably "There is none." Details:

Java's methods are "virtual" (in C# terminology) by default; they can be overridden in subclasses. No special keyword is required on the original method. When defining the override, it's useful (but not required) to use the @Override annotation so that the compiler will warn you if you're not overriding when you think you are, and so people reading the code (and JavaDoc) know what you're doing.

E.g.:

class Parent { public void method() { } } class Child extends Parent { @Override public void method() { } }

Note that there's no special keyword on method in Parent.

If a Java method is marked final (non-virtual, the C# default), you can't override it at all. There is no equivalent to C#'s new in that context. E.g.:

class Parent { public final void method() { } } class Child extends Parent { @Override // <== Won't compile, you simply can't override public void method() { // <== final methods at all (even if you added // <== "final" to the declaration) } }

更多推荐

本文发布于:2023-08-04 20:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阴影   关键字   VB   Java   Equivalent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!