此处将为大家介绍关于泛型奇数-我可以在地图中插入一个Long值它可以编译并且在运行时不会失败的详细内容,此外,我们还将为您介绍关于.NET是否可以在运行时编译和执行新代码?、c–如何在地图中插入结构?
此处将为大家介绍关于泛型奇数-我可以在地图中插入一个Long值 它可以编译并且在运行时不会失败的详细内容,此外,我们还将为您介绍关于.NET是否可以在运行时编译和执行新代码?、c – 如何在地图中插入结构?、c – 我可以在STL的地图中使用矢量吗?、c – 是否可以在运行时修改PYTHONPATH?的有用信息。
本文目录一览:- 泛型奇数-我可以在地图中插入一个Long值 它可以编译并且在运行时不会失败
- .NET是否可以在运行时编译和执行新代码?
- c – 如何在地图中插入结构?
- c – 我可以在STL的地图中使用矢量吗?
- c – 是否可以在运行时修改PYTHONPATH?
泛型奇数-我可以在地图中插入一个Long值 它可以编译并且在运行时不会失败
输入以下代码:
public static void main(String[] args) { HashMap<String, String> hashMap = new HashMap<>(); HashMap<String, Object> dataMap = new HashMap<>(); dataMap.put("longvalue", 5L); class TestMethodHolder { <T> T getValue(Map<String, Object> dataMap, String value) { return (T)dataMap.get(value); } } hashMap.put("test", new TestMethodHolder().<String>getValue(dataMap, "longvalue")); String value = hashMap.get("test"); // ClassCastException occurs HERE System.out.println(value); }
对我来说,这段代码可以编译并不奇怪,但是ClassCastException发生在get行而不是它上面的put行,尽管我确实对可能发生的事情有一个有根据的猜测。由于在运行时会擦除泛型类型,因此getValue()中的强制转换实际上从不会在运行时发生,并且实际上是对Object的强制转换。如果按以下方式实现该方法,则将发生运行时强制转换,并且将在放置行上失败(如预期的那样)。有人可以确认吗?
class TestMethodHolder { String getValue(Map<String, Object> dataMap, String value) { return (String)dataMap.get(value); } }
这是使用泛型的已知缺陷还是奇怪?那么在调用方法时使用<>表示法是不好的做法吗?
编辑:我正在使用默认的Oracle JDK 1.7_03。
上面另一个隐含的问题:原始getValue STILL的强制转换是否在运行时发生,但强制转换实际上是对对象的-
编译器是否足够聪明,可以完全消除这种强制转换?这可能解释了人们在运行ClassCastException时会注意到的区别。
答案1
小编典典编译器依赖于类型安全性进行假设并进行转换/优化。不幸的是,类型安全性可以通过未经检查的强制转换来破坏。如果您的程序包含不正确的未经检查的强制转换,则不清楚编译器应该做什么。理想情况下,它应在未检查的强制转换的确切点进行运行时检查,在您的示例中Object
为T
。但是由于擦除,这是不可能的,而这并不是类型系统的一部分。
在示例中的其他任何地方,类型都是健全的,因此编译器可以假定getValue()
确实返回aString
,无需再次检查。但是进行检查也是合法的,就像Eclipse编译器所做的那样(可能是因为它将返回值分配给String
本地temp变量)。
因此,有个坏消息是,如果您的程序包含不正确的未经检查的强制转换,则其行为是不确定的…。因此,请通过严格的推理确保所有未经检查的强制转换都是正确的。
一个好的做法是检查所有未选中的强制类型转换,以便您可以合法地取消选中未选中的警告。例如
<T> T getValue(Map<String, Object> dataMap, String value, Class<T> type) { Object value = dataMap.get(value); if(value!=null && !type.isInstance(value)) // check! throw new ClassCastException(); @SuppressWarning("unchecked") T t = (T)value; // this is safe, because we''ve just checked return t; }
看到我对类似问题的回答:Java中的惰性类转换?
.NET是否可以在运行时编译和执行新代码?
注意:数学表达式评估不是此问题的重点。 我想在.NET运行时编译并执行新代码。 话虽如此…
我想允许用户在文本框中输入以下任何方程式:
x = x / 2 * 0.07914x = x^2 / 5
并将该等式应用于输入数据点。输入的数据点由 x
表示,每个数据点由用户指定的方程式处理。我几年前就这样做了,但是我不喜欢该解决方案,因为它需要为每次计算都解析方程的文本:
float ApplyEquation (string equation, float dataPoint){ // parse the equation string and figure out how to do the math // lots of messy code here...}
当您处理大量数据点时,这会带来很多开销。我希望能够即时将方程式转换为一个函数,以便仅将其解析一次。它看起来像这样:
FunctionPointer foo = ConvertEquationToCode(equation);....x = foo(x); // I could then apply the equation to my incoming data like this
函数ConvertEquationToCode将解析方程式,并返回一个指向应用适当数学的函数的指针。
该应用程序基本上将在运行时编写新代码。.NET有可能吗?
答案1
小编典典是! 使用在Microsoft.CSharp,System.CodeDom.Compiler和System.Reflection命名空间中找到的方法。这是一个简单的控制台应用程序,它使用一个方法(“
Add42”)编译一个类(“
SomeClass”),然后允许您调用该方法。这是一个简单的示例,我对其进行了格式化,以防止滚动条出现在代码显示中。这只是为了演示在运行时编译和使用新代码。
using Microsoft.CSharp;using System;using System.CodeDom.Compiler;using System.Reflection;namespace RuntimeCompilationTest { class Program { static void Main(string[] args) { string sourceCode = @" public class SomeClass { public int Add42 (int parameter) { return parameter += 42; } }"; var compParms = new CompilerParameters{ GenerateExecutable = false, GenerateInMemory = true }; var csProvider = new CSharpCodeProvider(); CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, sourceCode); object typeInstance = compilerResults.CompiledAssembly.CreateInstance("SomeClass"); MethodInfo mi = typeInstance.GetType().GetMethod("Add42"); int methodOutput = (int)mi.Invoke(typeInstance, new object[] { 1 }); Console.WriteLine(methodOutput); Console.ReadLine(); } }}
c – 如何在地图中插入结构?
# include <iostream> # include <map> using namespace std; struct node {int test;}temp; int main() { temp.test = 24; int test = 30; map < node,bool > mymap1; map < int,bool > mymap2; //mymap1.insert(make_pair(temp,true)); mymap2.insert(make_pair(test,true)); return 0; }
解决方法
您只需要定义小于运算符,如下所示:
bool operator<(const node& n1,const node& n2) { return n1.test < n2.test; }
如果“test”数据成员是私有的,那么它也必须是节点结构的朋友(因为节点当前是结构,所以它现在是公共的).但是,我可能会这样做:
#include <map> class node { public: int gettest() const { return _test; } void setTest(int test) { _test = test; } private: int _test; }; bool operator<(const node& n1,const node& n2) { return n1.gettest() < n2.gettest(); } int main() { std::map<node,bool> foo; node n; n.setTest(25); foo[n] = true; return 0; }
c – 我可以在STL的地图中使用矢量吗?
map<string,vector<string>> mymap;
我认为这是适用的.
但是,它没有显示出来.
我试过了
map<string,vector<string>*> mymap;
然后就可以了
这是什么规则?
解决方法
map<string,vector<string> > mymap; ^ see the extra space
没有额外的空间,>>被解析为右移运算符.
规则已在C 0x中修改,无需额外空间.一些编译器(例如,Visual C 2008及更高版本)已经不需要额外的空间.
c – 是否可以在运行时修改PYTHONPATH?
http://docs.python.org/c-api/intro.html#embedding-python
但是,当我从Python-land打印sys.path时,它具有PYTHONPATH的原始内容,而不是我设置的内容.以下是我正在做的一个例子(使用Boost.Python):
int main(int argc,char* argv[]){ _putenv_s("PYTHONPATH","C:\\source\\\\modules"); Py_Initialize(); object main = import("__main__"); object global = (main.attr("__dict__")); exec("import sys\nprint sys.path"),global,global);}
PS – 我知道还有其他的方式来完成我的目标,但这不是我在问的.我想知道为什么Py_Initialize()在设置sys.path时不使用PYTHONPATH的当前值.或者也许我误解了应该如何工作?
解决方法
import syssys.path.append("C:\\source\\\\modules")
今天关于泛型奇数-我可以在地图中插入一个Long值 它可以编译并且在运行时不会失败的分享就到这里,希望大家有所收获,若想了解更多关于.NET是否可以在运行时编译和执行新代码?、c – 如何在地图中插入结构?、c – 我可以在STL的地图中使用矢量吗?、c – 是否可以在运行时修改PYTHONPATH?等相关知识,可以在本站进行查询。
本文标签: