PHP has a 100 function which outputs the internal contents of an object, showing an object's type and content.

例如:

class Person {
  private $firstName;
  private $lastName;

  public function __construct($firstName, $lastName) {
    $this->firstName = $firstName;
    $this->lastName = $lastName;
  }
}

$person = new Person('Jon', 'Smith');
var_dump($person);

将输出:

object(Person)#1 (2) {
  ["firstName:private"]=>
  string(3) "Jon"
  ["lastName:private"]=>
  string(5) "Smith"
}

What is the equivalent in Java that will do the same?

推荐答案

It is not quite as baked-in in Java, so you don't get this for free.它是按照惯例而不是语言 struct 来完成的.在所有数据传输类(甚至可能是in all classes you write...个)中,您应该实现一个合理的toString方法.因此,在这里您需要重写Person类中的toString()并返回所需的状态.

utilities available种方法可以帮助编写一个好的toString方法,或者大多数IDE都有一个自动toString()编写快捷方式.

Php相关问答推荐

自定义WooCommerce查询字符串仅显示前3个产品

使用正则表达式搜索两个子字符串(没有重叠/共享字符)

如何在php中生成包含第100秒的时间序列?

有没有可能从composer 过时的输出中隐藏不需要的主要版本?

如何在Laravel Model中自定义多个日期属性的日期格式?

Shopware 6插件:由于删除了配置密钥,更新后配置值不正确

如何批量处理消息总线中的消息

扩展 WooCommerce 类触发未捕获错误:未找到WC_Coupon类

Woocommerce 临时购物车税未根据第一个请求正确计算

服务器升级到新的mysql版本8.0.34后查询错误

Symfony:从控制器内部调用自定义命令

图像未在 Laravel Ajax Crud 中显示

Google Sheets PHP API - 添加多个选项卡时出现问题

Shopware 6 AuthControllerDecorator已弃用

根据购买产品的自定义字段替换WooCommerce添加到购物车按钮

Symfony 迁移判断器在我的工作中失控

将变量从中间件传递到控制器 Laravel

使用循环和引用而不是递归创建分层/父子数组

PHP 根据提供的日期确定下一个星期一还是星期四

Azure 应用服务中的 Wordpress 无法通过符号链接读取文件