java abstract使用_Java Abstract类和方法

In this tutorial, we will learn about abstract class and methods in Java along with understanding how we can implement abstraction using abstract classes. We will also have some code examples.

在本教程中,我们将学习Java中的抽象类和方法,并了解如何使用抽象类实现抽象。 我们还将有一些代码示例。

抽象类 (Abstract Class)

A class which is declared using abstract keyword known as abstract class. An abstract class may or may not have abstract methods. We cannot create object of abstract class.

使用抽象关键字声明的类,称为抽象类 。 抽象类可能具有也可能没有抽象方法。 我们不能创建抽象类的对象。

It is used to achieve abstraction but it does not provide 100% abstraction because it can have concrete methods.

它用于实现抽象,但不能提供100%抽象,因为它可以具有具体的方法。

An abstract class must be declared with an abstract keyword. 抽象类必须用abstract关键字声明。 It can have abstract and non-abstract methods. 它可以具有抽象和非抽象方法。 It cannot be instantiated. 无法实例化。 It is used for abstraction. 它用于抽象。

Syntax:

句法:

abstract class class_name { }

抽象方法 (Abstract method)

Method that are declared without any body within an abstract class are called abstract method. The method body will be defined by its subclass. Abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods.

在抽象类中没有任何主体声明的方法称为抽象方法 。 方法主体将由其子类定义。 抽象方法永远不能是最终的和静态的。 任何扩展抽象类的类都必须实现所有抽象方法。

Syntax:

句法:

abstract return_type function_name (); //No definition

关于抽象类和方法要记住的要点 (Points to Remember about Abstract Class and Method)

Here are some useful points to remember:

这里有一些有用的要记住的地方:

Abstract classes are not Interfaces. They are different, we will study this when we will study Interfaces. 抽象类不是接口。 它们是不同的,我们将在研究接口时进行研究。 An abstract class may or may not have an abstract method. But if any class has even a single abstract method, then it must be declared abstract. 抽象类可能具有也可能没有抽象方法。 但是,如果任何类甚至只有一个抽象方法,则必须将其声明为抽象。 Abstract classes can have Constructors, Member variables and Normal methods. 抽象类可以具有构造函数,成员变量和Normal方法。 Abstract classes are never instantiated. 抽象类从不实例化。 When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract. 使用抽象方法扩展Abstract类时,必须在子类中定义abstract方法,或者使子类抽象。

抽象类的例子 (Example of Abstract class)

Let's take an example of the Abstract class and try to understand how they can be used in Java.

让我们以Abstract类为例,尝试理解如何在Java中使用它们。

In this example, we created an abstract class A that contains a method callme() and Using class B, we are extending the abstract class.

在此示例中,我们创建了一个包含方法callme()的抽象类A,并使用类B扩展了该抽象类。

abstract class A

{

abstract void callme();

}

class B extends A

{

void callme()

{

System.out.println("Calling...");

}

public static void main(String[] args)

{

B b = new B();

b.callme();

}

}

calling...

正在呼叫...

非抽象方法的抽象类 (Abstract class with non-abstract method)

Abstract classes can also have non abstract methods along with abstract methods. But remember while extending the class, provide definition for the abstract method.

抽象类也可以具有非抽象方法以及抽象方法。 但是请记住在扩展类时,为抽象方法提供定义。

abstract class A

{

abstract void callme();

public void show()

{

System.out.println("this is non-abstract method");

}

}

class B extends A

{

void callme()

{

System.out.println("Calling...");

}

public static void main(String[] args)

{

B b = new B();

b.callme();

b.show();

}

calling... this is non-abstract method

调用...这是非抽象方法

使用Abstract类进行抽象 (Abstraction using Abstract class)

Abstraction is an important feature of OOPS. It means hiding complexity and show functionality only to the user. Abstract class is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method. Lets see how abstract class is used to provide abstraction.

抽象是OOPS的重要特征。 这意味着隐藏复杂性并仅向用户显示功能。 抽象类用于提供抽象。 尽管它不提供100%抽象,因为它也可以有具体的方法。 让我们看看抽象类如何用于提供抽象。

abstract class Vehicle

{

public abstract void engine();

}

public class Car extends Vehicle {

public void engine()

{

System.out.println("Car engine");

// car engine implementation

}

public static void main(String[] args)

{

Vehicle v = new Car();

v.engine();

}

}

Car engine

汽车引擎

Here by casting instance of Car type to Vehicle reference, we are hiding the complexity of Car type under Vehicle. Now the Vehicle reference can be used to provide the implementation but it will hide the actual implementation process.

在这里,通过将Car类型的实例转换为Vehicle参考,我们隐藏了Vehicle下Car类型的复杂性。 现在,可以使用Vehicle参考提供实现,但是它将隐藏实际的实现过程。

何时使用抽象方法和抽象类? (When to use Abstract Methods & Abstract Class?)

Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.

通常在需要两个或多个子类通过不同实现以不同方式执行相似操作的地方声明抽象方法。 这些子类扩展了相同的Abstract类,并为abstract方法提供了不同的实现。

Abstract classes are used to define generic types of behaviors at the top of an object-oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class.

抽象类用于在面向对象的编程类层次结构的顶部定义行为的通用类型,并使用其子类提供抽象类的实现细节。

翻译自: https://www.studytonight.com/java/abstract-class.php

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值