编译时与运行时多态(或方法签名)的行为(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 baseBicycle
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 typeA
and run-time typeD
) call the method declared inA
instead of the most-appropiate one (by signature) known by its runtime object of classD
? (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
最满意答案
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)
相关问答
更多-
如何从Angular 2中的指令追加动态DOM元素?(How to append dynamic DOM elements from a directive in Angular 2?)[2022-06-09]
使用Angular提供的Renderer来操作DOM: import { DOCUMENT } from '@angular/common'; export class MyDirective implements OnInit { constructor(private elementRef: ElementRef, renderer: Renderer2, @Inject(DOCUMENT) private document) { } ngOnInit() { const child ... -
如何将多个DOM元素附加到使用reactjs创建的元素?(How to append multiple DOM elements to an element created with reactjs?)[2023-01-13]
您仍在以强制性的方式思考您的代码。 React基于声明性编程范例。 首先,将整个应用程序视为React组件。 var App = React.createClass({ render: function() { return (foo); } }) React.render(, document.body); 现在,让我们首先渲染一些段落: var App = React.createClass({ render: func ... -
通过jQuery .append()添加DOM元素(即Angular指令)?(How to add DOM Elements (Angular directives) via jQuery's .append()?)[2023-05-16]
正确的方法是使用: $ compile ,如果你的指令返回: directive definition object (这是btw。推荐的方法),然后可以调用它的link函数(例如注入scope )。 $('body').append($compile("")(scope)); scope.$apply(); The right way to go is to use: $compile and in case your directive returns ... -
jQuery Fundamentals对此有一些方便的指导。 像你的样本和最后发生的'append(item1,item2)'的循环是最慢的。 您可能想要创建一个文档片段,将所有内容附加到该片段,然后在完成循环后,将片段附加到正文: var frag = document.createDocumentFragment(); $('abc').each(function() { frag.appendChild(/*Stuff you want to append */); } ...
-
如何将DOM元素附加到JavaScript中的空元素中?(How to append DOM Element into an empty element in JavaScript?)[2023-06-14]
这是文档片段的用途。 您可以通过document.createDocumentFragment()创建一个新的片段,并像使用单个元素一样使用它。 That's what document fragments are for. You can create a new fragment via document.createDocumentFragment() and use it as if it were a single element. -
追加到Dom在IE中花费的时间比Firefox或任何其他浏览器多(Append in Dom taking more time in IE than Firefox or any other browser)[2023-05-29]
插入大量数据可能非常慢。 此页面详细介绍了插入内容的各种方法 。 我不太了解实施这些我害怕但似乎有些方法在IE7中比在Firefox中慢10倍。 Inserting large amounts of data can be very slow. This page details various methods of inserting content. I don't know much implementing these I'm afraid but it seems that some method ... -
如何创建jQuery元素的集合并将它们附加到DOM?(How to create a collection of jQuery elements and append them to the DOM?)[2021-11-25]
能够使用以下示例代码附加此内容: var collection = []; var elem; for (var i = 0; i < 10; i+=1) { elem = $("").attr("class","foo-"+i).text("hello"); collection.push(elem); } console.log(collection); // the "context" error //$(collection).appendTo(document.b ... -
如何向DOM添加元素(How to add Elements to DOM)[2022-02-19]
这是一种在一行中完成的方法: function appendElement (elemNode, textContent, parent) { parent.appendChild(document.createElement(elemNode)).textContent = textContent; } appendElement("b","lorem", document.getElementById("container")); div { background-color: aqu ... -
在将DOM元素添加到文档之前操纵DOM元素[复制](Manipulate DOM elements before adding them to the document [duplicate])[2023-08-31]
你看过DOM DocumentFragments了吗? Have you looked at DOM DocumentFragments? -
document.createElement返回一个Element对象。 .append是一种实验方法,请在此处查找信息: https : //developer.mozilla.org/en-US/docs/Web/API/ParentNode/append document.createElement returns an Element object. .append is an experimental method, find info here:https://developer.mozilla ...