簡體   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