casting

extending superclass and ClassCastException

左心房为你撑大大i 提交于 2019-12-22 08:37:11
问题 I have a superclass, which two methods i want to override. Here's my code: public class MyCustomClass extends SomeSuperClass { protected MyCustomClass(params) { super(params); } @Override public void method1() { super.method1(); /* here goes my code */ } @Override public void method2() { super.method2(); /* here goes my another code */ } I have some constructor, that passes SomeSuperClass object as a parameter, and what i do next: MyCustomClass object; /* now i have object of type

Golang - conversion between structs

早过忘川 提交于 2019-12-22 08:35:13
问题 I have two structs type A struct { a int b string } type B struct { A c string // more fields } I'd like to convert variable of type A to type B (A has defined only basic fields that are crucial for some parts, B on the other hand contains 'full' data). Is it possible in Go, or do I have to copy fields manually (or create a method A.GetB() or something like this and use this to convert A to B)? 回答1: By converting do you mean this: func main() { // create structA of type A structA := A{a: 42,

Cast CGFloat to Int in extension BinaryFloatingPoint

梦想与她 提交于 2019-12-22 08:11:41
问题 FIXED IN SWIFT 4.2 Details Xcode 9.2, Swift 4 Code extension BinaryFloatingPoint { func toInt() -> Int { // Eror if remove this block (I do not know why) // if let value = self as? CGFloat { // return Int(value) // } return Int(self) } } Error occurrence let float: Float = 2.38945 print("Float: \(float.toInt())") // No Error let double = Double(float) print("Double: \(double.toInt())") // No Error let cgfloat = CGFloat(float) print("CGFloat(): \(Int(cgfloat))") // No Error print(".toInt(): \

How to convert a string to an enum?

限于喜欢 提交于 2019-12-22 08:06:26
问题 My original approach was to create method called to_str() which will return a slice, but I am not sure that it is correct approach because this code doesn't compile. enum WSType { ACK, REQUEST, RESPONSE, } impl WSType { fn to_str(&self) -> &str { match self { ACK => "ACK", REQUEST => "REQUEST", RESPONSE => "RESPONSE", } } } fn main() { let val = "ACK"; // test match val { ACK.to_str() => println!("ack"), REQUEST.to_str() => println!("ack"), RESPONSE.to_str() => println!("ack"), _ => println!(

DynamicObject implicit casting

僤鯓⒐⒋嵵緔 提交于 2019-12-22 06:39:50
问题 I have a subclass of DynamicObject and I would like to implement implicit casting for primitive types similarly as DO's explicit casting method TryConvert; that is, without writing multiple implicit operator [type] functions. Usage: dynamic myDynamicObject = new MyDynamicObject("1"); int sum = 1 + myDynamicObject; // instead of int i = 1 + (int)myDynamicObject; Is that possible and if so, how? 回答1: There are several things going on here. First, you are performing a binary operation. So, you

Casting in Java(interface and class)

浪子不回头ぞ 提交于 2019-12-22 06:35:21
问题 if: interface I{} class A implements I{} class B extends A{} class C extends B{} A a = new A(); B b = new B(); Why a = (B)(I)b; is correct but b = (B)(I)a; is false? I find casting to be very confusing, what is the best way to understand if I can down cast or up cast an object? 回答1: Your class hierarchy looks like this: Object x can be casted to class Y , if runtime type of x is subclass of Y . Or, in other words, if there is a path from runtime type of x to Y . By "runtime type" i mean the

1l for long, 1f for float, 1d for double, what about byte?

我们两清 提交于 2019-12-22 05:33:34
问题 1l for long, 1f for float, 1d for double, what about byte? long l = 1l; float f = 1f; double d = 1d; // byte b = 1?; What's the equivalent for byte ? Does it exist? 回答1: No, there is no suffix you can append to a numeric literal to make it a byte . See 3.10 Literals in the Java Language Specification. 回答2: You need to cast to byte like this: byte b = 1; b = (byte) 5; Since by default these numeric constant are treated as int in Java. 回答3: there is no suffix you can append a numeric literal

Need clarifications in C-style, reinterpret, and const casts

微笑、不失礼 提交于 2019-12-22 05:29:30
问题 Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly

Need clarifications in C-style, reinterpret, and const casts

落花浮王杯 提交于 2019-12-22 05:28:59
问题 Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly

Using MIDIPacketList in swift

扶醉桌前 提交于 2019-12-22 05:23:37
问题 I am looking at some of the examples of midi output using core midi. Specifically this question and this I have code that works based on these in objC and I now want to try to translate that to swift. the line I least understand is this one: MIDIPacketList* pktList = (MIDIPacketList*) pktBuffer; I read this as declaring a pointer pktlist of type MIDIPacketList and assigning it the value of pktBuffer, cast to the type MIDIPacketList I'm new to C and objC and this makes no sense to me.