参照接口创建对象 [英] Creating object with reference to Interface

查看:108
本文介绍了参照接口创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将引用变量声明为类类型或接口类型。如果将变量声明为接口类型,则它可以引用实现该接口的任何类的任何对象。


<基于以上陈述,我编写了理解代码。如上所述,声明为接口类型,它可以引用实现该接口的任何类的任何对象


但是在我的代码中显示的是 displayName()方法未在 objParent.displayName()

 公共类OverridenClass {
public static void main(String [] args){
Printable objParent = new Parent();
objParent.sysout();
objParent.displayName();
}
}

接口Printable {
void sysout();
}

类父级实现Printable {
public void displayName(){
System.out.println( This is Parent Name);
}

public void sysout(){
System.out.println(父类中我是可打印接口);
}
}

我确定我理解了错误的方式。有人可以解释吗?

解决方案


但是在我的代码中显示的displayName()方法未定义。


对,因为 displayName 没有在 Printable中定义界面。即使具体的类有其他方法,也只能通过声明为具有该接口的变量访问该接口上定义的方法。这就是为什么您可以调用 sysout 而不是 displayName 的原因。


原因如果您考虑这样的示例,则更明显:

  class Bar {
public static void foo(Printable p){
p.sysout();
p.displayName();
}
}

类测试{
public static final void main(String [] args){
Bar.foo(new Parent()) ;
}
}

foo 一定不要依赖除 Printable 界面中的功能以外的任何内容,因为我们在编译时不知道具体的类是什么。


接口的重点是仅使用接口引用来定义代码可使用的特征,而不必考虑所使用的具体类。


A reference variable can be declared as a class type or an interface type.If the variable is declared as an interface type, it can reference any object of any class that implements the interface.

Based on the above statement I have made a code on understanding. As said above declared as an interface type, it can reference any object of any class that implements the interface.

But in my code is displaying displayName() method undefined at objParent.displayName():

public class OverridenClass {
    public static void main(String[] args) {
        Printable objParent = new Parent();
        objParent.sysout();
        objParent.displayName();
    }
}

interface Printable {
    void sysout();
}

class Parent implements Printable {
    public void displayName() {
        System.out.println("This is Parent Name");
    }

    public void sysout() {
        System.out.println("I am Printable Interfacein Parent Class");
    }
}

I am sure I have understood the wrong way. Can someone explain the same?

解决方案

But in my code is displaying displayName()method undefined.

Right, because displayName is not defined in the Printable interface. You can only access the methods defined on the interface through a variable declared as having that interface, even if the concrete class has additional methods. That's why you can call sysout, but not displayName.

The reason for this is more apparent if you consider an example like this:

class Bar {
    public static void foo(Printable p) {
        p.sysout();
        p.displayName();
    }
}

class Test {
    public static final void main(String[] args) {
        Bar.foo(new Parent());
    }
}

The code in foo must not rely on anything other than what is featured in the Printable interface, as we have no idea at compile-time what the concrete class may be.

The point of interfaces is to define the characteristics that are available to the code using only an interface reference, without regard to the concrete class being used.

这篇关于参照接口创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