繁体   English   中英

Java中的抽象类。

[英]Abstract class in Java.

Wat是指抽象类的间接实例化吗? 我们如何实现这一目标? 正如我尝试过几次..它给错误有任何人为此做过一些事情

    abstract class hello //abstract class declaration 
    { 
    void leo() {}
    }             


    abstract class test {} //2'nd abstract class 


    class dudu {  //main class 

    public static void main(String args[]) 
    {
    hello d = new test() ;  // tried here 
    }

    }

您不能实例化一个抽象类。 Abstract类的整个思想是声明一个子类之间共有的东西,然后对其进行扩展。

  public abstract class Human {
         // This class can't be instantiated, there can't be an object called Human
         }

  public Male extends Human {
         // This class can be instantiated, getting common features through extension from Human class
   } 

  public Female extends Human {
        // This class can be instantiated, getting common features through extension from Human class

   } 

有关更多信息: http : //docs.oracle.com/javase/tutorial/java/IandI/abstract.html

我们不能实例化一个抽象类。如果想要的话,我们必须扩展它。

Wat是不是意味着我对抽象类的间接实例化? 我们如何实现这一目标?

我需要查看使用该短语的上下文,但是我希望“间接实例化”意味着扩展抽象类的非抽象类的实例化。

例如

public abstract class A {
    private int a;
    public A(int a) {
       this.a = a;
    }
    ...
}

public B extends A {
    public B() {
        super(42);
    } 
    ...
}

B b = new B();    // This is an indirect instantiation of A
                  // (sort of ....)

A a = new A(99);  // This is a compilation error.  You cannot
                  // instantiate an abstract class directly.

您不能创建抽象类的实例,我想这就是您想要做的。

abstract class hello //abstract class declaration 
{ 
    void leo() {}
}             

class test extends hello 
{
    void leo() {} // Custom test's implementation of leo method
}

您不能在Java中为Abstract类创建对象。 请参阅此链接-http: //docs.oracle.com/javase/tutorial/java/IandI/abstract.html

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM