这是一个一般性的设计问题.我们经常使用接口来解耦组件,写入接口而不是实现等.有时使用基本注入技术的接口,例如,
interface IMyInterface
{
void DoSomething();
}
static class IMyInterfaceFactory
{
public static IMyInterface GetInstance()
{
return new MyInterfaceInstance();
}
}
class IMyInterfaceConsumer
{
IMyInterface mInterface;
public IMyInterfaceConsumer()
{
this.mInterface = IMyInterfaceFactory.GetInstance();
}
public void UseTheInterface()
{
this.mInterface.DoSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于使用var关键字.甚至没有使用真正的C#界面,但仍然在设计意义上创建一个"界面",
static class IMyInterfaceFactory
{
// of course, this doesnt need to be a single instance
static MyInterfaceInstance mSingleInstance;
// no longer programming to the interface, just returning the instance
public static MyInterfaceInstance GetInstance()
{
// null coalesce
return mSingleInstance …Run Code Online (Sandbox Code Playgroud) 朋友们,我在Java中遇到了一个问题:我想实现一个结构,但是我遇到了一些困难,任何人都可以帮助我.
interface samp1{
method1()
method2()
method3()
}
interface samp2{
method4()
method5()
}
class Samp implements samp1,samp2
{
// this class needs only method1 from interface samp1 and method 4 from interface samp2
// I don't want to override all the methods from interface
}
Run Code Online (Sandbox Code Playgroud)
谁能为此提出一些解决方案?
有没有可用的设计模式?如果是,请提供参考链接.
提前致谢.
我试图理解为什么这段代码不能编译.
我有一个实现接口的类.由于某种原因,最后一个方法无法编译.
它不会简单地允许我将集合转换为集合,但允许它返回单个对象.
有人可以向我解释为什么会这样吗?谢谢.
public class Testing2 {
public SortedSet<ITesting> iTests = new TreeSet<ITesting>();
public SortedSet<Testing> tests = new TreeSet<Testing>();
public ITesting iTest = null;
public ITesting test = new Testing();
// Returns the implementing class as expected
public ITesting getITesting(){
return this.test;
}
// This method will not compile
// Type mismatch: cannot convert from SortedSet<Testing> to SortedSet<ITesting>
public SortedSet<ITesting> getITests(){
return this.tests;
}
}
Run Code Online (Sandbox Code Playgroud) 我希望我的类实现一个接口,并从Auditable表中获取其他属性.我可以两个都做吗?我试图在这里做,但我的IDE中出现错误.
public partial class ObjectiveDetail : IEquatable<ObjectiveDetail>, AuditableTable
{
...
}
public abstract class AuditableTable : IAuditableTable
{
...
}
Run Code Online (Sandbox Code Playgroud) 我可以在接口中声明函数和属性,但我无法实现它们.当我从界面继承时,我现在必须在我的班级中实现所有这些成员.如果我必须这样做,那么声明界面有什么意义呢?
例如,这是一个示例界面:
namespace InterfaceExample
{
public interface IVehicle
{
int Doors { get; set; }
int Wheels { get; set; }
Color VehicleColor { get; set; }
int TopSpeed { get; set; }
int Cylinders { get; set; }
int CurrentSpeed { get; }
string DisplayTopSpeed();
void Accelerate(int step);
}
}
Run Code Online (Sandbox Code Playgroud)
我已在此界面中声明了所有属性和函数.现在,当我在我的班级中使用这个界面时:
namespace InterfaceExample
{
public class Motorcycle : IVehicle
{
private int _currentSpeed = 0;
public int Doors { get; set; }
public int Wheels { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一个我认为类型为"[] interface {}"的变量
这是代码:
var s string
switch value1 := value1.(type) {
case int:
s = strconv.Itoa(value1)
case float64:
s = strconv.FormatFloat(value1, 'f', 0, 64)
//case array:
//fmt.Printf("array")
default :
fmt.Printf("\nvalue=v+%",value1)
}
Run Code Online (Sandbox Code Playgroud)
输出是:
value=v+%!(NOVERB)%!(EXTRA []interface {}=
Run Code Online (Sandbox Code Playgroud) 我试图通过接口实现next()方法,但它给了我一个错误.这是我有的:
private class MyIterator implements Iterator<Term>
{
private final Polynomial myArray;
private int current;
MyIterator(Polynomial myArray) {
this.myArray = myArray;
this.current = myArray.degree;
}
@Override
public boolean hasNext() {
return current < myArray.degree;
}
@Override
public Integer next() { //this method right here does not work
if (! hasNext()) throw new UnsupportedOperationException();;
return myArray.coeff[current++];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
Run Code Online (Sandbox Code Playgroud)
next()方法然后抛出这个错误:

这是我的界面:
public interface Term {
int coeff();
int exp();
String toString();
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是为什么接口不允许MyIterator实现next()方法
这两个类声明之间是否有任何差异
1:
class MyClass <T extends Number & Comparable>
Run Code Online (Sandbox Code Playgroud)
2:
class MyClass <T extends Number & Comparable<T>>
Run Code Online (Sandbox Code Playgroud)
我认为存在差异.但我找不到一个会显示差异的例子,因为我不完全理解.
你能告诉我这个例子吗?
我不知道我是否缺少一些明显的方法/属性,但在这里是:我有一个ProgressBar,默认情况下其属性IsIndeterminate为true。但是我只希望它在一定时期内是动画。我该如何实现?我尝试了类似的东西:
SomeBackgroundWorker.DoWork += SomeBackgroundWorker_DoWork;
pgbStatus.IsIndeterminate = true;
btnUpdate.IsEnabled = false;
SomeBackgroundWorker.RunWorkerAsync();
btnUpdate.IsEnabled = true;
pgbStatus.IsIndeterminate = false;
Run Code Online (Sandbox Code Playgroud)
但是,那不会动。很抱歉,如果它是重复的(我有感觉),但是我找不到任何答案。
您可以compareTo在不实现Comparable接口的情况下在类中定义方法
.实现Comparable
界面有什么好处?