即使我知道我只会使用一个新的对象,我是否总是用java中的所有内容创建新对象?
obj = new obj();
obj.method();
Run Code Online (Sandbox Code Playgroud)
要不就
classOfobject.method();
Run Code Online (Sandbox Code Playgroud)
在这样的情况下,为什么我要为创建一个新的obj而烦恼呢?
我在我的对象中使用接口方法时遇到问题.我是一个快速的例子,没有所有的补充.
public class Item{}
public interface IFruit
{
void MethodExample();
}
public class Apple : Item, IFruit
{
public void IFruit.MethodExample(){}
}
// put this in a run method somewhere
var example_item = new Apple();
//Here comes the problem.
example_item.MethodExample();
// this will return an error saying that it cant find the method.
Run Code Online (Sandbox Code Playgroud)
无论如何要做到这一点?我知道它实现了i_fruit.并有方法.但我无法访问它?