Java 中封装 vs 信息隐藏 vs 抽象 vs 数据隐藏的实际例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19944938/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:44:41  来源:igfitidea点击:

Practical example Encapsulation vs Information Hiding vs Abstraction vs Data Hiding in Java

javaoopabstractioninformation-hidingdata-hiding

提问by Prashant Shilimkar

I know there are lots of post regarding this question which has theoretical explanation with real time examples.These OOPs terms are very simple but more confusing for beginners like me. But I am expecting here not a definition and real time example BUT expecting code snippet in java.

我知道有很多关于这个问题的帖子,其中有实时示例的理论解释。这些 OOP 术语非常简单,但对于像我这样的初学者来说更容易混淆。但我在这里期待的不是定义和实时示例,而是期待 java 中的代码片段

Will anyone please give very small code snippetfor each one in Javathat will help me a lot to understand Encapsulation vs Information Hiding vs Abstraction vs Data Hiding practically?

有没有人请为Java中的每一个提供非常小的代码片段,这将有助于我在实践中理解封装、信息隐藏、抽象、数据隐藏?

采纳答案by yamafontes

Encapsulation = information hiding = data hiding. Information that doesn't need to be known to others in order to perform some task.

封装=信息隐藏=数据隐藏。执行某些任务不需要其他人知道的信息。

class Girl {
  private int age;
  Girl(int age) {
    this.age = age;
  }
  public boolean willGoOutWithGuy(boolean isGuyUgly) {
    return (age >= 22) && (!isGuyUgly);
  }
}

class Guy {
  private Girl girl = new Girl();
  private boolean isUgly = true;
  public boolean willGirlGoOutWithMe() {
    return girl.willGoOutWithGuy(isUgly);
  }
  // Guy doesn't have access to Girl's age. but he can ask her out. 
}

Abstraction = different implementations of the same interface.

抽象 = 同一接口的不同实现。

public interface Car {
  public void start();
  public void stop();
}

class HotRod implements Car {
  // implement methods
}

class BattleTank implements Car {
  // implement methods
}

class GoCart implements Car {
  // implement methods
}

The implementations are all unique, but can be bound under the Cartype.

实现都是唯一的,但可以绑定在Car类型下。

回答by me_digvijay

To reduce the confusion:

为了减少混乱:

Encapsulationis used for Information hiding or data hiding

封装用于信息隐藏或数据隐藏

Encapsulation means self contained. All the objects in Java have a set of data and methods to operate on that data. So the user of any object does not have to worry about the about how the obect is working. This way you hide the information and other complexities.

封装意味着自包含。Java 中的所有对象都有一组数据和方法来操作这些数据。所以任何对象的用户不必担心对象是如何工作的。通过这种方式,您可以隐藏信息和其他复杂性。

Example:Any Java object is enough to represent an example.

示例:任何 Java 对象都足以表示一个示例。

Abstraction: This means making things general i.e., instead of creating a very specfic class when you create base classes or interfaces and then extend them to get your specific class.

抽象:这意味着使事情变得通用,即,当您创建基类或接口然后扩展它们以获得您的特定类时,而不是创建一个非常特定的类。

Example:class Animal {} class Lion extends Animal{}

示例:class Animal {} class Lion extends Animal{}

So here for Lion class you have a generalized class i.e., Animal. This represents abstraction

因此,对于 Lion 类,您有一个广义类,即 Animal。这代表抽象

NoteExamples givien by KepaniHaole are perfect.

注意KepaniHaole 给出的例子是完美的。

Abstraction Example:

抽象示例:

public interface Animal{
    public String getType();
}

class Lion implements Animal {
    private String animalType = "WILD";

    @Override
    public String getType() {
        return this.animalType;
    }
}
class Cow implements Animal {
    private String animalType = "Domestic";

    @Override
    public String getType() {
        return this.animalType;
    }
}

In this example the Lionand Cowclasses implements the Animalinterface. The Lionand Cowclasses overridethe getTypemethod of the Animalinterface.

在这个例子中,LionCow类实现了Animal接口。的狮子override的getType所述的方法动物接口。

Here Lion and Cow are special cases and Animal is more generalized. So this gives you abstraction because whenever you have an Animalyou have the getTypemethod to know its type i.e., you have generalized it.

这里 Lion 和 Cow 是特例,Animal 是更普遍的情况。所以这给了你抽象,因为每当你有一个Animal你有getType方法来知道它的类型,即,你已经概括了它。

Now if you notice I have made the animalTypeas private in the Lionand Cowclasses so that nobody outside the class can modify it. This way I am hiding unwanted information from outer objects.

现在,如果您注意到我在LionCow类中将AnimalType设为私有,以便类外的任何人都不能修改它。这样我就隐藏了外部对象不需要的信息。

All the outer objects need is the getTypemethod to known the typeof the animal. This way I am exposing only relavent information to outer objects.

所有外部对象需要的是getType方法来知道动物的类型。这样我只将相关信息暴露给外部对象。