java java中数据抽象和过程抽象的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39597256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Data abstraction and procedural abstraction in java
提问by avg998877
I am trying to understand the difference between Data abstraction and procedural abstraction in java.
我试图理解 java 中数据抽象和过程抽象之间的区别。
I understand the procedural abstraction is making use of methods that accept formal parameters and hiding the implementation from the user. This totally makes sense in PL/SQL
language as I have an option of creating procedures separately in PL/SQL
.
我理解过程抽象是利用接受形式参数的方法并向用户隐藏实现。这在PL/SQL
语言中完全有意义,因为我可以选择在PL/SQL
.
But I get confused between Data abstraction and Procedural abstraction, as even the procedures in Java are implemented as part of some class. And every class can also have some static methods(public static
) which every other class in the package can use.
但是我在数据抽象和过程抽象之间感到困惑,因为即使是 Java 中的过程也是作为某个类的一部分实现的。每个类也可以有一些静态方法(public static
),包中的每个其他类都可以使用。
Please can someone give a clear distinction between these two(in terms of java)
请有人可以明确区分这两者(就java而言)
回答by appsdownload
In Procedural Abstraction, methods are used to capture the procedural patterns, abstracting over behaviour. For example, in Java, you could write something like:
在程序抽象中,方法用于捕获程序模式,对行为进行抽象。例如,在 Java 中,您可以编写如下内容:
public void printFibonacci(int n){
//your code to print Fibonacci numbers
}
In this way, users could use the procedure easily, without even having to wonder what's going on inside the function. You know that it will print the fibonacci for the given input.
通过这种方式,用户可以轻松使用该程序,甚至不必想知道函数内部发生了什么。您知道它将打印给定输入的斐波那契数列。
On the other hand, in Data Abstraction, Classes are used to abstract the related stateful values and their associated behaviours -- also called a s Abstract Data Type (ADT).
For example, in Java, it consists of:
另一方面,在数据抽象中,类用于抽象相关的有状态值及其相关行为——也称为抽象数据类型 (ADT)。
例如,在 Java 中,它包括:
- Interface classes
- The allowable behaviours
- 接口类
- 允许的行为
回答by Sakalya
In Object Oriented Programming, abstraction is one of the major pillars. In Java, when it comes to data abstraction, it means while designing/defining the classes itself, you need to identify only those attributes of class which are relevant to that domain. For example, if Person is an entity, it can have many attributes such as first name, surname, salary, spouse, age, height, weight, blood group. But if you are developing an application for healthcare domain, then you want to choose only those attributes which are related to healthcare (e.g. height, blood group) and ignore the rest. On the other hand, if you are developing an application for social survey, these attributes are not required. In this way, identifying the required attributes and ignoring the rest is the data abstraction.
在面向对象编程中,抽象是主要支柱之一。在 Java 中,当谈到数据抽象时,这意味着在设计/定义类本身时,您只需要识别与该域相关的类的那些属性。例如,如果 Person 是一个实体,它可以有很多属性,例如名字、姓氏、薪水、配偶、年龄、身高、体重、血型。但是,如果您正在为医疗保健领域开发应用程序,那么您只想选择与医疗保健相关的那些属性(例如身高、血型)而忽略其余属性。另一方面,如果您正在开发用于社会调查的应用程序,则不需要这些属性。这样,识别所需的属性并忽略其余的就是数据抽象。
As for the procedural abstraction, the necessary part is "What the procedure does and ignoring how it does it". Most of the languages by default support it. In Java, one class can call methods of other class without knowing its implementation details. This is one example of procedural abstraction.
至于过程抽象,必要的部分是“过程做了什么而忽略它是怎么做的”。默认情况下,大多数语言都支持它。在 Java 中,一个类可以在不知道其实现细节的情况下调用另一个类的方法。这是过程抽象的一个例子。