简体   繁体   English

如何在Java中使用接口作为类型?

[英]How can I use an interface as type in java?

I have created an interface named Leen and created a class X that implements this interface, but when i go to main class to create a variable named x like this 我创建了一个名为Leen的接口,并创建了一个实现该接口的类X,但是当我进入主类来创建一个名为x的变量时,就像这样

Leen x;
x.printname();

The compiler gives me error: 编译器给我错误:

Connect.java:10: error: variable x might not have been initialized

You can't use an interface without implementation. 没有实现,就不能使用接口。 Implementation can be a concrete class or abstract class which implements your interface. 实现可以是实现您的接口的具体类或抽象类。 Based on your code, clearly there is no implementation for you interface. 根据您的代码,显然没有接口实现。 I'm also not sure where did you use the variable p . 我也不确定您在哪里使用变量p Kindly provide your whole class so that we can help you. 请提供您的整个课程,以便我们为您提供帮助。

Suppose if u want to access in the variable with out instantiating it u can make the variable static in the class. 假设如果您想在不实例化的情况下访问变量,则可以使该变量在类中为静态。 say App is ur class name and xyz is ur interface. App是您的类名,而xyz是您的界面。

class App implements xyz{
    public static int x;
    public static void printme(){
        //Your code goes here...
    }

}

Now u can access that from ur mainclass like 现在你可以从你的主类访问它了

App app= null;
app.x= 6;
app.printme();

As mentioned earlier, it's because you declared the variable but never instantiated it, meaning it does not exsist yet, it's just a placeholder for the type defined at the declaration, what you need to do is 如前所述,这是因为您声明了变量但从未实例化它,这意味着它尚不存在,它只是声明中定义的类型的占位符,您需要做的是

Leen x; // declare the variable
x = new implementationClass(); // instantiate the variable

x.PrintName(); // then you functions

Now since the interface is used, all functions on the interface is available, but the code will attempt to use the implementation form whatever class implemented the interface and was intantiated 现在,由于使用了接口,因此接口上的所有功能都可用,但是代码将尝试使用实现形式,无论实现接口并实例化的任何类

Why is this smart ? 为什么这很聪明? well say you lave a List<Leen> then all elements in the list can use the functions on Leen regardless of what implementation of Leen they were created with 好吧,如果您喜欢List<Leen>那么列表中的所有元素都可以使用Leen上的函数,而与创建Leen实现无关

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何引用接口在Java中实现的类类型? - How can I refer to the class type a interface is implementing in Java? 我如何在java中使用一个接受扩展接口类型的参数的函数来创建接口? - How can i make an interface in java with a function that accepts a parameter of the type that extends the interface? 如何在接口的默认方法中使用java接口计算两组集合的并集,交集和差? - How can I use a java interface to calculate the union, intersection, and difference of two sets, all in a default method in the interface? 在Java界面中,我怎么能*不*使用从父接口继承的一个特定方法? - In a Java interface, how can I *not* use one particular method inherited from a parent interface? 如何通过 Java 反射获得存储在接口类型中声明的变量中的实现 class 的类型? - How can I get the type of the implementation class stored in the variable declared in the interface type by Java reflection? 如何使用Java泛型来列出 <InterfaceImpl> 可以为具有返回类型List的方法返回 <Interface> - How to use Java Generics so that List<InterfaceImpl> can be returned for a method that has return type List<Interface> 在java中使用接口中的实现类型 - use implementation type in interface in java 在Java中将接口用作参数类型 - Use of interface as parameter type in java 如何在java Map接口中关联泛型类型参数的值? - how can I relate values of generic type parameters in java Map interface? 如何避免在Java 8中使用接口? - How can I avoid using an interface in java 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM