java中的多态性

发布于 2024-08-31 11:12:00 字数 32 浏览 3 评论 0原文

方法重载是运行时多态性还是编译时多态性的一个例子?

Is method overloading is an example of runtime polymorphism or compile time polymorphism?

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

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

发布评论

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

评论(4

星軌x 2024-09-07 11:12:00

Java 中没有运算符重载。

方法重写可实现运行时多态性;方法重载可以实现编译时多态性。

您可以在教科书中阅读更多相关内容。

另请参阅

There is no operator overloading in Java.

Method overriding enables run-time polymorphism; method overloading enables compile-time polymorphism.

You can read more about this in your textbook.

See also

酷到爆炸 2024-09-07 11:12:00

方法重载和多态是两个完全不同的概念。多态性与继承相关,并确定从类层次结构的哪个级别调用该方法。方法重载是在同一位置创建两个具有相同名称且采用不同参数的方法。例如:

class A{
    public void doSomething(){
         System.out.println("A method");
    }

    //method overloaded with different arguments-- no polymorphism 
    //or inheritance involved!

    public void doSomething(int i){
         System.out.println("A method with argument");
    }
}

class B extends A{

    //method polymorphically overridden by subclass. 

    public void doSomething(){
         System.out.println("B method");
    }
}

//static type is A, dynamic type is B.
A a = new B();
a.doSomething(); //prints "B method" because polymorphism looks up dynamic type 
                 //for method behavior

Method overloading and polymorphism are two completely different concepts. Polymorphism relates to inheritance and determines from which level of the class hierarchy the method is called. Method overloading is creating two methods in the same location with the same name that take different arguments. For instance:

class A{
    public void doSomething(){
         System.out.println("A method");
    }

    //method overloaded with different arguments-- no polymorphism 
    //or inheritance involved!

    public void doSomething(int i){
         System.out.println("A method with argument");
    }
}

class B extends A{

    //method polymorphically overridden by subclass. 

    public void doSomething(){
         System.out.println("B method");
    }
}

//static type is A, dynamic type is B.
A a = new B();
a.doSomething(); //prints "B method" because polymorphism looks up dynamic type 
                 //for method behavior
撩心不撩汉 2024-09-07 11:12:00

方法重载是编译时绑定或静态绑定的示例
方法重载会导致多态方法,一个方法可以被定义多次,具有相同的名称和不同数量的方法参数或参数类型(java允许这样做),当发生这种情况时,方法调用和实际定义之间的链接就被解决了在编译时,因此重载称为编译时绑定或静态绑定。 OO程序语言的特点称之为编译时多态性。

Method overloading is example for compile time binding or static binding
method overloading results in polymorphic methods, a method can be defined more than one time having the same name and varying number either method parameters or parameter types (java allows to do this) , when this happens the linkage between method call and actual definition is resolved at compile time thus overloading is called compile time binding or static binding. OO Program language features call it has compile time Polymorphism.

抽个烟儿 2024-09-07 11:12:00

方法重载是编译时绑定的示例。编译器只在编译时决定需要调用哪个方法,所以是编译时多态或者可以说静态绑定,但是方法重写是运行时多态,bcz在运行时,根据对象引用,方法调用被解析。

Method overloading is example of complie time binding. Compiler resolves which method needs to be called at compile time only, So it is compile time polymorphism or you can say static binding, But Method overriding is Runtime polymorphism, bcz at run time , depending on the object reference , method call is resolved.

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