interface

Java Class and Interface Name Collision

蹲街弑〆低调 提交于 2020-12-13 04:16:49
问题 interface A { void print(); } class A implements A { public void print() { System.out.println("Hello"); } public static void main(String args[]) { A a=new A(); a.print(); } } When i am using this code then it is saying "duplicate class:A". Why so? Can I not have same class and interface name 回答1: You can't have a class and an interface with the same name because the Java language doesn't allow it. First of all, it's ambiguous. If you declare a variable like this: A a; What is the type of that

Java Class and Interface Name Collision

青春壹個敷衍的年華 提交于 2020-12-13 04:12:26
问题 interface A { void print(); } class A implements A { public void print() { System.out.println("Hello"); } public static void main(String args[]) { A a=new A(); a.print(); } } When i am using this code then it is saying "duplicate class:A". Why so? Can I not have same class and interface name 回答1: You can't have a class and an interface with the same name because the Java language doesn't allow it. First of all, it's ambiguous. If you declare a variable like this: A a; What is the type of that

Java generic Interface performance

二次信任 提交于 2020-12-08 06:25:23
问题 Simple question, but tricky answer I guess. Does using Generic Interfaces hurts performance? Example: public interface Stuff<T> { void hello(T var); } vs public interface Stuff { void hello(Integer var); <---- Integer used just as an example } My first thought is that it doesn't. Generics are just part of the language and the compiler will optimize it as though there were no generics (at least in this particular case of generic interfaces). Is this correct? 回答1: There is potential for a minor

How to use TypeScript to map a large object to a smaller interface?

冷暖自知 提交于 2020-12-05 11:44:01
问题 I have an object returned from a server that contains e.g. { lorem: 1, ipsa: [2,3], dolor: { sit: 'amet', consectetur: 'adipiscing'}, elit: [{you: 'get'}, {the: 'picture'}] } and a TypeScript interface of export interface smallerInterface { ipsa: number[]; elit: any[]; } I'm saving the returned object into IndexedDb, and don't want to save any fields that are not present on the interface. I have tried casting fullObject as smallerInterface and <smallerInterface>fullObject , but when saving

How to use TypeScript to map a large object to a smaller interface?

时光总嘲笑我的痴心妄想 提交于 2020-12-05 11:42:50
问题 I have an object returned from a server that contains e.g. { lorem: 1, ipsa: [2,3], dolor: { sit: 'amet', consectetur: 'adipiscing'}, elit: [{you: 'get'}, {the: 'picture'}] } and a TypeScript interface of export interface smallerInterface { ipsa: number[]; elit: any[]; } I'm saving the returned object into IndexedDb, and don't want to save any fields that are not present on the interface. I have tried casting fullObject as smallerInterface and <smallerInterface>fullObject , but when saving

How to determine the method set of an interface in Golang?

拥有回忆 提交于 2020-11-29 23:57:36
问题 How would one print the method set of the following interface? type Searcher interface { Search(query string) (found bool, err error) ListSearches() []string ClearSearches() (err error) } Such that Search ListSearches ClearSearches is printed out? (Without knowledge of a concrete type which implements it). 回答1: reflect package is the right tool for this.Using reflection one can get the type information of a variable without knowing the type before hand . Here is a code snippet showing how to

TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index

若如初见. 提交于 2020-11-29 03:57:27
问题 In TypeScript, I declare an interface like this: export default interface MyDTO { readonly num: string; readonly entitle: string; readonly trb: string; readonly ucr: string; readonly dcr: string; readonly udm?: string; readonly ddm?: string; } With a function, I would like to access the value of a property, whose name is contained in a variable. private doSomething(dto: MyDTO, property: string): any { let label: any; if (['dcr', 'ddm'].includes(property)) { label = doSomethingElse(dto

TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index

大兔子大兔子 提交于 2020-11-29 03:55:04
问题 In TypeScript, I declare an interface like this: export default interface MyDTO { readonly num: string; readonly entitle: string; readonly trb: string; readonly ucr: string; readonly dcr: string; readonly udm?: string; readonly ddm?: string; } With a function, I would like to access the value of a property, whose name is contained in a variable. private doSomething(dto: MyDTO, property: string): any { let label: any; if (['dcr', 'ddm'].includes(property)) { label = doSomethingElse(dto