使用.NET时,我遇到了一个非常奇怪的问题XmlSerializer.
采用以下示例类:
public class Order
{
public PaymentCollection Payments { get; set; }
//everything else is serializable (including other collections of non-abstract types)
}
public class PaymentCollection : Collection<Payment>
{
}
public abstract class Payment
{
//abstract methods
}
public class BankPayment : Payment
{
//method implementations
}
Run Code Online (Sandbox Code Playgroud)
AFAIK,有三种不同的方法可以解决InvalidOperationException由序列化程序不知道派生类型引起的问题Payment.
1.添加XmlInclude到Payment类定义:
这是不可能的,因为所有类都包含在我无法控制的外部引用中.
2.在创建XmlSerializer实例期间传递派生类型的类型
不行.
3.定义XmlAttributeOverrides目标属性以覆盖属性的默认序列化(如本SO帖子中所述)
也不起作用(XmlAttributeOverrides初始化如下).
Type bankPayment = typeof(BankPayment);
XmlAttributes attributes = …Run Code Online (Sandbox Code Playgroud) 我一直试图通过使用以下鼠标监听器在屏幕上移动一个未修饰的阶段:
这些事件来自一个矩形.我的想法是移动未装饰的窗口,点击矩形并拖动所有窗口.
@FXML
protected void onRectanglePressed(MouseEvent event) {
X = primaryStage.getX() - event.getScreenX();
Y = primaryStage.getY() - event.getScreenY();
}
@FXML
protected void onRectangleReleased(MouseEvent event) {
primaryStage.setX(event.getScreenX());
primaryStage.setY(event.getScreenY());
}
@FXML
protected void onRectangleDragged(MouseEvent event) {
primaryStage.setX(event.getScreenX() + X);
primaryStage.setY(event.getScreenY() + Y);
}
我用这些事件得到的就是当我按下矩形并开始拖动窗口时,它会移动一点点.但是,当我释放按钮时,窗口移动到矩形所在的位置.
提前致谢.
在处理Java枚举时,我对代码重用的最佳设计模式有疑问.基本上,我想要实现的是能够定义几个模拟静态业务集合(常量集)的枚举,但我也想用最少的编码来共享它们之间的行为.
通过抽象类的类继承来实现这是微不足道的,但是,由于Java枚举无法扩展(它们只能实现接口),这种类型的工作很繁琐,并且涉及很多容易出错的复制/粘贴工作(从枚举中复制代码)枚举).应在所有枚举之间共享的"业务逻辑"示例包括从/转换为字符串,实例和逻辑比较等.
我现在最好的方法是将helper类与业务接口结合使用,但这只能降低代码复杂性(因为所有枚举仍然必须声明并调用辅助类).参见示例(只是为了澄清):
public enum MyEnum {
A, B, C;
// Just about any method fits the description - equals() is a mere example
public boolean equals(MyEnum that) {
ObjectUtils.equals(this, that);
}
}
Run Code Online (Sandbox Code Playgroud)
StackOverflowers如何处理这种"语言功能"?
我正在实现一个流畅的构建器模式,它需要在静态扩展方法中接受可枚举并迭代其内容,同时将一个仿函数应用于可枚举的内容.如(不是实际代码,只是一个例子):
public static IValidator<IEnumerable<T>> Each<T>(
this IValidator<IEnumerable<T>> enumerable,
Func<T, bool> action)
{
foreach (T value in enumerable)
action(value);
return validator;
}
Run Code Online (Sandbox Code Playgroud)
这对于枚举非常有效,但对于继承的类型/接口则失败.让我们说:
IValidator<IEnumerable<Guid>> validator = ...;
IEnumerable<Guid> guids = ...;
validator.Each(guids, guid => guid != Guid.Empty); // ok
IList<Guid> guids = ...;
validator.Each(guids, guid => guid != Guid.Empty); // doesn't compile (see below)
Run Code Online (Sandbox Code Playgroud)
例外是:
IValidator<IList<Guid>>不包含'Each'的定义,也没有扩展方法'each'接受类型的第一个参数IValidator<IList<Guid>>可以找到(你是否缺少using指令或汇编引用?
我的问题是关于继承链IValidator<T>,更具体地说,是它的泛型类型参数T.为什么类型IValidator<IEnumerable<T>>不可分配IValidator<IList<T>>?没有任何情况我可以想到哪个IList<T>不是IEnumerable<T>(给定相同T).
约束泛型参数T : IEnumerable<R>确实有效,但是如果可能的话,这需要两个我想避免的类型参数(T和 …
我正在编写一个SQLServer 2008存储过程,它接受一个付款表,并尝试根据相关表中描述的一组规则(基本上是一组存储桶)分配这些付款.然而,分配(将支付价值放入桶中)正是导致我头痛的原因.
假设表Payments包含要支付的值和表Buckets是关于应该在每个桶中放入多少钱,直到要支付的初始值耗尽(达到0).
使用下面的表作为示例(实际用例有点做作,因为有一些复杂的标准来选择适合每次付款的存储桶):
PaymentId Value BucketId MaxAmount
-------------------------- --------------------------------
1 16.5 1 5.5
2 7.0 2 10
3 8.3
Run Code Online (Sandbox Code Playgroud)
对于付款1:5.5单位(该桶的最大值)应进入桶1,10个单位进入桶2(11.5是桶1的剩余量,但仍超过桶2的最大值)和1个单位(16.5 - 5.5 - 10) )应该放入桶3.重复所有付款.
这很容易在任何命令式语言中实现,甚至可能在带有for/while循环的SQL中实现,但我试图意识到是否有更好的方法(即使它是非可移植的并且特定于SQLServer 2005+).
我已经做了一些研究(主要是递归CTE),但没有真正想到的东西.我确信有很多StackOverflowers和SQL-fu一起回答它们的问题,所以我想把它放在那里看看......
非常感谢你的帮助.
我有一个MM/dd/yy格式的日期.我是从XML文件中获取的.比方说,比方说,就是这样10/03/11.
我在其上执行以下代码:
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("dateStr: " + dateStr);
date = sdf.parse(dateStr);
System.out.println("dateStr After: " + sdf.format(date));
} catch(Exception e) {
System.out.println("Error when formatting date");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
dateStr: 10/03/11 dateStr After: 10/03/0011
我似乎无法得到它10/03/2011.有什么想法吗?
我有一节课:
class Stock : Product
{
}
Run Code Online (Sandbox Code Playgroud)
在那堂课中,我做了一个Equals方法:
public bool Equals(Product p)
{
return (p.Id == this.Id);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.它告诉我Equals:
警告 1'WindowsFormsApplication1.Stock.Equals(WindowsFormsApplication1.Product)'隐藏继承的成员'WindowsFormsApplication1.Product.Equals(WindowsFormsApplication1.Product)'.如果要隐藏,请使用new关键字.
C:\Users\tom\Desktop\uni\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1\Stock.cs 36 21 WindowsFormsApplication1
Run Code Online (Sandbox Code Playgroud)
有谁知道这是为什么?