java 我可以在不实现接口的情况下调用接口内部的方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4554661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can i call a method which is inside an interface without implementing the interface?
提问by theJava
Can i call a method inside an interface without implementing the interface in my class?
我可以在接口内调用方法而不在我的类中实现接口吗?
package;
import Contact;
public interface IPerson{
public void savePerson(Contact contact);
}
Now some class here...
现在在这里上课...
public class HulkHogan {
//Calling the method savePerson here
//I dont want to implement the Interface in all.
}
采纳答案by rodion
Statically, you can declare it to be called. You don't necessarily have to implement the interface insidethe calling class. For example:
静态地,您可以声明它被调用。你不一定要实现接口里面调用的类。例如:
public class HulkHogan {
private IPerson person;
public HulkHogan(IPerson person){
this.person = person;
}
public void doSomething(Contant contact){
//call your method here
person.savePerson(contact);
}
}
But in order to execute it, you willneed to implement it somewhere, because interface just describes how something behaves but does not provide the actual behaviour (i.e. does not implement it).
但为了执行它,你将需要在一些地方实现它,因为接口只是描述事情的行为,但不提供实际的行为(即没有实现它)。
回答by fastcodejava
You can in this way :
你可以这样:
Person p = new IPerson{
public void savePerson(Contact contact) {
// some code
}
}
Now call savePerson
on p. You are implementing IPerson
without creating a separate class.
现在调用savePerson
p。您正在实施IPerson
而不创建单独的类。
回答by RoflcoptrException
You can't because the method doesn't contain a definition (method body). The purpose of a interface is to implement it somewhere so that you have an actual implementation of this method.
你不能,因为该方法不包含定义(方法体)。接口的目的是在某处实现它,以便您实际实现此方法。
回答by Grace
No you cant...bcoz all methods of the class interface are abstract..and you cant use them without implementing the class ...
不,你不能......因为类接口的所有方法都是抽象的......并且你不能在不实现类的情况下使用它们......
回答by Jigar Joshi
NoYou can't call it, You will have to provide its implementation atleast
不,你不能调用它,你必须至少提供它的实现
Further to call method you will need an object. you can't instantiate interfacewithout its implementation provided
进一步调用方法,您将需要一个对象。如果没有提供接口,就不能实例化接口
回答by jbx
The interface is just a definition of methods without implementation. If you are not implementing the methods what will they do when you call them?
接口只是方法的定义,没有实现。如果你没有实现这些方法,当你调用它们时它们会做什么?
On the other hand, an interface is not necessarily implemented by the calling class. So in your case HulkHogan can call another object's savePerson() method, without implementing it itself. But some class has to implement it somewhere for it to do something.
另一方面,接口不一定由调用类实现。因此,在您的情况下,HulkHogan 可以调用另一个对象的 savePerson() 方法,而无需自己实现它。但是某些类必须在某处实现它才能执行某些操作。
回答by avgvstvs
The goal of an interface is to mandate a specific and well-known contract for a known type of object. For example, by tradition all Stacks have push(), pop(), and peek(). So by writing an interface, you're expressly writing a contract that anyone who writes a stack should follow.
接口的目标是为已知类型的对象授权特定且众所周知的契约。例如,按照传统,所有堆栈都有 push()、pop() 和 peek()。因此,通过编写接口,您明确地编写了任何编写堆栈的人都应遵守的契约。
To put in another way, it is a template that dictates that any class that calls itself a Stack must implement methods called push(), pop(), and peek() as my example here discusses.
换句话说,它是一个模板,它规定任何自称为 Stack 的类必须实现称为 push()、pop() 和 peek() 的方法,如我在此讨论的示例。
public interface StackInterface {
public void push(int x);
public int pop();
public void peek();
}
public class myStack implements StackInterface{
}
It's an object-oriented technique. At this point you will not get a compile because you haven't at least implemented methods for the interface methods. You will need to write methods matching the signatures of the Interface before you can fully use the interface.
这是一种面向对象的技术。在这一点上,你不会得到编译,因为你至少还没有实现接口方法的方法。您需要编写与接口签名匹配的方法,然后才能完全使用该接口。
回答by wkl
Unless you implement
the interface or have a member object of a class that implements the interface, you can not use the savePerson
method, since by definition it has no implementation yet.
除非您implement
是接口或具有实现该接口的类的成员对象,否则您不能使用该savePerson
方法,因为根据定义它还没有实现。
回答by David
You'd have to call it on an object which is an instance of the interface, so either HulkHogan
would have to implement IPerson
or an instance of something which implements IPerson
would have to be supplied to (or instantiated within, though that is not preferred) HulkHogan
.
你必须把它称为一个对象,它是该接口的实例,所以要么HulkHogan
就必须实施IPerson
或一些东西,实现了一个实例IPerson
就必须提供给(或在实例化,尽管这不是优选)HulkHogan
。
More to the point (based also on your previous questions), what do you expect to accomplish by calling a method which has no implementation?
更重要的是(也基于您之前的问题),您希望通过调用一个没有实现的方法来完成什么?
回答by sij
when you say you are implementing an interface for a class, you actually mean you are abiding a contract (or a discipline) which is defined in the interface, that is you will give the definition for the method in interface, so just feel that interface is a container which contains a rule,
当你说你正在为一个类实现一个接口时,你实际上是说你遵守了在接口中定义的契约(或纪律),也就是说你将在接口中给出方法的定义,所以只是感觉那个接口是一个包含规则的容器,
You said you want to call the method in the interface : that is not a method in itself that is just a rule defined so what is the point is calling a method which has no body (actually is its not a method in itself)
你说你想调用接口中的方法:这本身不是一个方法,它只是一个定义的规则,所以调用一个没有主体的方法有什么意义(实际上它本身不是一个方法)
...enjoy
...请享用