我有对象XML序列化消息进入一个名为MessageRouter的类.XML包含它从中序列化的类型名称,我需要能够根据直到运行时才知道的类型调用不同的委托方法.我在仿制药方面并不是非常强大,所以希望这对某人有意义......
我希望MessageRouter提供一个RegisterDelegateForType方法,如下所示:
myMessageRouter.RegisterDelegateForType(new Action<MySerializableType>(myActionHandler));
Run Code Online (Sandbox Code Playgroud)
然后将类型或类型的字符串表示形式存储在字典中,如下所示:
Dictionary<Type, Action<T>> registeredDelegates;
Run Code Online (Sandbox Code Playgroud)
这样,我可以执行类似下面的伪代码,调用类型的已分配委托并传递反序列化的对象:
Type xmlSerializedType = TypeFromXmlString(incomingXml);
object deserializedObject = DeserializeObjectFromXml(xmlSerializedType, incomingXml);
// then invoke the action and pass in the deserialized object
registeredDelegates[xmlSerializedType](deserializedObject);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
Type键作为键和泛型Action<T>作为值的Dictionary ,并使用RegisterDelegateForType方法填充字典?通过尝试在昨天的课堂上向朋友解释Monty Hall问题,我们最终用Python编写代码来证明如果你总是交换,你将赢得2/3次.我们想出了这个:
import random as r
#iterations = int(raw_input("How many iterations? >> "))
iterations = 100000
doors = ["goat", "goat", "car"]
wins = 0.0
losses = 0.0
for i in range(iterations):
n = r.randrange(0,3)
choice = doors[n]
if n == 0:
#print "You chose door 1."
#print "Monty opens door 2. There is a goat behind this door."
#print "You swapped to door 3."
wins += 1
#print "You won a " + doors[2] + "\n"
elif n …Run Code Online (Sandbox Code Playgroud) 当用户到达某个页面向下滚动网页时,是否可以更改滚动条位置?例如,一旦你到达页面的一半,滚动条就会自动移回到顶部.
假设我有一个数字/向量的数字,如1,3,7,9然后我需要随机猜测这个列表中的数字.在Java中使用Random类似乎不可能这样做.任何人都可以帮助我指出做这种事情的方法.我必须更改用于生成随机数的数字列表.我正在尝试实施一种战略,作为任务自动发挥战舰游戏.请帮我这样做?
这就是我想做的事情.我有一些内容,我写的是一个观点.此内容具有与文档相关的图像引用.例如,如果我正在查看以下网址:
http://localhost/article/8AB98/
Run Code Online (Sandbox Code Playgroud)
内容可能具有以下形式的图像:
<img src="myimage.png" />
Run Code Online (Sandbox Code Playgroud)
这显然会导致浏览器在以下URL查询图像:
http://localhost/article/8AB98/myimage.png
Run Code Online (Sandbox Code Playgroud)
但是,由于mvc路由,将无法找到此图像.您是否知道一种简单的方法可以使该URL将正确的图像返回给浏览器?
请注意:实际上重要的是标记保持原始状态不变...这意味着以某种方式重写图像网址,以便它们指向当前视图URL之外的另一个文件夹,遗憾的是不可能.
谢谢!!
基于Java中的递归算法,我很难理解以下代码.我不明白,什么是不同的价值x,y并且当他们彼此呼唤时有什么?我试图通过System.out.print()在代码中调用来获得正确的值,但仍然没有得到帮助.
public class RecursionExample
{
private static int[][] arr={
{3},
{7, 4},
{2, 4, 6},
{8 ,5, 9, 3}
};
public static int maxSum(int[][] graph, int x, int y, int sum) {
if (x == 3)
{
return sum+graph[x][y];
}
int max= Math.max(maxSum(graph, x+1, y, sum), maxSum(graph, x+1, y+1, sum));
sum += graph[x][y];
return sum+max;
}
public static void main(String[] ar)
{
System.out.println(maxSum(arr,0,0,0));
}
}
Run Code Online (Sandbox Code Playgroud)
我不是编程大师,我正在努力学习Java.任何帮助表示赞赏.
我从以前的帖子中得到一些想法,他们正在谈论为每个$ array [$ i]创建一个哈希值,然后比较哈希来获得唯一的数组,但我不知道我能做些什么.
我的示例数组数据:
$arr[] = array(0,1,2,3);
$arr[] = array(4,5,2,1);
$arr[] = array(0,0,0,0);
$arr[] = array(0,1,2,3);
Run Code Online (Sandbox Code Playgroud)
我期待回归:
$arr[] = array(0,1,2,3);
$arr[] = array(4,5,2,1);
$arr[] = array(0,0,0,0);
Run Code Online (Sandbox Code Playgroud)
任何人都可以为此发布功能吗?
非常感谢!
如果我有一个对象构造函数,如:
function cat(color, sex){
this.color = color;
this.sex = sex;
}
Run Code Online (Sandbox Code Playgroud)
我做了一些猫:
var fluffball = new cat("blue","male");
var shiznitz = new cat("red","male");
var slothersburger = new cat("green","female");
Run Code Online (Sandbox Code Playgroud)
是否有可能遍历我宣布的所有猫?就像是:
var current_cat;
for(current_cat in document.cat){
alert(current_cat.color);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.人们通常将所有猫对象存储在一个数组中吗?或者制作另一个包含各个猫的数组的对象:
function all_cats(){
this.the_cats = new Array();
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何提示!
我是C新手,我来自C#.我一直在学习内存管理和malloc()功能.我也遇到过这段代码:
char *a_persons_name = malloc(sizeof(char) + 2);
Run Code Online (Sandbox Code Playgroud)
我不明白的是这分配了多少空间a_persons_name.是分配2个字符(例如AB)还是别的什么?
我也知道你有时可以"幸运" malloc并使用未分配的空间(这可能导致数据损坏和seg故障).那么我怎么知道我分配了多少空间以及我需要多少空间?