簡體   English   中英

如何以及何時使用抽象類

[英]How and when to use an abstract class

這是我的 Java 測試程序。 我想知道這里有多少抽象類更重要,以及為什么我們為此使用抽象類。

它是強制性的還是最好的方法; 如果是這樣怎么辦?

class Shape1 {
    int i = 1;
    void draw() {
        System.out.println("this is shape:" + i);
    }
}

class Shape2 {
    int i = 4;
    void draw() {
        System.out.println("this is shape2:" + i);
    }
}


class Shape {
    public static void main(String args[]) {
        Shape1 s1 = new Shape1();
        s1.draw();

        Shape2 s2 = new Shape2();
        s2.draw();
    }
}

您將在此處使用抽象類或接口來創建提供void draw()方法的公共基類/接口,例如

abstract class Shape() {
  void draw();
}

class Circle extends Shape {
   void draw() { ... }
}

...

Shape s = new Circle();
s.draw();

我通常會使用一個接口。 但是,如果出現以下情況,您可能會使用抽象類:

  1. 您需要/想要提供通用功能或類成員(例如您的情況下的int i成員)。
  2. 您的抽象方法除了公共訪問(這是接口允許的唯一訪問類型)之外還有任何其他內容,例如在我的示例中, void draw()將具有包可見性。

抽象類與您的子類具有“is-a”類型關系。 因此,例如,您可以有一個抽象類Shape ,它具有任何形狀所具有的東西(如繪制函數),然后是一個SquareShape類。 每個正方形都是一個形狀,但並非所有形狀都是正方形。

在您的示例中,您有 2 個形狀類型,以及一個具有 2 個這些實例的類。 我認為這不是你應該用abstract定義的關系。

不過,您可能想對您的名字做一些事情,因為這是一個很小的例子,很難看出文件的真正含義以及它們應該如何工作。

抽象類是一個類,它至少有一個未實現的方法,或關鍵字abstract。 例如,抽象方法可能如下所示:

public abstract String myMethod(String input);

(請注意,該方法以分號結尾)。

一個類可能如下所示:

public abstract class MyClass {

    public abstract String myMethod(String input);

    public String anotherMethod(String input) {
        return intput + " additional text";
    }
}

抽象類不能被實例化。 抽象類需要一個子類來實現缺失的行為,以便它可以被實例化。

抽象類的主要目標是提供公共行為的共享實現——促進代碼的重用。

在 Java 中,可以通過使用類的組合而不是從廣泛定義的抽象類繼承來實現相同的效果。 這允許更多模塊化、功能特定的類促進代碼重用,從而提高可維護性。

我的建議是僅在絕對必要時才使用抽象類,尤其要避免將其用作充滿各種功能的技巧包。

在 Scala 中,人們會使用特征,這是解決這個問題的一種優雅方式。 但是,它確實需要大量關注才能正確完成。

編輯:從 Java 8 開始,接口中的默認方法是另一種添加常見行為的方法。

在 java 中使用抽象類的示例示例。

package use_of_abstract;

abstract class Shapes 
{
   int i=1;

   abstract void draw();
   abstract void color(String mycolor);

   //not an abstract method
   void fill()
   {
      System.out.println("Non-Abstract Method -> Fill"); 
   }

   //not an abstract method
   String anotherMethod(String input) 
   {
       return input + " additional text";
   }

}

package use_of_abstract;

public class Shape_One extends Shapes 
{
    int i=1;

    @Override
    void draw() 
    {
        System.out.println("This is Shape One:"+i);
    }

    @Override
    void color(String mycolor) 
    {
        System.out.println("This is Shape One:"+mycolor);

    }

    @Override
    String anotherMethod(String anotherMethod) 
    {
        System.out.println("This is Shape One:"+anotherMethod);
        return anotherMethod;

    }

}

package use_of_abstract;

public class Shape_Two extends Shapes
{
    int i=2;

    @Override
    void draw() 
    {
        System.out.println("This is Shape Two :"+i);
    }

    @Override
    void color(String mycolor) 
    {
        System.out.println("This is Shape Two Color:"+mycolor);
    }

    @Override
    String anotherMethod(String anotherMethod) 
    {
        System.out.println("This is Shape Two:"+anotherMethod);
        return anotherMethod;

    }

}

package use_of_abstract;

import java.awt.Color;

public class Shape_Main 
{

    public static void main(String args[])
    {
        Shape_One s1;
        Shape_Two s2;

        s1=new Shape_One();
        s2= new Shape_Two();

        s1.draw();
        s2.draw();

        s1.fill();
        s2.fill();

        s1.color("Blue");
        s2.color("Green");


        s1.anotherMethod("HELLO..............Its Another Method 1");
        s2.anotherMethod("HELLO..............Its Another Method 2");


    }   
}

常見的東西 + 不常見的東西 = 抽象類

什么時候使用?

抽象類最適合有大量可重用代碼且您不想一次又一次編寫的場景 + 每個類特有的東西很少。

例子?

儀式:

常見的事情:唱國歌,升旗等
具體的東西:印度國旗,印度國歌(印度),中國國旗,中國國歌(中國)等。

在此處輸入圖片說明

如何使用它?

1)創建抽象類
2)把所有東西都放在公共方法中,這是常見的
3) 將所有內容都放在特定於子類的抽象方法中

示例代碼在哪里?

基類:

public abstract class BaseCeremony{

    Flag natonalFlag;
    Anthem anthem;

    //**** Common Task ******//
    public void startCeremony(){
        configure();
        hoistFlag();
        singAnthem();
    }

    public void hoistFlag(){
        natonalFlag.hoist();                       
    }

    public void singAnthem(){
        anthem.anthem();                       
    }

    private void configure(){
        natonalFlag = provideFlag();
        anthem = provideAnthem();
    }

    //**** Differs in all part ******//
    public abstract Flag provideFlag();
    public abstract Anthem provideAnthem();

}

在子類中,您只需要提供不常見部分的實現。
兒童班

public class IndianCeremony extends BaseCeremony{

       public Flag provideFlag(){
            return new IndianFlag();
       }

       public Anthem provideAnthem(){
            return new IndianAnthem();
       }
}

獎金

  • 抽象類是不完整的類,這就是您不能創建其對象的原因。
  • 一個抽象類應該至少有一個抽象方法才能成為抽象類
  • Android中抽象類實現示例

抽象類,顧名思義就是抽象類。

抽象類不談實現部分。 實際上,我們擴展抽象類來為其抽象方法提供實現。 它也可以有非抽象方法。

在這里檢查: 在 Java 中使用抽象類

編輯 :

示例代碼:

abstract class Shapes {

 int i=1;
 abstract void draw(); 

 // Remember : Non-abstract methods are also allowed 
 void fill() {
     System.out.println("Non abstract method - fill");
 }
}

class Shape1 extends Shapes {

 int i=1;
 void draw(){
 System.out.println("This is Shape 1:"+i);
 }
}

class Shape2 extends Shapes {
    int i=2;
    void draw() {
        System.out.println("This is Shape 2:"+i);
    }
}

class Shape {

public static void main(String args[])
       {
        Shape1 s1 = new Shape1();
        s1.draw();                     // Prints This is Shape 1:1

        Shape2 s2 = new Shape2();
        s2.draw();                     // Prints This is Shape 2:2
       }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM