casting

Type casting into byte in Java

倖福魔咒の 提交于 2019-12-12 09:44:13
问题 I am a beginner in Java. I came across a concept called Type Casting. I have the following snippet- class Demo { byte b; int a=257; double d= 323.142 b=(byte)a; System.out.println(b); b=(byte)d; System.out.println(b); } The output for the code is- 1 67 Can anybody explain me the outputs. Thanks in Advance! 回答1: The byte type is encoded on 8 bits, so it takes its values between -128 and 127. In your case, casting by byte is the same as computing a modulo and rounding to an int . Try the

Cast derived class to base class in php

一世执手 提交于 2019-12-12 09:41:54
问题 I'm probably missing something obvious here as this is basic functionality in other OOP languages, but I'm struggling with PHP way of doing this. I understand that PHP is not "true" OOP language, but still... What I'm after is casting an object instantiated as derived class to base class. Something along the lines of: class Some{} class SomeMock extends Some {} function GetSomeMock(){ $mock = new SomeMock(); return (Some)$mock; //Syntax error here } I've found some questions with strange

Cannot convert from Func<T,T,T> to Func<T,T,T>

橙三吉。 提交于 2019-12-12 09:01:46
问题 I'm quite confused by this error: Cannot implicitly convert type 'System.Func<T,T,T> [c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll]' to 'System.Func<T,T,T> [c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll]' path\to\my\project\Operators.cs The types are identical, why is it even trying to do a cast? Here's the code: public static class Operators<T> { private static Func<T,T,T> _add = null; public

numpy: syntax/idiom to cast (n,) array to a (n, 1) array?

半世苍凉 提交于 2019-12-12 08:50:00
问题 I'd like to cast a numpy ndarray object of shape ( n ,) into one having shape ( n , 1). The best I've come up with is to roll my own _to_col function: def _to_col(a): return a.reshape((a.size, 1)) But it is hard for me to believe that such a ubiquitous operation is not already built into numpy's syntax. I figure that I just have not been able to hit upon the right Google search to find it. 回答1: I'd use the following: a[:,np.newaxis] An alternative (but perhaps slightly less clear) way to

Why cast “extern puts” to a function pointer “(void(*)(char*))&puts”?

让人想犯罪 __ 提交于 2019-12-12 08:47:56
问题 I'm looking at example abo3.c from Insecure Programming and I'm not grokking the casting in the example below. Could someone enlighten me? int main(int argv,char **argc) { extern system,puts; void (*fn)(char*)=(void(*)(char*))&system; char buf[256]; fn=(void(*)(char*))&puts; strcpy(buf,argc[1]); fn(argc[2]); exit(1); } So - what's with the casting for system and puts? They both return an int so why cast it to void? I'd really appreciate an explanation of the whole program to put it in

Swift: Unable to downcast AnyObject to SKPhysicsBody

断了今生、忘了曾经 提交于 2019-12-12 08:43:02
问题 Apple has the following method in the SKPhysicsBody class. /* Returns an array of all SKPhysicsBodies currently in contact with this one */ func allContactedBodies() -> [AnyObject]! I noticed it returns an array of AnyObject. So I read about how to deal with down casting AnyObject Here I want to loop through the allContactedBodies array of my physics body. The problem is, no matter what I try I just can't get things to work. I tried this first: for body in self.physicsBody.allContactedBodies(

IQueryable<a> to ObservableCollection<a> where a = anonymous type

∥☆過路亽.° 提交于 2019-12-12 08:35:05
问题 I want the datacontext of my listview to be binded to an observable collection. Right now I have: // CurrentEmploye = some employee Entities.DatabaseModel m = new Entities.DatabaseModel(); var q = from t in m.TimeSheet join emp in m.Employees on t.idEmployee equals emp.id where emp.id == CurrentEmploye.id select new { firstName = emp.firstName, lastName = emp.lastName, position = emp.position, clockInDate = t.clockInDate, clockOutDate = t.clockOutDate, }; listView1.DataContext = q; that code

How to cast a struct to Anyobject in Swift 2?

我们两清 提交于 2019-12-12 08:30:53
问题 I have, struct S {} Along with, func y (x: S) -> AnyObject {} In Swift 2, is it possible to, within y(), return x My current code: struct S {let value: Int = 0} let x = S() func y(x: S) -> AnyObject {return x} Yields the following error: return expression of type 'S' does not conform to type 'AnyObject' Is there a way to mitigate this? 回答1: A Struct cannot conform to AnyObject . It can only conform to Any 来源: https://stackoverflow.com/questions/33921907/how-to-cast-a-struct-to-anyobject-in

Converting float to decimal in SQL Server 2008

怎甘沉沦 提交于 2019-12-12 08:27:03
问题 I have a view which needs to return type decimal for columns stored as float. I can cast each column to decimal as follows: , CAST(Field1 as decimal) Field1 The problem with this approach, is that decimal defaults to 18,0, which automatically rounds the float columns to 0. I would like to keep a precision of up to 12 decimal places. However, if I do this: , CAST(Field1 as decimal(12,12)) Field1 I get a runtime error: "Arithmetic overflow error converting float to data type numeric" the float

Other ways of verifying reflect.Type for int and float64

a 夏天 提交于 2019-12-12 08:17:05
问题 In golang, a number in JSON message is always parsed into float64. In order to detect if it is actually integer, I am using reflect.TypeOf() to check its type. Unfortunately there is no constant that represents reflect.Type . intType := reflect.TypeOf(0) floatType := reflect.TypeOf(0.0) myType := reflect.TypeOf(myVar) if myType == intType { // do something } Is there more elegant solution instead of using 0 or 0.0 to get reflect.Type ? 回答1: You may also use the Value.Kind() or Type.Kind()