使用代码比使用单词更容易理解这个问题:
Map<Integer, Parent> objectMap = new HashMap<Integer, Parent>();
Parent myParent;
Child1 myChild1;
Child2 myChild2;
//A lot more myChilds
objectMap.put(1, myChild1);
objectMap.put(2, myChild2);
//Place all the myChilds
myChild1 = new Child1(); //Constructor is expensive, object may not get used
myChild2 = new Child2(); //Constructor is expensive, object may not get used
//Call constructor for all of myChilds
Parent finalObject;
int number = 1; //This can be any number
finalObject = objectMap.get(number);
如您所见,我事先并不知道finalObject会是哪一个类。代码工作没有问题,但这是我的问题:
如何避免同时调用两个构造函数?
由于只使用myChild1或myChild2且构造函数方法非常昂贵,我只想调用实际使用的那个。
像
这样的东西finalObject.callConstructor();
在最后一行
有什么想法吗?
提前致谢。
编辑:我想知道的是如何在不知道类的名称的情况下调用构造函数。检查更新的代码。
答案 0 :(得分:4)
这个怎么样?
#include <stdio.h>
#include <stdlib.h>
char getStaticElement();
char getDynamicElement();
int main() {
char dynamicElement = getDynamicElement();
char staticElement = getStaticElement();
printf("Dynamic Element: %c\n", dynamicElement);
printf("Static Element: %c\n", staticElement);
return 0;
}
char getStaticElement() {
char staticArray [] = {'a','b','c'};
return staticArray[1]; // returns b
}
char getDynamicElement() {
char * dynamicArray = malloc(sizeof(char)*4);
dynamicArray [0] ='a';
dynamicArray [1] ='b';
dynamicArray [2] ='c';
dynamicArray [3] ='\0';
return dynamicArray[1]; // returns b
}
或者,甚至更好,这个?
Parent finalObject;
if (condition) {
finalObject = new Child1();
} else {
finalObject = new Child2();
}
答案 1 :(得分:1)
不要构建两个对象。只构造你需要的对象。
<div id='donor_table'>
<div style='height: 600px; width: 100%; overflow: scroll'>
<table id='donor_tbl' border='5' cellpadding='10'>
<thead>
<th>#</th>
<th>Announcements</th>
<th>Date</th>
<th>Time</th>
<th>Post</th>
</thead>
<tbody>
<?php
include "connection.php";
error_reporting(0);
$searchannouncement = $_POST['searchannouncement'];
$displayAnn = " SELECT annid,announcement,dateee,timeee FROM announcements_tbl WHERE annid LIKE '%" . $searchdonor . "%' OR announcement LIKE '%" . $searchdonor . "%' OR dateee LIKE '%" . $searchdonor . "%' OR timeee LIKE '%" . $searchdonor . "%' order by annid desc";
$annData = mysql_query($displayAnn);
while ($drecords = mysql_fetch_assoc($annData)) {
echo "<tr><td>" . $drecords['annid'] . "</td>";
echo "<td>" . $drecords['announcement'] . "</td>";
echo "<td>" . $drecords['dateee'] . "</td>";
echo "<td>" . $drecords['timeee'] . "</td>";
echo "<td align='center'><input type='button' class='btn-post-announcement button' id=" . $drecords['annid'] . " value='Post'></td>";
};
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
<div id="dlg">
<div>
<script type="text/javascript">
$(function () {
$("#dlg").dialog({
autoOpen: false,
height: 600,
width: 780,
modal: true
});
$(".btn-post-announcement").button();
$(".btn-post-announcement").click(function () {
})
$("#donor_tbl").DataTable();
});
</script>
答案 2 :(得分:0)
您应该使用工厂模式:
public interface Factory<T> {
T create();
}
...
Factory<T> factory;
if (condition) {
factory = FACTORY1;
} else {
factory = FACTORY2;
}
object = factory.create()
答案 3 :(得分:0)
首先检查你的条件,然后在if或else语句中调用构造函数。 如果(条件) finalobject = new myChild1(); 其他 finalobject = new myChild2();