interface

How can I use interface as a C# generic type constraint?

一曲冷凌霜 提交于 2019-12-17 05:39:09
问题 Is there a way to get the following function declaration? public bool Foo<T>() where T : interface; ie. where T is an interface type (similar to where T : class , and struct ). Currently I've settled for: public bool Foo<T>() where T : IBase; Where IBase is defined as an empty interface that is inherited by all my custom interfaces... Not ideal, but it should work... Why can't you define that a generic type must be an interface? For what it's worth, I want this because Foo is doing reflection

Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?

无人久伴 提交于 2019-12-17 05:16:18
问题 A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain. For example, given the interface: public interface IAnything { void m1(); void m2(); void m3(); } the following abstract class gets merrily compiled without a warning or an error: public abstract class AbstractThing implements

Java getting an error for implementing interface method with weaker access

北慕城南 提交于 2019-12-17 05:04:26
问题 When I compile this code: interface Rideable { String getGait(); } public class Camel implements Rideable { int x = 2; public static void main(String[] args) { new Camel().go(8); } void go(int speed) { System.out.println((++speed * x++) + this.getGait()); } String getGait() { return " mph, lope"; } } I get the following error: Camel.java:13: error: getGait() in Camel cannot implement getGait() in Rideable String getGait() { ^ attempting to assign weaker access privileges; was public 1 error

Finding SSID of a wireless network with Java

孤者浪人 提交于 2019-12-17 04:32:55
问题 We're doing a project coded in Java (compiled for JRE 1.6) and need some help with a little but apparently complicated feature: We want to do a certain action when a specific wireless network is connected e.g. when the connected SSID=="myNetworkAtHome" or similar. After looking through this site, google and the Java documentation we have come a little closer. After looking at the code here: http://download.oracle.com/javase/tutorial/networking/nifs/retrieving.html It seems we were getting

Why are C# 4 optional parameters defined on interface not enforced on implementing class?

假如想象 提交于 2019-12-17 04:12:22
问题 I noticed that with the optional parameters in C# 4 if you specify a parameter as optional on an interface you DON'T have to make that parameter optional on any implementing class: public interface MyInterface { void TestMethod(bool flag = false); } public class MyClass : MyInterface { public void TestMethod(bool flag) { Console.WriteLine(flag); } } and therefore: var obj = new MyClass(); obj.TestMethod(); // compiler error var obj2 = new MyClass() as MyInterface; obj2.TestMethod(); // prints

Interface or abstract class?

自作多情 提交于 2019-12-17 03:20:09
问题 For my new Pet-Project I have a question for design, that is decided already, but I want some other opinions on that too. I have two classes (simplified): class MyObject { string name {get;set;} enum relation {get;set;} int value {get;set;} } class MyObjectGroup { string name {get;set;} enum relation {get;set;} int value {get;set;} List<MyObject> myobjects {get;set;} } Later in the Project MyObjectGroup and MyObject should be used equally. For this I could go two ways: Create an interface:

How to Implement IComparable interface?

北慕城南 提交于 2019-12-17 03:04:43
问题 I am populating an array with instances of a class: BankAccount[] a; . . . a = new BankAccount[] { new BankAccount("George Smith", 500m), new BankAccount("Sid Zimmerman", 300m) }; Once I populate this array, I would like to sort it by balance amounts. In order to do that, I would like to be able to check whether each element is sortable using IComparable . I need to do this using interfaces. So far I have the following code: public interface IComparable { decimal CompareTo(BankAccount obj); }

Inner class within Interface

醉酒当歌 提交于 2019-12-17 02:41:47
问题 Is it possible to create an inner class within an interface? If it is possible why would we want to create an inner class like that since we are not going to create any interface objects? Do these inner classes help in any development process? 回答1: Yes, you can create both a nested class or an inner class inside a Java interface (note that contrarily to popular belief there's no such thing as an " static inner class ": this simply makes no sense, there's nothing "inner" and no "outter" class

Java - declaring from Interface type instead of Class

杀马特。学长 韩版系。学妹 提交于 2019-12-17 02:28:36
问题 In my quest to correctly grasp Interface best practices, I have noticed declarations such as: List<String> myList = new ArrayList<String>(); instead of ArrayList<String> myList = new ArrayList<String>(); -To my understanding the reason is because it allows flexibility in case one day you do not want to implement an ArrayList but maybe another type of list. With this logic, I set up an example: public class InterfaceTest { public static void main(String[] args) { PetInterface p = new Cat(); p

When should one use interfaces?

非 Y 不嫁゛ 提交于 2019-12-17 02:26:07
问题 I know that an interface does not have a body just a method definition. But when should I use interfaces? If I provide someone a set of interfaces with no body, why would they feel a need to write the function body? Would they be better off writing their own abstract class with abstract methods in it. Edited: I guess the use of Interfaces is more when you are a part of a team. Suppose Team A writes a code for something and they wanted to see if a call to a method. with name getRecords(), is