转换类型(Conversion type)
我有以下问题。
我有一门课“Instellingen”,这是另外三门课的一门课public class Instellingen { private int _ID; } public class Class1: Button { private Instellingen _Instellingen; } public class Class2 : Label { private Instellingen _Instellingen; } public class Class3 : TextBox { private Instellingen _Instellingen; }
如果我有另一个班级,那就使用其他班级(但它可以是这三个班级中的任何班级)我是否必须使用一个开关? 还是有更简单的方法?
public class AnotherClass { public AnotherClass () { GetInstellingenFromClass(new Class1()); GetInstellingenFromClass(new Class2()); GetInstellingenFromClass(new Class3()); } private void GetInstellingenFromClass(Control c) { switch (c.GetType.ToString()) { case "Class1": Class1 klasse = (Class1) c; //Do something with the _Instellingen of this class break; case "Class2": Class2 klasse2 = (Class2) c; //Do something with the _Instellingen of this class break; case "Class3": Class3 klasse3 = (Class3)c; //Do something with the _Instellingen of this class break; } } }
(是否存在某些东西,所以我可以像c._Instellingen那样做 - >而不是先将它转换为正确的类型,这与c的类型无关?)
我希望你能理解我的问题。
谢谢
I've got the following question.
I've got one class "Instellingen" that's a field in 3 other classespublic class Instellingen { private int _ID; } public class Class1: Button { private Instellingen _Instellingen; } public class Class2 : Label { private Instellingen _Instellingen; } public class Class3 : TextBox { private Instellingen _Instellingen; }
If I've got another class, that uses the other classes (but it can be anyone of this 3 classes) Do i have to use a switch? or is there an easier way?
public class AnotherClass { public AnotherClass () { GetInstellingenFromClass(new Class1()); GetInstellingenFromClass(new Class2()); GetInstellingenFromClass(new Class3()); } private void GetInstellingenFromClass(Control c) { switch (c.GetType.ToString()) { case "Class1": Class1 klasse = (Class1) c; //Do something with the _Instellingen of this class break; case "Class2": Class2 klasse2 = (Class2) c; //Do something with the _Instellingen of this class break; case "Class3": Class3 klasse3 = (Class3)c; //Do something with the _Instellingen of this class break; } } }
(does there exists something so i can just do something like c._Instellingen --> without converting it first to the right type, where it doesn't matter what type c is?)
I hope you understand my question.
Thanks
原文:https://stackoverflow.com/questions/2507989
最满意答案
你所描述的是一种竞争条件。
.subscribe()
内的箭头函数是一个回调函数,这意味着它在HTTP get返回后执行。 然而,这个函数是非阻塞的,所以你的代码的其余部分继续执行。 因此,当您尝试console.log
时,可能无法设置this.gifs
。要解决这个问题,您应该使用一些反应数据类型(如Promises或RxJS),这样您只有在设置了
this.gifs
之后才能获得它。What you are describing is a race condition. The arrow function inside
.subscribe()
is a callback function, meaning it is executed after the HTTP get returns. However, this function is non-blocking, so the rest of your code continues to execute. Thus,this.gifs
may not be set when you try toconsole.log
it.To remedy this, you should use some reactive data type (like Promises or RxJS) so that you can get the value of
this.gifs
only after it has been set.
相关问答
更多-
为什么在向Giphy API发出请求时会收到HTTP Error 403?(Why do I get a HTTP Error 403 when making a request to the Giphy API?)[2022-06-12]
可能是因为您有一个免费帐户,他们只能访问网站白名单。 如果您希望我们将网站添加到白名单,请向support@pythonanywhere.com发送请求或使用反馈表单。 在请求中包含指向API文档的链接。 Probably because you have a free account and they only have access to a whitelist of sites. If you'd like us to add a site to the whitelist, send a reque ... -
我发现他们不允许在这里 。 我通过在我的slackbot文件中使用superagent npm for ajax和giphy api来解决这个问题,如下所示: var url = 'https://api.giphy.com/v1/gifs/search?q=' + 'searchString' + '&api_key=dc6zaTOxFJmzC&rating=r'; request.get(url).end( function(err, res) { // console.log(res. ...
-
尝试将其更改为此。 “data”是对象中的关键名称,使用它两次会让人感到困惑。 success: function(response) { //console.log("This works too") //debugger console.log(response.data[0].bitly_url); } Try changing it to this. "data" is key name within the object and gets confusing using ...
-
无法调用GIPHY API(Unable to call GIPHY API)[2023-08-05]
问题在于jsfiddle上的url 。 如果您检查控制台,您将看到以下消息: 阻止加载混合活动内容“ http://api.giphy.com/v1/gifs/search?q=test&api_key=dc6zaTOxFJmzC ” 这是什么意思,你问? 这意味着你从https调用http 。 仅用于测试,您可以将url更改为//api.giphy.com/v1/gifs/search?q=" + $('#srcCriteria').val() + "&api_key=dc6zaTOxFJmzC 你会看到 ... -
这两个选项 尽可能快地完成所有请求,但是要连续处理结果 $.when(...chapter.split(" ").map(word => { var queryURL = "http://api.giphy.com/v1/gifs/search?q=" + word + "&api_key=
"; return $.ajax({ url: queryURL, method: 'GET' }); })).then((...respo ... -
你所描述的是一种竞争条件。 .subscribe()内的箭头函数是一个回调函数,这意味着它在HTTP get返回后执行。 然而,这个函数是非阻塞的,所以你的代码的其余部分继续执行。 因此,当您尝试console.log时,可能无法设置this.gifs 。 要解决这个问题,您应该使用一些反应数据类型(如Promises或RxJS),这样您只有在设置了this.gifs之后才能获得它。 What you are describing is a race condition. The arrow functio ...
-
问题解决了,我能够与Facebook分享器共享GIF。 例如,链接如下, https://www.facebook.com/sharer/sharer.php?u=http://example.com/gif 。 因此,在我的应用程序中,当Facebook的机器人尝试输入我的链接时 - http://example.com/gif我只是通过将它直接写入输出流来返回gif图像。 如果您返回实际的GIF网址或Feed反馈:图片元标记,则无法使用。 所以秘密就是直接将gif写入输出流。 以下是如何处理它的java ...
-
你正在寻找一个 Map
在你的@Get界面中,你会收到它: (@QueryMap Map parameters) 所以你的整个界面调用将是这样的: @GET("gifs") Call getGifsByID(@QueryMap Map parameters); 如果你想查看它,我写了一个关于如何使用Retrofit 2的完整教程: http : //toomanytutorials ... -
需要onclick从giphy api调用在图片和gif之间切换(Need onclick to toggle between image and gif from giphy api call)[2023-12-30]
我认为你需要将gif url设置为你使用jQuery创建的img元素的属性。 就像是: `killerImg.attr("data-gif", gif);` 看到你已经定义var gif = respData.images.fixed_height_small.url; 。 您可能还想给它一个唯一的ID,如: killerImg.attr("id", "killer-img"); 然后,在您的点击事件中,您可以从元素本身检索该属性: var tempUrl = $("#killer-img").attr( ... -
对象中的数据字段是一个数组,因此您需要放置索引以访问内容,即 data = JSON.parse(body) console.log data[0].images The data field in your object is an Array so you need to put the index in order to access the content, i.e. data = JSON.parse(body) console.log data[0].images