【发布时间】:2015-07-08 19:14:02
【问题描述】:
我有一个 APIRequest 类,我需要一个 isInBackground 变量的默认值。
APIRequest 类简化:
public class APIRequest
{
public string url;
public bool isInBackground = true;
}
当我这样做时,isInBackground 的值总是被忽略并设置为默认值:
APIRequest rq = new APIRequest{
url = "/user/suggest/" + pseudo,
isInBackground = false
};
然后我记录该值并得到“真”,但是 url 是正确的值...
将其设置为 'False' 的唯一方法是这样做
rq.isInBackground = false;
那么这个值确实是'False'
所以我的问题是它为什么会这样?是不是因为 isInBackground 有默认值?
public class APITest {
public string url;
public bool background = true;
}
public class TestClass : MonoBehaviour {
// Use this for initialization
void Start () {
APITest t = new APITest{
url = "coucou",
background = false
};
Debug.Log(" url: " + t.url + " background: " + t.background);
}
}
编辑:尝试将默认值设置为“False”并将变量设置为“True”后,问题不会发生...
编辑以显示完整代码:
private void ShowPseudoSuggestionsFor(string pseudo){
Debug.Log("Get suggestions");
RemoteRequest r = RemoteRequest.GetNewRequest("PseudoSuggestions");
APIRequest rq = new APIRequest{
url = "/user/suggest/" + pseudo,
successCallback = OnSuggestionsLoaded,
successCallbackArgs = new Hashtable{
{ "pseudo", pseudo }
},
isInBackground = false
};
//rq.isInBackground = false;
Debug.Log("isInBackground : " + rq.isInBackground);
r.SetRequest(rq);
Debug.Log("isInBackground : " + rq.isInBackground);
r.SendRequest();
}
完整的 APIRequest 类:
public class APIRequest
{
public string url; /*!< Url you wish to send you request at */
public HTTPMethod method = HTTPMethod.GET; /*!< HTTP method (GET, POST, PUT, DELETE) */
public Dictionary<string, string> headers = new Dictionary<string, string>(); /*!< Headers, add one with headers.Add(<headerName>, <headerValue>) */
public byte[] payload; /*!< If you need to send POST or PUT data create a WWWForm, add the fields you need in it and then set payload to be <yourForm>.data */
public bool resendOnFailure = false; /*!< Should we resend the request if it fails ? */
public bool resendOnTimeout = false; /*!< Should we resend the request if it times out ? */
public bool isInBackground = true; /*!< Should we send request start and completed events */
public bool askBeforeResending = false; /*!< Should we ask user before resending request ? */
public Rect askingPopupRect; /*!< Size of the popup that will ask user to resend request */
public string askingPopupTitle; /*!< Title for resend request popup */
public string askingPopupMessage; /*!< Message for resend request popup */
public GUI.WindowFunction drawAskingPopup; /*!< Function responsible for drawing the resend request popup */
public float timeout = 10f; /*!< Time until the request is considered a failure */
public int maxAttempts = 1; /*!< Maximum number of attempts before which request will be resent, upon reaching it, no more attempts to resend will be made */
public RequestCallback successCallback; /*!< Function called when server responded with a status code 2XX (success) */
public RequestCallback failureCallback; /*!< Any other status code than 2XX will trigger this function (failure) */
public Hashtable failureCallbackArgs; /*!< This hashtable will be passed along to the 'successCallback' function as a mean to provide it with arguments */
public Hashtable successCallbackArgs; /*!< This hashtable will be passed along to the 'failureCallback' function as a mean to provide it with arguments */
/** Returns a string representation of the request that is human readable
* \return A string describing the request parameters
*/
public override string ToString(){
string result = "Request to url: " + url + " method: " + method.ToString () + "\nHeaders:\n";
foreach(KeyValuePair<string, string> kvp in headers){
result += kvp.Key + " -> " + kvp.Value + "\n";
}
result += "Params :\n";
result += "resendOnFailure: " + resendOnFailure + "\n";
result += "resendOnTimeout: " + resendOnTimeout + "\n";
result += "askBeforeResending: " + askBeforeResending + "\n";
result += "timeout: " + timeout + "\n";
result += "maxAttempts: " + maxAttempts + "\n";
result += "isInBackground: " + isInBackground + "\n";
return result;
}
}
【问题讨论】:
-
我最好的猜测是因为您使用的是字段而不是属性。
-
我说以前是struct,后来改成了class。而对于字符串,对不起,我输入了它而不是复制并在这一点上犯了一个错误
-
@musefan 这不是真的;使用对象初始化器时,参数是可选的。
-
@GeoffreyHug 您需要展示一个完整的程序来重现您描述的问题。您的程序不仅无法编译,而且不会重现您声称的问题。
-
这里他们说这是一个单一开发的问题:answers.unity3d.com/questions/59530/…
标签: c# object unity3d constructor