简体   繁体   English

实用示例封装与信息隐藏与抽象与数据隐藏在Java中

[英]Practical example Encapsulation vs Information Hiding vs Abstraction vs Data Hiding in Java

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. 我知道有很多关于这个问题的帖子,它有实时例子的理论解释。这些OOP术语非常简单,但对于像我这样的初学者来说更加困惑。 But I am expecting here not a definition and real time example BUT expecting code snippet in java . 我期待这里不是一个定义和实时的例子但是期待java中的代码片段

Will anyone please give very small code snippet for each one in Java that will help me a lot to understand Encapsulation vs Information Hiding vs Abstraction vs Data Hiding practically? 请问任何人都可以为Java中的每一个提供非常小的代码片段 ,这将有助于我理解封装与信息隐藏与抽象与数据隐藏的实际关系吗?

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 Car type. 这些实现都是唯一的,但可以绑定在Car类型下。

To reduce the confusion: 为了减少混淆:

Encapsulation is 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. Java中的所有对象都有一组数据和方法来操作该数据。 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. 这样您就可以隐藏信息和其他复杂性。

Example: Any Java object is enough to represent an example. 示例:任何Java对象都足以代表示例。

Abstraction : This means making things general ie, 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 {}类Lion扩展Animal {}

So here for Lion class you have a generalized class ie, Animal. 所以对于Lion类,你有一个通用类,即Animal。 This represents abstraction 这代表抽象

Note Examples givien by KepaniHaole are perfect. 注意 KepaniHaole的例子givien是完美的。

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 Lion and Cow classes implements the Animal interface. 在此示例中, LionCow类实现了Animal接口。 The Lion and Cow classes override the getType method of the Animal interface. LionCowoverrideAnimal接口的getType方法。

Here Lion and Cow are special cases and Animal is more generalized. 狮子和牛是特殊情况,动物更普遍。 So this gives you abstraction because whenever you have an Animal you have the getType method to know its type ie, you have generalized it. 所以这给你抽象,因为每当你有一个Animal你有getType方法来知道它的类型,即你已经推广它。

Now if you notice I have made the animalType as private in the Lion and Cow classes so that nobody outside the class can modify it. 现在,如果你注意到我已经在LionCow类中将animalType设为私有,那么班级以外的人都无法修改它。 This way I am hiding unwanted information from outer objects. 这样我就可以从外部对象中隐藏不需要的信息。

All the outer objects need is the getType method to known the type of the animal. 所有外部对象都需要getType方法来知道动物的类型 This way I am exposing only relavent information to outer objects. 这样我只向外部对象公开相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM