Java Encapsulation Concept不清楚 [英] Java Encapsulation Concept not clear

查看:81
本文介绍了Java Encapsulation Concept不清楚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是基本问题,但我仍然不理解封装概念。我不明白如何从其他类更改类的属性。因为每当
我们尝试设置类的公共实例值时,我们必须创建该类的对象然后设置value.and每个对象引用不同的内存。即使我们更改
实例值,这也不会影响任何其他对象。

This is basic question but still i don't understand encapsulation concept . I did't understand how can we change the properties of class from other class.because whenever we try to set the public instance value of class we have to create object of that class and then set the value.and every object refer to different memory.so even if we change the instance value this will not impact to any other object.

即使我尝试使用静态公共实例进行更改价值也是我无法更改类属性值。

Even I try to change using static public instance value also i am not able to change the class property value.

示例如下:

// Employee class
public class Employee {
    public static int empid;
    public static String empname;

    public static void main(String[] args) {
        System.out.println("print employe details:"+empid+" "+empname);
    }

    // EmployeeTest  class
    public class EmployeeTest {

        public static void main(String[] args) {
            Employee e = new Employee();
            e.empid=20;
            e.empname="jerry";
            Employee.empid=10;
            Employee.empname="tom";
        }

    }
}

每次我运行员工类我得到相同的值

Every time I run Employee class I am getting same value

print employees details:0 null

尽管我没有遵循封装概念,但我无法更改员工类的公共实例值。请帮助我理解这个概念我错的地方。

Even though I am not following encapsulation concept and I am not able to change public instance value of employee class.Please help me to understand the concept where i am going wrong.

推荐答案

是的,有时这可能有点令人困惑。让我们一步一步:首先,您需要了解

Yeah, this can be a little confusing sometimes. Let's go step by step: First, you need to understand


  • 什么是封装,为什么要使用它。?

  • What is encapsulation and why is it used.?

封装是四个基本OOP概念之一。封装是制作技巧私有类中的字段,并通过公共方法提供对字段的访问。如果字段被声明为私有,则类外的任何人都无法访问该字段,从而将字段隐藏在类中。出于这个原因,封装也被称为数据隐藏。

Encapsulation is one of the four fundamental OOP concepts.Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

封装可以被描述为一个保护屏障,阻止其他人随机访问代码和数据在课外定义的代码。通过接口严格控制对数据和代码的访问。

Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.

封装的主要好处是能够在不破坏代码的情况下修改我们实现的代码使用我们代码的其他人。使用此功能封装为我们的代码提供了可维护性,灵活性和可扩展性

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

举一个小例子:

public class EncapTest{

   private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}

以上方法称为Accessors(又名getter和setter) )。现在你可能会问,

The above methods are called Accessors(aka getters and setters). Now you might ask,


  • 为什么要使用访问器..?
    实际上有很多考虑使用访问器而不是直接暴露类的字段的好理由.Getter和Setters使API更稳定。

例如,考虑一个类中的字段public,由其他类访问。现在稍后,您希望在获取和设置变量时添加任何额外的逻辑。这将影响使用API​​的现有客户端。因此,对此公共字段的任何更改都需要更改引用它的每个类。相反,使用访问器方法,可以轻松添加一些逻辑,如缓存一些数据,稍后懒洋洋地初始化它。此外,如果新值与先前值不同,则可以触发属性更改事件。所有这些都将与使用访问器方法获取价值的类无缝连接。

For instance, consider a field public in a class which is accessed by other classes. Now later on, you want to add any extra logic while getting and setting the variable. This will impact the existing client that uses the API. So any changes to this public field will require change to each class that refers it. On the contrary, with accessor methods, one can easily add some logic like cache some data, lazily initialize it later. Moreover, one can fire a property changed event if the new value is different from the previous value. All this will be seamless to the class that gets value using accessor method.

关于它们如何以及它们是什么,有很多教程和解释。谷歌他们。

There are so many tutorials and explanations as to how and what are they. Google them.

至于你当前的问题:


  1. 你有两个不同的班级,每班都有一个主要的。那是错的。他们将拥有不同的属性。

  2. @Subhrajyoti建议的代码更改Majumder是正确的。检查解决问题的答案。

在此期间,请阅读

  • Encapsulation
  • Accessors

以便更好地理解这些概念。希望能帮助到你。 :)

for a better understanding of the concepts. Hope it helps. :)

这篇关于Java Encapsulation Concept不清楚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