Java—当您只有引用类型时,实例化X类型的对象
我快速浏览了一下建议的“相关问题”,但找不到与我所问问题直接相关的问题。即使有,我还是很感激你对最好的方法的意见 首先是一些上下文。Java—当您只有引用类型时,实例化X类型的对象,java,interface,copy,types,Java,Interface,Copy,Types,我快速浏览了一下建议的“相关问题”,但找不到与我所问问题直接相关的问题。即使有,我还是很感激你对最好的方法的意见 首先是一些上下文。 我正在扩展Java应用程序,我有以下接口和类: public interface Mapper { public void foo(); } public class SomeGrammar implements Mapper { public SomeGrammar() {} public SomeGrammar(SomeGrammar toC
我正在扩展Java应用程序,我有以下接口和类:
public interface Mapper {
public void foo();
}
public class SomeGrammar implements Mapper {
public SomeGrammar() {}
public SomeGrammar(SomeGrammar toCopy) {
//Copy specific fields
}
public void foo() {}
}
我使用的应用程序只考虑了一种语法类型,SomeGrammar
,因此,SomeGrammar
类贯穿整个应用程序
SomeGrammar
通常使用复制构造函数复制对象:
SomeGrammar aGrammar = new SomeGrammar();
SomeGrammar newGrammar = new SomeGrammar(aGrammar);
interface Interface {
}
class A implements Interface {
public A(Interface other) {
}
}
class B implements Interface {
public B(Interface other) {
}
}
public class Test {
public static Interface newWithExactType(Interface i) throws Exception {
Class<? extends Interface> c = i.getClass();
Constructor<? extends Interface> ctor = c.getConstructor(Interface.class);
return ctor.newInstance(i);
}
public static void main(String[] args) throws Exception {
Interface a = newWithExactType(new A(null));
Interface b = newWithExactType(new B(null));
System.out.println("Type of a: "+a.getClass().getSimpleName());
System.out.println("Type of b: "+b.getClass().getSimpleName());
}
}
我正在尝试实现新类型的语法。因此,我打算拿出核心方法,实现一个Grammar
接口,甚至将这些方法整合到Mapper
接口中。所以所有语法类都将实现这个接口
无论如何,我要问你的问题是,替换上面这一行的最佳方法是什么,这样我就可以概括复制接口类型当前引用的语法对象 显然我不能
Grammar newGrammar = new Grammar(aGrammar);
因为Grammar
是接口的类型,而newgrammar(aGrammar)
没有任何意义。我想我可以在我的接口中添加一个克隆方法来代替复制构造函数,但是正如我之前所说的,我非常感谢你对这件事的意见
提前感谢,Eoin如果我正确理解了您的问题,我的解决方案如下所示: 假设我们有3个我们想要创建的具体语法,GrammarA、GrammarB和GrammarC。我们将定义这些类中的每一个来实现语法接口。GrammarInterface将定义foo()函数和doCopy()函数。然后,这些具体的语法类中的每一个都将负责实现foo()功能,并通过调用copy构造函数返回自己的副本。这就是你要找的吗
interface IGrammar {
public void foo();
public Grammar doCopy();
}
class GrammarA implements IGrammar {
public Grammar doCopy(Grammar g) {
return new GrammarA(g);
}
}
如果我正确理解了您的问题,我的解决方案如下所示: 假设我们有3个我们想要创建的具体语法,GrammarA、GrammarB和GrammarC。我们将定义这些类中的每一个来实现语法接口。GrammarInterface将定义foo()函数和doCopy()函数。然后,这些具体的语法类中的每一个都将负责实现foo()功能,并通过调用copy构造函数返回自己的副本。这就是你要找的吗
interface IGrammar {
public void foo();
public Grammar doCopy();
}
class GrammarA implements IGrammar {
public Grammar doCopy(Grammar g) {
return new GrammarA(g);
}
}
您可以添加一个clone()
,作为接口的一部分,并让具体的类正确地实现它。然后,您的客户端代码将如下所示:
Grammar aGrammar = new SomeGrammar();
Grammar newGrammar = aGrammar.clone();
Grammar newGrammar = oldGrammar.makeCopy();
查看一些解释。就我个人而言,我建议您将自己的copy()
方法作为Grammar
接口的一部分。这比重用clone()
IMHO要好。您可以添加clone()
作为接口的一部分,并让具体的类正确地实现它。然后,您的客户端代码将如下所示:
Grammar aGrammar = new SomeGrammar();
Grammar newGrammar = aGrammar.clone();
Grammar newGrammar = oldGrammar.makeCopy();
查看一些解释。就我个人而言,我建议您将自己的
copy()
方法作为Grammar
接口的一部分。这比重用clone()
IMHO要好。我认为您想要的是语法
界面包含制作副本
签名
然后制作一个副本看起来像:
Grammar aGrammar = new SomeGrammar();
Grammar newGrammar = aGrammar.clone();
Grammar newGrammar = oldGrammar.makeCopy();
避免使用
克隆
方法,因为那样会导致疯狂 我认为您想要的是语法
界面包含制作副本
签名
然后制作一个副本看起来像:
Grammar aGrammar = new SomeGrammar();
Grammar newGrammar = aGrammar.clone();
Grammar newGrammar = oldGrammar.makeCopy();
避免使用
克隆
方法,因为那样会导致疯狂 在我看来,最好的选择是
公共接口映射器扩展了可克隆性{
公共对象克隆();
}
并让该方法调用super.clone(),并在相关的情况下执行深度克隆(原则上只有实现类知道这一点)
使用反射率可能会影响性能,并使扩展设计变得(太)复杂。这里的另一个选择是提供一个抽象类,该类以复制构造函数而不是接口为特征,但它有明显的限制,可能会成为一个阻碍。IMO最好的选择是
公共接口映射器扩展了可克隆性{
公共对象克隆();
}
并让该方法调用super.clone(),并在相关的情况下执行深度克隆(原则上只有实现类知道这一点)
使用反射率可能会影响性能,并使扩展设计变得(太)复杂。这里的另一个选项是提供一个抽象类,该类以复制构造函数(而不是接口)为特征,但它有明显的限制,可能会成为显示的障碍。您还可以使用反射来查找语法类型,并使用匹配的构造函数创建此类型的新实例:
SomeGrammar aGrammar = new SomeGrammar();
SomeGrammar newGrammar = new SomeGrammar(aGrammar);
interface Interface {
}
class A implements Interface {
public A(Interface other) {
}
}
class B implements Interface {
public B(Interface other) {
}
}
public class Test {
public static Interface newWithExactType(Interface i) throws Exception {
Class<? extends Interface> c = i.getClass();
Constructor<? extends Interface> ctor = c.getConstructor(Interface.class);
return ctor.newInstance(i);
}
public static void main(String[] args) throws Exception {
Interface a = newWithExactType(new A(null));
Interface b = newWithExactType(new B(null));
System.out.println("Type of a: "+a.getClass().getSimpleName());
System.out.println("Type of b: "+b.getClass().getSimpleName());
}
}
接口{
}
类实现接口{
公共A接口(其他){
}
}
类实现接口{
公共B(其他接口){
}
}
公开课考试{
公共静态接口newWithExactType(接口i)引发异常{
类您还可以使用反射来查找语法类型,并使用匹配的构造函数创建此类型的新实例:
SomeGrammar aGrammar = new SomeGrammar();
SomeGrammar newGrammar = new SomeGrammar(aGrammar);
interface Interface {
}
class A implements Interface {
public A(Interface other) {
}
}
class B implements Interface {
public B(Interface other) {
}
}
public class Test {
public static Interface newWithExactType(Interface i) throws Exception {
Class<? extends Interface> c = i.getClass();
Constructor<? extends Interface> ctor = c.getConstructor(Interface.class);
return ctor.newInstance(i);
}
public static void main(String[] args) throws Exception {
Interface a = newWithExactType(new A(null));
Interface b = newWithExactType(new B(null));
System.out.println("Type of a: "+a.getClass().getSimpleName());
System.out.println("Type of b: "+b.getClass().getSimpleName());
}
}
接口{
}
类实现接口{
公共A接口(其他){
}
}
类实现接口{
公共B(其他接口){
}
}
公开课考试{
公共静态接口newWithExactType(接口i)引发异常{
ClassObject.clone()不好,请参阅有效Java进行讨论