如何创建抽象类和接口的对象

发布于 2024-10-05 12:22:40 字数 40 浏览 5 评论 0原文

如何创建抽象类和接口的对象?我知道我们不能直接实例化抽象类的对象。

How do I create an object of an abstract class and interface? I know we can't instantiate an object of an abstract class directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

活泼老夫 2024-10-12 12:22:45

不,您不会在这里创建抽象类的实例。相反,您正在创建抽象类的匿名子类的实例。然后您在指向子类对象的抽象类引用上调用该方法。

No, you are not creating the instance of your abstract class here. Rather you are creating an instance of an anonymous subclass of your abstract class. And then you are invoking the method on your abstract class reference pointing to subclass object.

不美如何 2024-10-12 12:22:44

你所知道的都是正确的。您不能创建抽象类或接口的对象,因为它们是不完整的类(接口甚至不被视为类)。

您可以做的是实现抽象类的子类,当然,抽象类不能是抽象的。对于接口,您必须创建一个实现该接口的类并实现接口方法的主体。

这里是oracle网站上的原始教程,http://download.oracle.com /javase/tutorial/java/IandI/abstract.htmlhttp://download.oracle.com/javase/tutorial/java/concepts/interface.html

What you know is correct. You cannot create an object of abstract class or interface since they are incomplete class (interface is not even considered as a class.)

What you can do is to implement a subclass of abstract class which, of course, must not be abstract. For interface, you must create a class which implement the interface and implement bodies of interface methods.

Here are orginal tutorial on oracle site, http://download.oracle.com/javase/tutorial/java/IandI/abstract.html and http://download.oracle.com/javase/tutorial/java/concepts/interface.html

傾旎 2024-10-12 12:22:44

您不能实例化抽象类或接口,但可以实例化它们的子类/实现器之一。

您无法实例化抽象类或接口,只能实例化它们的派生类之一。

在您的示例中,

 MyAbstractClass ABC = new MyAbstractClass("name") {
    };

您正在实例化任何实现令人惊奇的类。

You can not instantiate the abstract class or an interface, but you can instantiate one of their subclasses/implementers.

You can't instantiate an abstract class or an interface, you can only instantiate one of their derived classes.

In your example

 MyAbstractClass ABC = new MyAbstractClass("name") {
    };

You are instantiating any class that implements Suprising.

匿名。 2024-10-12 12:22:44
public abstract class AbstractClass { ... }

public interface InterfaceClass { ... }

// This is the concrete class that extends the abstract class above and
// implements the interface above.  You will have to make sure that you implement
// any abstract methods from the AbstractClass and implement all method definitions
// from the InterfaceClass
public class Foo extends AbstractClass implements InterfaceClass { ... }
public abstract class AbstractClass { ... }

public interface InterfaceClass { ... }

// This is the concrete class that extends the abstract class above and
// implements the interface above.  You will have to make sure that you implement
// any abstract methods from the AbstractClass and implement all method definitions
// from the InterfaceClass
public class Foo extends AbstractClass implements InterfaceClass { ... }
墨小墨 2024-10-12 12:22:44

不,我们不能从接口或抽象类创建对象,因为

创建对象的主要目的是利用包装的方法和数据。
由于接口没有任何具体的实现,因此我们不能。

对于抽象类,我们可能有具体方法或抽象方法或两者都有。
API 开发人员无法限制未实现的方法的使用。

希望有帮助。

NO, we can't create object out of an interface or Abstract class because

Main intention of creating an object is to utilize the wrapped methods and data.
As interface don't have any concrete implementation hence we cannot.

For abstract class we may have concrete method or abstract method or both.
There is no way for the API developer to restrict the use of the method thats don't have implementation.

Hope help.

七分※倦醒 2024-10-12 12:22:43
public abstract class Foo { public abstract void foo(); }
public interface Bar { public void bar(); }
public class Winner extends Foo implements Bar {
  @Override public void foo() { }
  @Override public void bar() { }
}
new Winner(); // OK
public abstract class Foo { public abstract void foo(); }
public interface Bar { public void bar(); }
public class Winner extends Foo implements Bar {
  @Override public void foo() { }
  @Override public void bar() { }
}
new Winner(); // OK
岁月打碎记忆 2024-10-12 12:22:43

“实例化”的意思是“创建一个对象”。

所以你不能直接创建一个。

接口和抽象类的目的是描述一些实现接口或扩展抽象类的具体类的行为。

实现接口的类可以被其他只知道接口的代码使用,这有助于您分离职责,并清楚您想要从对象中得到什么。 (调用代码只会知道该对象可以执行接口中指定的任何操作;它不会知道它具有的任何其他方法。)

