То, каков правильный синтаксис для, "является" переменным методом считывания/методами set в классе POJO?

var uri = new Uri("http://localhost/SOAP/SOAPSMS.asmx/add");

var req = (HttpWebRequest) WebRequest.CreateDefault(uri); 
req.ContentType = "text/xml; charset=utf-8"; 
req.Method = "POST"; 
req.Accept = "text/xml"; 
req.Headers.Add("SOAPAction", "http://localhost/SOAP/SOAPSMS.asmx/add"); 

var strSoapMessage = @"<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
               xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
               xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
  <soap:Body><add xmlns='http://tempuri.org/'><a>23</a><b>5</b></soap:Body>
</soap:Envelope>"; 

using (var stream = new StreamWriter(req.GetRequestStream(), Encoding.UTF8)) 
    stream.Write(strSoapMessage); 
15
задан Nik Reiman 13 July 2009 в 08:24
поделиться

7 ответов

One reason for using properties is to decouple the API from the implementation. In other words, you shouldn't feel bound by what your private variable is called. That shouldn't inform the naming beyond trying to keep it readable to code maintainers.

I would say that if "type" is boolean in this case, then the second form is correct. If it's not boolean, you should use getXXX - but I probably wouldn't use getIsXXX. To me, "is" has a very strong correspondence with Boolean properties, and using it in other contexts would not only break the JavaBeans conventions (which could affect other tools) but would be misleading IMO.

20
ответ дан 1 December 2019 в 01:45
поделиться

Wouldn't say there's a strong convention for POJOs, but for JavaBeans the second (IntelliJ) example is the standard for boolean attributes, everything else uses getX.

3
ответ дан 1 December 2019 в 01:45
поделиться

I would also choose your second option. The first, with getIsBlah() seems wordy and redundant.

2
ответ дан 1 December 2019 в 01:45
поделиться

Есть одна большая проблема с синтаксисом «is», если вы используете JSTL, а именно то, что JSTL EL не распознает их. Это довольно глупо, но разработчики JSTL EL не удосужились проверить свою логику на соответствие javabeans.

Я часто замечаю, что пишу методы getIsBlah () в своих классах уровня представления, которые вызывают isBlah () только для того, чтобы дайте JSTL зацепку. Это ужасно.

3
ответ дан 1 December 2019 в 01:45
поделиться

Note that the name of the field is completely irrelevant to the JavaBean specification. Only the names of the getter/setter are relevant.

Normally the name of the getter is get(). Only for boolean properties is is() allowed as an alternative.

Note that in your example the Bean property name is "Blah" when you call the getter isBlah() and it's "IsBlah" when you call your getter getIsBlah().

Personally I usually prefer isBlah().

4
ответ дан 1 December 2019 в 01:45
поделиться

И «get», и «is» на самом деле хороши, поскольку технически они все еще приемлемы в соглашение JavaBeans. Я бы выбрал то, что звучит лучше или естественнее, в зависимости от того, какое слово на самом деле ваше «Бла».

1
ответ дан 1 December 2019 в 01:45
поделиться

JSTL допускает isMyBool только в том случае, если это логическое значение, а не логическое значение или любой другой объект в соответствии со спецификацией компонента. (примитив против объекта).

0
ответ дан 1 December 2019 в 01:45
поделиться
Другие вопросы по тегам:

Похожие вопросы: