GSON直接访问私人会员? [英] GSON directly accesses private members?

查看:153
本文介绍了GSON直接访问私人会员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


摘要:


GSON是否访问班级的私人会员直接不调用其getter方法来获取值?






遵循的Java规则是类B 有一个私人会员无法访问 A类没有getter / setter方法。



现在我正在与GSON合作开发一个项目,我觉得getter / setter方法被绕过[未使用,私有成员被直接访问]



嗯我只是一名学生,所以我可能会错过一些常见的工作。


A类




  public class A {
public static void main(String [] args){
B b = new B();
Gson gson = new GsonBuilder()。setPrettyPrinting()。create();
System.out.println(gson.toJson(b));
System.out.println( - end--);
}
}




B类




 公共类B {
private int a;

public B(){
a = 1;
}

public int getA(){
System.out.println(我被调用!);
返回10;
}

public void setA(int a){
this.a = a;
}
}

注意:Ba的值为1 ,如果我从A类访问Ba,一切按预期工作,我将getA()编码为总是返回10



不会让我这么做,直到这里都很好






GSON怀疑开始这里

 公共类A {
public static void main(String [] args){
B b = new B();
Gson gson = new GsonBuilder()。setPrettyPrinting()。create();
System.out.println(gson.toJson(b));
System.out.println( - end--);
}
}

现在由于Ba是私有的,它应该调用编码为始终返回10的getter方法但是得到的输出是


输出:




  {
a:1
}
--end--

所以换句话说,私有方法的getter方法永远不会被调用

解决方案

你所说的是封装。这是一个面向对象的概念,它限制了某些类成员的访问。



但是,Java还有另一种访问对象中值的方法。那就是反思。这允许对类进行更通用的访问。例如,在Gson的情况下,他们需要知道类中的每个字段,但是他们不能为现有的每个类编写方法。同样,他们也不能要求你为它编写JSON适配器。这违背了框架的要点。



反思。它允许Gson库有一个方法将任何对象转换为某些JSON。这很重要,因为Reflections库提供了一个API,用于获取类的私有成员而无需通过访问器和更改器。



额外阅读




  • 你应该看一下反射。它是一个非常强大的API。


Summary:

does GSON access private members of a class directly without invoking its getter methods to fetch the value ?


The rule of Java that is followed is if class B has a private member it cannot be accessed by class A without getter/setter methods.

Now I was working on a project with GSON where in I get a feel getter/setter methods are being bypassed [not used,and private member is directly accessed]

Well am just a student so i could possibly be missing some common working.

class A

public class A {
public static void main(String[] args) {
    B b = new B();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    System.out.println(gson.toJson(b));
    System.out.println("--end--");
}
}

class B

public class B {
private int a;

public B() {
    a = 1;
}

public int getA() {
    System.out.println("I am invoked !");
    return 10;
}

public void setA(int a) {
    this.a = a;
}
}

Note : B.a is assigned value of 1 , and I coded getA() to always return 10

if I access B.a from class A, EVERYTHING WORKS AS EXPECTED and it wont let me do it , all fine till here


GSON doubt starts here

public class A {
public static void main(String[] args) {
    B b = new B();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    System.out.println(gson.toJson(b));
    System.out.println("--end--");
}
}

now since B.a is private it is supposed to invoke the getter method that is coded to always return 10 BUT the output i get is

Output:

{
  "a": 1
}
--end--

so in other words my getter method for the private method is NEVER INVOKED

解决方案

What you're talking about is encapsulation. This is an Object Orientated concept that restricts the access of certain class members.

However, Java has another way of accessing the values in an object. That is, reflection. This allows for a more generic access to a class. For example, in the case of Gson, they need to know about each field in a class, but they can't write a method for every class in existence. Likewise, they can't ask you to write a JSON adapter for it. That defeats the point of the framework.

In comes Reflection. It allows the Gson library to have a single method that converts any object into some JSON. This is important because the Reflections library provides an API for getting at private members of a class without going through the accessors and mutators.

Extra Reading

  • You should have a look into Reflection. It is a very powerful API.

这篇关于GSON直接访问私人会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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