interface

Does Java have plan that default method (java8) Substitute for Abstract Class?

╄→гoц情女王★ 提交于 2019-12-10 11:13:50
问题 Does Java have plan that default method substitute for Abstract Class ? I could not find a real case to use default method instead of Abstract? 回答1: Default methods can't substitute abstract classes, as abstract classes can (and often do) have fields. Interfaces can only contain behaviour and not state, which is unlikely to change in the future as multiple inheritance of state in Java is seen (rightly or wrongly) as evil. They can also have final methods, which is another thing you can't

Cast concrete class to generic base interface

吃可爱长大的小学妹 提交于 2019-12-10 11:09:47
问题 Here's the scenario i am faced with: public abstract class Record { } public abstract class TableRecord : Record { } public abstract class LookupTableRecord : TableRecord { } public sealed class UserRecord : LookupTableRecord { } public abstract class DataAccessLayer<TRecord> : IDataAccessLayer<TRecord> where TRecord : Record, new() { } public abstract class TableDataAccessLayer<TTableRecord> : DataAccessLayer<TTableRecord>, ITableDataAccessLayer<TTableRecord> where TTableRecord : TableRecord

How to make this class generic? (.NET C#)

匆匆过客 提交于 2019-12-10 10:56:27
问题 My class has the following core: class SmartDbConnection { private readonly IDbConnection Connection; public SmartDbConnection(string ConnectionString) { if(ConnectionString.Contains("MultipleActiveResultSets=true")) { Connection = new SqlConnection(ConnectionString); } } } I don't want it to have "SqlConnection" hardcoded. So I thought in making it a Generic class (accepting IDbConnection classes). But I don't know how to do it. Anyone can help? 回答1: First - I've added IDisposable to this,

Linq - Casting IQueryable to IList returns null - WHY?

二次信任 提交于 2019-12-10 10:48:44
问题 I have to be missing something obvious here. I don't get why this cast of the results of a linq query returns null and not the typed list I'm requesting. IList<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>; The complete code to run this is below. This is a knowledge gap I need to bridge. I have tried all kinds of permutations of casts to get it to work. I get no Exceptions, just a null. Of note is that the Linq query is selecting its results into instances of my custom

How to implement callbacks with interface

北城以北 提交于 2019-12-10 10:32:40
问题 I have a class which needs more callbacks.. I am trying to implement them with an interface: class CallbacksInterface { public: virtual bool mycallback1() = 0; virtual bool mycallback2() = 0; virtual bool mycallback3() = 0; }; Class BusImplementation{ public: addRequest(bool (CallbacksInterface::*callback)()); } Callback is parameter for addRequest() method and is defined as pointer to interface method . So I want to add request.. //class with callbacks class Main:CallbacksInterface{ public:

declare parameter subtype in Java interface, use subtypes in Java implementing methods

偶尔善良 提交于 2019-12-10 10:18:03
问题 I want to declare a method in an interface where the parameter of the method defined in implementing classes can be a subtype of a specific java class for example: interface Processor{ processRequest( Request r); } public class SpecialRequest extends Request{...} public class SpecialProcessor implements Processor{ processRequest(SpecialRequest r){...} } but I get errors in the SpecialProcessor because it doesn't properly implement the Processor interface. What can I change in the Processor

Generic interface overloading for methods?

冷暖自知 提交于 2019-12-10 09:54:50
问题 Is there a good, generic, way to do the following without resorting to a second method or lots of casts - I want to keep the API as light as possible and it seems ok to me OO-wise: class Foo { public T Bar<T>() where T: IAlpha { /* blahblahblah */ } public T Bar<T>() where T: IBeta { /* blahblahblah */ } } interface IAlpha { string x {set;} } interface IBeta { string y {set;} } thanks 回答1: You can't overload a method by return value only (generic or not). Additionally, it would be impossible

Casting between Interfaces and Classes

北战南征 提交于 2019-12-10 09:52:33
问题 Yesterday I just started learning interfaces and have been doing some simple examples, I noticed that I have had lots of trouble understanding casting between classes and interfaces so I read Java Cast Interface to Class as well as Interfaces Oracle Java Tutorials However when my book gave a small review question at the bottom, I noticed that I still didn't really understand completely, and the book I bought doesent have solutions. The following is the question ( I gave my attempt and

Why Implement the IEquatable<T> Interface

≯℡__Kan透↙ 提交于 2019-12-10 09:48:33
问题 I have been reading articles and understand interfaces to an extent however, if i wanted to right my own custom Equals method, it seems I can do this without implementing the IEquatable Interface. An example. using System; using System.Collections; using System.ComponentModel; namespace ProviderJSONConverter.Data.Components { public class Address : IEquatable<Address> { public string address { get; set; } [DefaultValue("")] public string address_2 { get; set; } public string city { get; set;

Adding Plugin Support : Interface or Base class to inherit?

十年热恋 提交于 2019-12-10 09:46:32
问题 I'm adding plugin support to my .NET application. A base class sounds reasonable to me, so people can inherit it and override certain calls as well can keep the default functionality or can use some internal helper functions. Why would I choose interface instead of a base plugin class to inherit? Could you tell which design I should choose and why? 回答1: You should consider taking a look at the System.Addin (MAF) namespace or the Managed Extensibility Framework (MEF). MEF would be the