首页 \ 问答 \ 编译时与运行时多态(或方法签名)的行为(Behaviour of compile-time vs run-time polymorphism (or method signature))

编译时与运行时多态(或方法签名)的行为(Behaviour of compile-time vs run-time polymorphism (or method signature))

我有以下用Java编写的说明性代码。 它显示了不同自行车的introduceYourself()方法的覆盖。

public class Bicycle{
    public void introduceYourself(){
        System.out.println("Hello I am just a bicycle.");
    }
}

public class MountainBike extends Bicycle{
    public void introduceYourself(){
        System.out.println("Hello I am a mountain bike and I love going outdoors.");
    }
}

public class CityBike extends Bicycle{
    public void introduceYourself(){
        System.out.println("My name is city bike and I prefer calm trips.");
    }
}

正如我所期望的那样, 下面的代码为每个运行时对象调用了introduceYourself()方法 ,尽管这些变量被声明为基本的Bicycle类。 如果我要将Bicycle或Bicycle子类型对象添加到数组并在循环上调用该方法,这将非常有用。

public class HelloWorld{

     public static void main(String []args){
        Bicycle b1 = new Bicycle();
        Bicycle b2 = new MountainBike();
        Bicycle b3 = new CityBike();

        b1.introduceYourself(); // Output: Hello I am just a bicycle.       
        b2.introduceYourself(); // Output: Hello I am a mountain bike and I love going outdoors.
        b3.introduceYourself(); // Output: My name is city bike and I prefer calm trips.  
     }
}

但是,我无法理解其他代码的行为。 我有以下类,它们再次显示继承,但具有不同签名的方法(重载):

public class A{
    public int calc (double num){
    return (int)(num + 1);
    }
}

public class B extends A{
    public int calc (long num){
        return (int)(num + 2);
    }
}

public class C extends B{
    public int calc (int num){
        return (num + 3);
    }
}

public class D extends C{
    public int calc (float num){
        return (int)(num + 4);
    }
}

以下代码在main方法中:

public class HelloWorld{
 public static void main(String []args){
    int num1 = 10;
    long num2 = 10;

    A a1 = new D();
    D d1 = new D();

    System.out.println(a1.calc(num1)); // Output: 11
    System.out.println(a1.calc(num2)); // Output: 11

    System.out.println(d1.calc(num1)); // Output: 13
    System.out.println(d1.calc(num2)); // Output: 12
 }

}

为什么a1引用的对象(声明的类型A和运行时类型D )调用A声明的方法而不是D类运行时对象已知的最合适的方法(通过签名)? (另外,我认为有一个自动转换,因为参数类型不一样。)为什么它看起来与Bicycle示例有如此不同? 谢谢。


I have the following illustrative code written in Java. It shows the overriding of the introduceYourself() method for the different bikes.

public class Bicycle{
    public void introduceYourself(){
        System.out.println("Hello I am just a bicycle.");
    }
}

public class MountainBike extends Bicycle{
    public void introduceYourself(){
        System.out.println("Hello I am a mountain bike and I love going outdoors.");
    }
}

public class CityBike extends Bicycle{
    public void introduceYourself(){
        System.out.println("My name is city bike and I prefer calm trips.");
    }
}

Just as I was expecting, the following code calls the introduceYourself() method for each run-time object, although the variables were declared as the base Bicycle class. This would be useful if I were to add Bicycle or Bicycle subtype objects to an array and call the method on a loop.

public class HelloWorld{

     public static void main(String []args){
        Bicycle b1 = new Bicycle();
        Bicycle b2 = new MountainBike();
        Bicycle b3 = new CityBike();

        b1.introduceYourself(); // Output: Hello I am just a bicycle.       
        b2.introduceYourself(); // Output: Hello I am a mountain bike and I love going outdoors.
        b3.introduceYourself(); // Output: My name is city bike and I prefer calm trips.  
     }
}

However, I am having trouble understanding the behaviour of this other code. I have the following classes which, again, show inheritance, but methods with different signatures (overloading):

public class A{
    public int calc (double num){
    return (int)(num + 1);
    }
}

public class B extends A{
    public int calc (long num){
        return (int)(num + 2);
    }
}

public class C extends B{
    public int calc (int num){
        return (num + 3);
    }
}

public class D extends C{
    public int calc (float num){
        return (int)(num + 4);
    }
}

And the following code in the main method:

public class HelloWorld{
 public static void main(String []args){
    int num1 = 10;
    long num2 = 10;

    A a1 = new D();
    D d1 = new D();

    System.out.println(a1.calc(num1)); // Output: 11
    System.out.println(a1.calc(num2)); // Output: 11

    System.out.println(d1.calc(num1)); // Output: 13
    System.out.println(d1.calc(num2)); // Output: 12
 }

}

Why does the object referenced by a1 (with declared type A and run-time type D) call the method declared in A instead of the most-appropiate one (by signature) known by its runtime object of class D? (Also, I suppose there is an automated casting, since the argument type is not the same.) Why does it seem to behave so differently from the Bicycle example? Thanks.


原文:https://stackoverflow.com/questions/36144773
更新时间:2024-02-12 12:02

最满意答案

jQuery Fundamentals对此有一些方便的指导。

像你的样本和最后发生的'append(item1,item2)'的循环是最慢的。

您可能想要创建一个文档片段,将所有内容附加到该片段,然后在完成循环后,将片段附加到正文:

   var frag = document.createDocumentFragment();
   $('abc').each(function() {
      frag.appendChild(/*Stuff you want to append */);
   }
   $("<tr>").appendChild(frag);

或者使用循环来构建一个html字符串并添加如此

 $('<tr>').html(myHtmlString)    

jQuery Fundamentals has some handy guidelines about this.

Loops like your sample and what ends up happening with 'append(item1,item2)' are the slowest.

You might want to create a document fragment, append everything to that and then after the loop is complete, append the fragment to the body:

   var frag = document.createDocumentFragment();
   $('abc').each(function() {
      frag.appendChild(/*Stuff you want to append */);
   }
   $("<tr>").appendChild(frag);

Or use the loop to build a string of html and append like so

 $('<tr>').html(myHtmlString)    

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。