虚函数如何在C#和Java中工作? [英] How do virtual functions work in C# and Java?

查看:173
本文介绍了虚函数如何在C#和Java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虚拟函数如何在C#和Java中运行?

How do the virtual functions work in C# and Java?

它是否使用与C ++类似的相同vtable和vpointer概念,还是完全不同?

Does it use same vtable and vpointer concept similar to C++ or is it something totally different?

推荐答案

虚拟功能如何在Java中工作?



编码采访者喜欢这个问题。是。虽然Java没有虚拟关键字,但Java有虚函数,你可以编写它们。

How do virtual functions work in Java?

Coding interviewers love this question. Yes. Although Java does NOT have a virtual keyword, Java has virtual functions and you can write them.

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

In object-oriented programming, 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).

询问有关这种特定语言的架构问题需要很好的沟通技巧和深入掌握Java编译器的基本原理,特别是接口,抽象类以及继承如何工作。

Asking an architecture question about a specific language like this requires great communication skills and a deep mastery of underlying principles of the Java compiler, specifically interfaces, abstract classes, and how inheritance works.

引导访问者了解虚函数的具体示例。

Guide the interviewer onto a specific example of a virtual function.

是的,您可以使用接口在Java中编写虚函数。

Java 接口方法都是纯虚拟的,因为它们被设计为覆盖。例如:

Java interface methods are all "pure virtual" because they are designed to be overridden. For example:

interface Bicycle {         //the function applyBrakes() is virtual because
    void applyBrakes();     //functions in interfaces are designed to be 
}                           //overridden.

class ACMEBicycle implements Bicycle {
    public void applyBrakes(){               //Here we implementing applyBrakes()
       System.out.println("Brakes applied"); //function, proving it is virtual.
    }
}

是的,你可以用Java编写虚函数使用抽象类。

Java 抽象类包含隐式虚拟方法,由扩展它的类实现。例如:

Java Abstract classes contain implicitly "virtual" methods, implemented by classes extending it. For Example:

abstract class Dog {                   
    final void bark() {               //bark() is not virtual because it is 
        System.out.println("woof");   //final and if you tried to override it
    }                                 //you would get a compile time error.

    abstract void jump();             //jump() is a virtual function because it
}                                     //is part of an abstract class and isn't
                                      //final.  
class MyDog extends Dog{
    void jump(){
        System.out.println("boing");    //here jump() is being overridden, a 
    }                                   //demonstration that it is virtual.
}
public class Runner {
    public static void main(String[] args) {
        MyDog myDog = new MyDog();       //instantiating myDog
        myDog.jump();                    //calling the overridden function jump()
    }
}

您可以通过使函数 final

You can force a function to NOT be virtual in a generic class by making it final

强制函数在泛型类中不是虚拟函数例如:

For example:

class myJavaFoobarClass {

    final boolean giveMeTrueFunction()   //this Java function is NOT virtual
    {                                    //because final keyword prevents this
        return true;                     //function from being modified in a
    }                                    //subclass.

    boolean isItRainingFunction()   //this Java function IS virtual because
    {                               //without the final keyword, the function
        return false;               //can be overridden in a subclass.
    }
}

这篇关于虚函数如何在C#和Java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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