如果您正在使用需要 Fooable 的其他人的代码(其中这是某个接口的名称),实际上并没有要求您提供某个 Fooable 类的对象(因为实际上并不存在这样的类)。您只是被要求提供某个实现 Fooable 的类的实例,即声明它可以执行该接口中的所有操作。简而言之,就是“可以被Foo'd”的东西。

"instantiate" means "create an object of".

So you can't create one directly.

The purpose of interfaces and abstract classes is to describe the behaviour of some concrete class that implements the interface or extends the abstract class.

A class that implements an interface can be used by other code that only knows about the interface, which helps you to separate responsibilities, and be clear about what you want from the object. (The calling code will only know that the object can do anything specified in the interface; it will not know about any other methods it has.)

If you are using someone else's code that expects a Fooable (where that is the name of some interface), you are not really being asked for an object of some Fooable class (because there isn't really such a class). You are only being asked for an instance of some class that implements Fooable, i.e. which declares that it can do all the things in that interface. In short, something that "can be Foo'd".

孤蝉 2024-10-12 12:22:43

您编写一个派生自抽象类或实现该接口的类,然后实例化它。

You write a class that derives from the abstract class or implements the interface, and then instantiate that.

所有深爱都是秘密 2024-10-12 12:22:42

要创建抽象类的对象,只需使用new,就像创建其他非抽象类的对象一样,只有一点点不同,如下:

package com.my.test;

public abstract class MyAbstractClass {
    private String name;

    public MyAbstractClass(String name)
    {
        this.name = name;
    }

    public String getName(){
        return this.name;
    }


}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {
        MyAbstractClass ABC = new MyAbstractClass("name") {
        };

        System.out.println(ABC.getName());
    }

}

同样,您可以创建接口类型的对象,就像如下:

package com.my.test;

public interface MyInterface {

    void doSome();
    public abstract void go();

}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {

        MyInterface myInterface = new MyInterface() {

            @Override
            public void go() {
                System.out.println("Go ...");

            }

            @Override
            public void doSome() {
                System.out.println("Do ...");

            }
        };

        myInterface.doSome();
        myInterface.go();
    }

}

To create object of an abstract class just use new just like creating objects of other non abstract classes with just one small difference, as follows:

package com.my.test;

public abstract class MyAbstractClass {
    private String name;

    public MyAbstractClass(String name)
    {
        this.name = name;
    }

    public String getName(){
        return this.name;
    }


}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {
        MyAbstractClass ABC = new MyAbstractClass("name") {
        };

        System.out.println(ABC.getName());
    }

}

In the same way You can create an object of interface type, just as follows:

package com.my.test;

public interface MyInterface {

    void doSome();
    public abstract void go();

}

package com.my.test;

public class MyTestClass {

    public static void main(String [] args)
    {

        MyInterface myInterface = new MyInterface() {

            @Override
            public void go() {
                System.out.println("Go ...");

            }

            @Override
            public void doSome() {
                System.out.println("Do ...");

            }
        };

        myInterface.doSome();
        myInterface.go();
    }

}
风铃鹿 2024-10-12 12:22:42

有两种方法可以实现这一目标。

1)要么在新类中扩展/实现抽象类/接口,创建这个新类的对象,然后根据您的需要使用该对象。

2) 编译器允许您在代码中创建接口的匿名对象。

例如。 ( 新的 Runnable() { ... } );

希望这有帮助。

问候,
马亨德拉·利亚。

There are two ways you can achieve this.

1) Either you extend / implement the Abstract class / interface in a new class, create the object of this new class and then use this object as per your need.

2) The Compiler allows you to create anonymous objects of the interfaces in your code.

For eg. ( new Runnable() { ... } );

Hope this helps.

Regards,
Mahendra Liya.

不知在何时 2024-10-12 12:22:42

您可以提供一个实现作为匿名类:

new SomeInterface() {
    public void foo(){
      // an implementation of an interface method
    }
};

同样,匿名类可以扩展父类而不是实现接口(但它不能两者兼而有之)。

You can provide an implementation as an anonymous class:

new SomeInterface() {
    public void foo(){
      // an implementation of an interface method
    }
};

Likewise, an anonymous class can extend a parent class instead of implementing an interface (but it can't do both).

如梦亦如幻 2024-10-12 12:22:41

您不能实例化抽象类或接口 - 您可以实例化它们的子类/实现器之一。

这种情况的典型例子是 Java 集合的使用。

List<String> stringList = new ArrayList<String>();

您使用接口类型 List 作为类型,但实例本身是 ArrayList

You can not instantiate an abstract class or an interface - you can instantiate one of their subclasses/implementers.

Examples of such a thing are typical in the use of Java Collections.

List<String> stringList = new ArrayList<String>();

You are using the interface type List<T> as the type, but the instance itself is an ArrayList<T>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文