我JAXB
为了将一些对象编组/解组到XML文件中以用于我要实现的小型服务。现在,我的XML模式(.xsd文件)包括一些unique
约束:
<!--....-->
<xs:unique name="uniqueValueID">
<xs:selector xpath="entry/value"/>
<xs:field xpath="id"/>
</xs:unique>
<!--....-->
Run Code Online (Sandbox Code Playgroud)
我已经将XML模式加载到marshaller对象中:
try {
//....
//Set schema onto the marshaller object
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(xsdFileName));
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setSchema(schema);
//Apply marshalling here
jaxbMarshaller.marshal(myObjectToMarshall, new File(xmlFileNName));
} catch (JAXBException | SAXException ex) {
//Exception handling code here....
}
Run Code Online (Sandbox Code Playgroud)
架构有效时,将正常更新目标文件,但验证失败时,将清空文件或包含不完整的数据。
我猜测问题是封送程序打开了文件流,但是当验证失败时,它无法正确处理这种情况。有没有一种方法可以正确处理此问题,以便在验证失败时不对XML文件进行任何写入操作?
在C#中,如果我有
public class BaseClass
{
//BaseClass implementation
}
public class Derived : BaseClass
{
//Derived implementation
}
public class AnotherClass
{
AnotherClass(BaseClass baseClass)
{
//Some code
}
AnotherClass(Derived derived) : this(derived as BaseClass)
{
//Some more code
}
}
Run Code Online (Sandbox Code Playgroud)
然后做:
BaseClass baseVariable = new Derived();
AnotherClass anotherVariable = new AnotherClass(baseVariable);
Run Code Online (Sandbox Code Playgroud)
这将导致早期绑定,调用该AnotherClass(BaseClass)
方法.
相反,如果我使用dynamic
关键字转换它- 或者使用动态实例化变量然后将其作为构造函数参数传递,AnotherClass(Derived)
则将调用它:
BaseClass baseVariable = new Derived();
//This will instantiate it using the AnotherClass(Derived)
AnotherClass anotherVariable = new AnotherClass(baseVariable as dynamic);
Run Code Online (Sandbox Code Playgroud)
方法是否在C#中进行早期绑定(在编译时评估)?这意味着,有没有其他方式或技巧 确定对其他类构造函数的主要派生调用 …
我正在与Unity进行RTS游戏.我的游戏中有很多类型的资源,例如树,农场.每个资源都是一个GameObject,并拥有自己的主脚本来控制它.
防爆.我想要收获一棵树,我称之为.
gameObject.GetComponent<Tree>().Harvest();
Run Code Online (Sandbox Code Playgroud)
如果我想收获农场,我会调用相同的脚本,但将"Tree"更改为"Farm",这很好,但代码将被复制.所以我通过使用这样的泛型方法来抽象它.
void Harvest<T>(){
gameObject.GetComponent<T>().Harvest();
}
Run Code Online (Sandbox Code Playgroud)
但是C#编译器不允许我这样做.我想知道是否可以定义使用泛型方法的泛型方法?如果没有,有没有办法像这样抽象我的代码?谢谢.
错误信息:
'T'不包含'Harvest'的定义,并且没有扩展方法'Harvest'可以找到接受类型'T'的第一个参数(你是否缺少using指令或汇编引用?)[Assembly-CSharp]
我想创建一个接口,除了其他方法签名之外,它将具有此类型的签名:
Set<Instruction> parse(String rawData);
Run Code Online (Sandbox Code Playgroud)
在实现接口的类中,我想做一个实现:
Set<Instruction> parse(String rawData){
//Do work.
//return an object of type HashSet<DerivedInstruction>.
}
Run Code Online (Sandbox Code Playgroud)
其中DerivedInstruction
扩展了Instruction
抽象类.(指令也可以是接口,或者).
我的观点不在于Collection类型(我知道HashSet实现Set),而是在泛型类型上.通过在其搜索,我发现,无论Set<Instruction>
和HashSet<SpecificInstruction>
扩展Object
类型,并通过继承是不相关的(至少不是直接).因此,我无法HashSet<SpecificInstruction>
对返回类型进行预测.关于如何做到这一点的任何想法?谢谢.
我有很多数组:
Array ( [0] => A-I-only )
Array ( [0] => B-III-only )
Array ( [0] => C-I-and-II-only )
Array ( [0] => D-II-and-III-only )
Array ( [0] => E-I,-II,-III )
Run Code Online (Sandbox Code Playgroud)
我想将每个数组的第一行放在一个数组中,如下所示:
Array( [0] => A-I-only [1] =>B-III-only [2] => C-I-and-II-only [3] => D-II-and-III-only [4] => E-I,-II,-III )
Run Code Online (Sandbox Code Playgroud)
有办法吗?
我有一个带有以下字符串的文本文件:
I want to know. bye bye. I found you. I hate you. I hear you.
Run Code Online (Sandbox Code Playgroud)
我想要做的是搜索文件中的目标句子.这是我使用的代码:
public String lookFor(String target, File targetDestination) throws FileNotFoundException {
Scanner scan = new Scanner(targetDestination);
scan.useDelimiter("\\. ");
while (scan.hasNext()) {
if (scan.next().compareTo(target) == 0)
return target;
}
return "Sorry,(" + target + ") cannot be found!";
}
Run Code Online (Sandbox Code Playgroud)
当我试图寻找任何句子时,代码工作正常,例如:"我讨厌你"它返回"我讨厌你",但是当我试图返回最后一句"我听到你"它说它没有找到,直到我添加一个点"我听到你." 然后它返回它.
谁能解释一下究竟发生了什么?我觉得这是分隔符,但我对正则表达式知之甚少.
java ×3
c# ×2
generics ×2
arrays ×1
collections ×1
delimiter ×1
inheritance ×1
jaxb ×1
marshalling ×1
php ×1
polymorphism ×1
refactoring ×1
regex ×1
xml ×1
xsd ×1