简体   繁体   English

如何使用封装?

[英]How to use encapsulation?

After I read online E-book.They said the benefit of encapsulation is "A class can change the data type of a field and users of the class do not need to change any of their code." 在我阅读在线电子书之后。他们说封装的好处是"A class can change the data type of a field and users of the class do not need to change any of their code." . I don't understand what they say in the point. 我不明白他们在说什么。 What is the main meaning of the point? 该点的主要含义是什么? Can you give an example,please? 请给一个例子吗?

Let's take a simple class Vehicles, which maintains a list: 让我们看一个简单的Vehicles类,它维护一个列表:

public class Vehicles {

    private ArrayList<String> vehicleNames;

    Vehicles() {
        vehicleNames = new ArrayList<String>();
    }

    public void add(String vehicleName) {
        vehicleNames.add(vehicleName);
    }
}

This will be used by a client in the following way: 客户端将通过以下方式使用它:

public class Client {

    Public static void main(String []args) {
        Vehicles vehicles = new Vehicles();
        vehicles.add("Toyota");
        vehicles.add("Mazda");
    }
}

Now if Vehicles changes its internal private vehicleNames field to be a LinkedList instead, Client would be unaffected. 现在,如果Vehicles将其内部私有的vehicleNames字段更改为LinkedList ,则Client将不受影响。 That is what the book is talking about, that the user/client does not need to make any changes to account for the changes in the class due to encapsulation. 这就是本书在谈论的内容,即由于封装,用户/客户端不需要进行任何更改即可解决类中的更改。

Encapsulation is really important in Object-Oriented Programming. 封装在面向对象的编程中非常重要。 Using encapsulation, you can hide information from users who use your class library/API. 使用封装,您可以向使用您的类库/ API的用户隐藏信息。

"And why do I need to hide stuff from the users?", you ask. 你问:“为什么我要向用户隐藏东西?” There are a lot of reason. 原因很多。 One main reason is that some users who are naughty or just don't know what the API is doing may mess with your classes and stuff. 一个主要原因是,有些顽皮的用户或者只是不知道API在做什么的用户可能会弄​​乱您的类和东西。 Let me give you an example. 让我给你举个例子。

Suppose you have a class here: 假设您在这里有一堂课:

public class Computer {
    public int coreCount;
}

As you can see here, coreCount is declared public . 如您所见, coreCount被声明为public That means all other classes can access it. 这意味着所有其他类都可以访问它。 Now imagine a naughty person do this: 现在想象一个顽皮的人这样做:

Computer myPC = new Computer ();
myPC.coreCount = 0;

Even fools can tell that this doesn't make any sense. 即使是傻瓜也可以说这没有任何意义。 It might also affect your program's other stuff. 它也可能会影响程序的其他内容。 Imagine you want to divide by the core count. 假设您想除以核心数量。 An Exception would occur. 将会发生异常。 So to prevent this, we should create setters and getters and mark the field private . 因此,为防止这种情况,我们应该创建setter和getter并将字段标记为private

C# Version: C#版本:

public class Computer {
    private int coreCount;
    public int CoreCount {
        get {return coreCount;}
        set {
            if (value > 0)
                coreCount = value;
        }
    }
}

Java version Java版本

public class Computer {
    private int coreCount;
    public int getCoreCount () {return coreCount;}
    public void setCoreCount (int value) {
        if (value > 0)
            coreCount = value;
}

Now no one can set the core count to non-positive values! 现在,没有人可以将核心计数设置为非正值!

Here's an example of encapsulation. 这是封装的示例。 Say we have a Person class, like so 说我们有一个Person类,就像这样

class Person {
  private String name;
  private String email;
  public String getName() { return this.name; }
  public String getEmail() { return this.email; }

  public void setName(String name) { this.name = name; }
  public void setEmail(String email) { this.email = email; }
}

And at some point, we decide we need to store these values not as a couple strings, but as a HashMap (for some reason or another). 并且在某个时候,我们决定我们需要将这些值存储为HashMap (出于某种原因或其他原因),而不是将其存储为一对字符串。

We can change our internal representation without modifying the public interface of our Person class like so 我们可以更改内部表示,而无需像这样修改Person类的公共接口。

class Person {
  HashMap<String, String> data;
  public Person() {
    this.data= new HashMap<String, String>();
  }
  public String getName() { return this.data.get("name"); }
  public String getEmail() { return this.data.get("email"); }

  public void setName(String name) { this.data.put("name", name); }
  public void setEmail(String email) { this.data.put("email", email); }
}

And from the client code perspective, we can still get and set Strings name and email without worrying about anything else. 从客户端代码的角度来看,我们仍然可以获取和设置Strings名称和电子邮件,而无需担心其他任何事情。

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

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