Java中的“抽象类”是什么?


当前回答

使用abstract关键字声明的类称为抽象类。 抽象是一个隐藏数据实现细节,只向用户显示功能的过程。抽象让您关注对象做了什么,而不是它是如何做的。

抽象类的主要内容

An abstract class may or may not contain abstract methods.There can be non abstract methods. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: ex : abstract void moveTo(double deltaX, double deltaY); If a class has at least one abstract method then that class must be abstract Abstract classes may not be instantiated (You are not allowed to create object of Abstract class) To use an abstract class, you have to inherit it from another class. Provide implementations to all the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

声明抽象类 在声明期间在类之前指定abstract关键字将使其抽象。看看下面的代码:

abstract class AbstractDemo{ }

声明抽象方法 在声明过程中在方法之前指定abstract关键字将使方法抽象。看看下面的代码,

abstract void moveTo();//no body

为什么我们需要抽象类

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for ex -: position, orientation, line color, fill color) and behaviors (for ex -: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for ex : fill color, position, and moveTo). Others require different implementation(for ex: resize or draw). All graphic objects must be able to draw or resize themselves, they just differ in how they do it.

对于抽象超类来说,这是一个完美的情况。您可以利用这些相似性,并将所有图形对象声明为继承自相同的抽象父对象(例如:GraphicObject),如下图所示。

首先,声明一个抽象类GraphicObject,以提供所有子类完全共享的成员变量和方法,例如当前位置和moveTo方法。GraphicObject还声明了抽象方法,如draw或resize,这些方法需要由所有子类实现,但必须以不同的方式实现。GraphicObject类看起来像这样:

abstract class GraphicObject {

  void moveTo(int x, int y) {
    // Inside this method we have to change the position of the graphic 
    // object according to x,y     
    // This is the same in every GraphicObject. Then we can implement here. 
  }

  abstract void draw(); // But every GraphicObject drawing case is 
                        // unique, not common. Then we have to create that 
                        // case inside each class. Then create these    
                        // methods as abstract 
  abstract void resize();
}

在子类中使用抽象方法 GraphicObject的每个非抽象子类,如Circle和Rectangle,必须为draw和resize方法提供实现。

class Circle extends GraphicObject {
  void draw() {
    //Add to some implementation here
  }
  void resize() {
    //Add to some implementation here   
  }
}
class Rectangle extends GraphicObject {
  void draw() {
    //Add to some implementation here
  }
  void resize() {
    //Add to some implementation here
  }
}

在main方法中,你可以像这样调用所有的方法:

public static void main(String args[]){
   GraphicObject c = new Circle();
   c.draw();
   c.resize();
   c.moveTo(4,5);   
}

在Java中实现抽象的方法

在java中有两种实现抽象的方法

抽象类(0到100%) 接口(100%)

具有构造函数、数据成员、方法等的抽象类

abstract class GraphicObject {

  GraphicObject (){
    System.out.println("GraphicObject  is created");
  }
  void moveTo(int y, int x) {
       System.out.println("Change position according to "+ x+ " and " + y);
  }
  abstract void draw();
}

class Circle extends GraphicObject {
  void draw() {
    System.out.println("Draw the Circle");
  }
}

class TestAbstract {  
 public static void main(String args[]){

   GraphicObject  grObj = new Circle ();
   grObj.draw();
   grObj.moveTo(4,6);
 }
}

输出:

GraphicObject  is created
Draw the Circle
Change position according to 6 and 4

记住两条规则:

如果类中抽象方法和具体方法都很少, 将其声明为抽象类。 如果类只有抽象方法,则将其声明为接口。

引用:

TutorialsPoint - Java抽象 Java抽象类方法 Java文档-抽象方法和类 JavaPoint - Java中的抽象类

其他回答

使用abstract关键字声明的类称为抽象类。 抽象是一个隐藏数据实现细节,只向用户显示功能的过程。抽象让您关注对象做了什么,而不是它是如何做的。

抽象类的主要内容

An abstract class may or may not contain abstract methods.There can be non abstract methods. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: ex : abstract void moveTo(double deltaX, double deltaY); If a class has at least one abstract method then that class must be abstract Abstract classes may not be instantiated (You are not allowed to create object of Abstract class) To use an abstract class, you have to inherit it from another class. Provide implementations to all the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

声明抽象类 在声明期间在类之前指定abstract关键字将使其抽象。看看下面的代码:

abstract class AbstractDemo{ }

声明抽象方法 在声明过程中在方法之前指定abstract关键字将使方法抽象。看看下面的代码,

abstract void moveTo();//no body

为什么我们需要抽象类

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for ex -: position, orientation, line color, fill color) and behaviors (for ex -: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for ex : fill color, position, and moveTo). Others require different implementation(for ex: resize or draw). All graphic objects must be able to draw or resize themselves, they just differ in how they do it.

对于抽象超类来说,这是一个完美的情况。您可以利用这些相似性,并将所有图形对象声明为继承自相同的抽象父对象(例如:GraphicObject),如下图所示。

