简体   繁体   English

如何使用Java模仿PHP匿名类?

[英]How to mimic PHP anonymous class using Java?

I would like to implement a strong encapsulated version of object composition using Java. 我想使用Java实现对象组合的强大封装版本。 My current code in PHP is the follow: 我当前在PHP中的代码如下:

<?php

class Email {
    private $subject;
    private $body;
    private $recipient;

    public function __construct() {
        $this->subject = new class {
            public $text;
        };
        $this->body = new class {
            public $text;
            public $format;
        };
        $this->recipient = new class {
            public $address;
        };
    }
    public function setSubject($text){
        $this->subject->text = $text;
    }
    //more getters & setters...
}

I have tried this in Java, but without success: 我已经用Java尝试过,但是没有成功:

public class Email {

    private Subject      subject;
    private Body         body;
    private Recipient    recipient;

    public Email() {
        this.subject = new Object() {
            public String text;
        };
        this.body = new Object() {
            public String text;
            public String format;
        };
        this.recipient = new Object() {
            public String address;
        };
    }

public void setText(String text){
      this.body.text = text;
    }
//more getters & setters...

}

JDK error saying that Object class does not contain "text" property. JDK错误,指出Object类不包含“文本”属性。 So, is it possible to use anonymous classes in Java like in PHP (without classes names?) 因此,是否有可能像PHP中那样在Java中使用匿名类(没有类名?)

Thanks in advance. 提前致谢。

So I'm no PHP developer -- I did PHP 5 once and that was enough -- so I'm not 100% on all the aspects of what PHP supports in this case. 所以我不是PHP开发人员-我只做过一次PHP 5,就足够了-因此,在这种情况下,我对PHP支持的所有方面都不是100%。

However, I think what you want are private inner classes. 但是,我认为您想要的是私有内部类。 A public class can have private inner classes that only it has access to. 公共类可以具有只有其才能访问的私有内部类。 So, for your Email example ... 因此,对于您的电子邮件示例...

public class Email {
  private Subject      subject;
  private Body         body;
  private Recipient    recipient;

  public Email() {
    this.subject = new Subject();
    this.body = new Body();
    this.recipient = new Recipient();
  }

  public void setText(String text){
    this.body.text = text;
  }
  //more getters & setters...

  private class Subject {
    public String text;
  }
  private class Body {
    public String text;
    public String format;
  }
  private class Recipient {
    public String address;
  }
}

Since Subject, Body, Recipient are private inner classes, external classes can't see them. 由于Subject,Body和Recipient是私有内部类,因此外部类看不到它们。 So you can't return an instance of, say, Body and expect some external class to be able to do work on it because the external class can't even see its definition to import it. 因此,您无法返回例如Body的实例,并期望某些外部类能够对其进行处理,因为外部类甚至看不到其定义来导入它。 But if your getters and setters fully encompass all read/write operators that should be fine for your use case. 但是,如果您的getter和setter完全包含所有读/写运算符,则对您的用例来说应该是不错的选择。

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

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