Java 如何将接口强制转换为类?

Java 如何将接口强制转换为类?,java,class,interface,groovy,casting,Java,Class,Interface,Groovy,Casting,我有一个接口类对象作为方法参数提供,并且我还有一个正在转换的对象的对象实例 任务:将对象转换为interface=object,但手动使用Class.cast()方法 以及该节目的明星: def readObject(type, path) { Object obj // Prevent playing with NPE if (groovyClassLoader == null) groovyClassLoader = n

我有一个接口类对象作为方法参数提供,并且我还有一个正在转换的对象的对象实例

任务:将对象转换为interface=object,但手动使用Class.cast()方法

以及该节目的明星:

def readObject(type, path) {
        Object obj
        // Prevent playing with NPE
        if (groovyClassLoader == null)
            groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader())

        // The final quest begins
        try {
            Class groovyClass = groovyClassLoader.parseClass(new File(path))

// at this point: type ins an instance of Class which is an interface
// to which I need to assign the obj instance

// groovyClass is an instance of Class 
// out of which I need to get object and eventually cast it to type
// so something like:
//            groovyClass = groovyClass.cast(type)
//            obj = groovyClass.newInstance()
// or
//            obj = groovyClass.newInstance()
//            obj = groovyClass.cast(type)



            obj = groovyClass.newInstance()
            obj = type.cast(obj)

        } catch (ex){
            //TODO:IO and obj creation exception should be logged
            ex.printStackTrace()
        }
        return obj
    }

这是一个强大的魔法师施放的战争求生任务,我开始怀疑我的任务不在强大的土地可能性范围内XD

你不能创建一个界面实例。您必须首先定义一个实现接口的类,然后创建该类的实例。然后,您可以将此对象作为参数传递给任何接受接口类型参数的方法。

您无法创建接口实例。您必须首先定义一个实现接口的类,然后创建该类的实例。然后,您可以将此对象作为参数传递给任何接受接口类型参数的方法。

您不能拥有接口的实例。界面就像一个模板。实现接口的具体类可以有自己的实例。假设我有以下几点:

public interface IExample {
  public void foo();
}

class Example implements IExample {
  public void foo() {
    // do something
  }
}

class MyMain {
  public static void main(String[] args) {
    IExample iExample;
    Example example = new Example();

    iExample = example; // Polymorphism :)

    // IExample iExample = new IExample();  -- is wrong
  }
}

在上面的示例中,可以将对象“example”强制转换为IExample(多态性),但是您不能为IExample接口分配内存,因为它什么都不做

接口不能有实例。界面就像一个模板。实现接口的具体类可以有自己的实例。假设我有以下几点:

public interface IExample {
  public void foo();
}

class Example implements IExample {
  public void foo() {
    // do something
  }
}

class MyMain {
  public static void main(String[] args) {
    IExample iExample;
    Example example = new Example();

    iExample = example; // Polymorphism :)

    // IExample iExample = new IExample();  -- is wrong
  }
}

在上面的示例中,可以将对象“example”强制转换为IExample(多态性),但是您不能为IExample接口分配内存,因为它什么都不做

Class
是一个类,而不是一个接口。也许你可以通过发布一些代码来澄清这个问题?:-)另外,这与Groovy有什么关系?
Class
是一个类,而不是一个接口。也许你可以通过发布一些代码来澄清这个问题?:-)还有,这与Groovy有什么关系?是的,在清除代码中的所有冗余注释后,我明白了这一点:D但太晚了)谢谢,好的。很好,你得到了答案:)我想你的意思是“实现”接口,而不是“继承”接口。是的,在清除代码中的所有冗余注释后,我理解了这一点:D但为时已晚)谢谢,好的。很好,你得到了答案:)我想你的意思是“实现”接口,而不是“继承”接口。别忘了,你可以在Groovy中使用映射作为接口的实现,即:
def endlessStreamOfOnes=[hasNext:{true},next:{1}]作为迭代器
别忘了,你可以在Groovy中使用映射作为接口的实现,ie:
def endlessStreamOfOnes=[hasNext:{true},next:{1}]作为迭代器