首先,声明一个抽象类GraphicObject,以提供所有子类完全共享的成员变量和方法,例如当前位置和moveTo方法。GraphicObject还声明了抽象方法,如draw或resize,这些方法需要由所有子类实现,但必须以不同的方式实现。GraphicObject类看起来像这样:

abstract class GraphicObject {

  void moveTo(int x, int y) {
    // Inside this method we have to change the position of the graphic 
    // object according to x,y     
    // This is the same in every GraphicObject. Then we can implement here. 
  }

  abstract void draw(); // But every GraphicObject drawing case is 
                        // unique, not common. Then we have to create that 
                        // case inside each class. Then create these    
                        // methods as abstract 
  abstract void resize();
}

在子类中使用抽象方法 GraphicObject的每个非抽象子类,如Circle和Rectangle,必须为draw和resize方法提供实现。

class Circle extends GraphicObject {
  void draw() {
    //Add to some implementation here
  }
  void resize() {
    //Add to some implementation here   
  }
}
class Rectangle extends GraphicObject {
  void draw() {
    //Add to some implementation here
  }
  void resize() {
    //Add to some implementation here
  }
}

在main方法中,你可以像这样调用所有的方法:

public static void main(String args[]){
   GraphicObject c = new Circle();
   c.draw();
   c.resize();
   c.moveTo(4,5);   
}

在Java中实现抽象的方法

在java中有两种实现抽象的方法

抽象类(0到100%) 接口(100%)

具有构造函数、数据成员、方法等的抽象类

abstract class GraphicObject {

  GraphicObject (){
    System.out.println("GraphicObject  is created");
  }
  void moveTo(int y, int x) {
       System.out.println("Change position according to "+ x+ " and " + y);
  }
  abstract void draw();
}

class Circle extends GraphicObject {
  void draw() {
    System.out.println("Draw the Circle");
  }
}

class TestAbstract {  
 public static void main(String args[]){

   GraphicObject  grObj = new Circle ();
   grObj.draw();
   grObj.moveTo(4,6);
 }
}

输出:

GraphicObject  is created
Draw the Circle
Change position according to 6 and 4

记住两条规则:

如果类中抽象方法和具体方法都很少, 将其声明为抽象类。 如果类只有抽象方法,则将其声明为接口。

引用:

TutorialsPoint - Java抽象 Java抽象类方法 Java文档-抽象方法和类 JavaPoint - Java中的抽象类

抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法。抽象类不能被实例化,但可以被子类化。

换句话说,用abstract关键字声明的类在java中称为抽象类。它可以有抽象方法(没有主体的方法)和非抽象方法(有主体的方法)。

重要提示: 抽象类不能用于实例化对象,它们可以用于创建对象引用,因为Java的运行时多态性方法是通过使用超类引用实现的。因此,必须能够创建对抽象类的引用,以便可以使用它指向子类对象。您将在下面的示例中看到该特性

abstract class Bike{  
  abstract void run();  
}  

class Honda4 extends Bike{  
    void run(){
        System.out.println("running safely..");
    }  

    public static void main(String args[]){  
       Bike obj = new Honda4();  
       obj.run();  
    }  
} 

抽象类不能直接实例化,但必须从抽象类派生才能使用。一个类必须是抽象的,如果它包含抽象方法:或者直接

abstract class Foo {
    abstract void someMethod();
}

或间接地

interface IFoo {
    void someMethod();
}

abstract class Foo2 implements IFoo {
}

然而,一个类可以是抽象的,而不包含抽象方法。这是一种防止直接瞬化的方法,例如。

abstract class Foo3 {
}

class Bar extends Foo3 {

}

Foo3 myVar = new Foo3(); // illegal! class is abstract
Foo3 myVar = new Bar(); // allowed!

抽象类的后一种风格可以用来创建“类接口”类。与接口不同,抽象类允许包含非抽象方法和实例变量。您可以使用它为扩展类提供一些基本功能。

另一种常见的模式是在抽象类中实现主要功能,并在由扩展类实现的抽象方法中定义部分算法。愚蠢的例子:

abstract class Processor {
    protected abstract int[] filterInput(int[] unfiltered);

    public int process(int[] values) {
        int[] filtered = filterInput(values);
        // do something with filtered input
    }
}

class EvenValues extends Processor {
    protected int[] filterInput(int[] unfiltered) {
        // remove odd numbers
    }
}

class OddValues extends Processor {
    protected int[] filterInput(int[] unfiltered) {
        // remove even numbers
    }
}

除了这些帖子之外。

有时你可能想声明一个 类,但不知道如何定义 所有的方法都属于这个 类。例如,你可能想要 声明一个名为Writer和的类 在它中包含一个名为 写()。但是,您不知道如何编写write()代码,因为它就是 每种类型的作者都不一样 设备。当然,你打算处理 通过派生Writer的子类, 如打印机,磁盘,网络和 控制台。

什么是抽象类? 好的!让我们举个例子,你对化学了解不多,我们有一个元素碳(符号C)。碳有一些基本的原子结构,你不能改变,但用碳你可以制造很多化合物,如(CO2),甲烷(CH4),丁烷(C4H10)。

所以这里碳是抽象类,你不想改变它的基本结构,但你想让他们的孩子(CO2,CH4等)使用它。但是以他们自己的方式