在Java中,为什么我不能继承Interfaces?
如果情况是接口没有提供方法的实现,那么我们如何能够从Abstract Classes继承.
我在以下实现中有错误.它说OnlineWebStore_Process无法实现IWebStore接口,因为它们没有匹配的返回类型.但该方法返回Item,它实现了在IWebStore接口中用作返回类型的IItem接口.什么是这个问题的好方法?
public interface IItem
{
string id { get; set; }
string name { get; set; }
}
public interface IWebStore
{
List<IItem> GetOnlineItems();
}
public class Item : IItem
{
public Item(string _id)
{
id = _id;
}
public string id { get; set; }
public string name { get; set; }
}
public class OnlineWebStore_Process : IWebStore
{
public List<Item> GetOnlineItems()
{
List<Item> items = new List<Item>();
return items
}
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个泛型类,它接受一些可比较的泛型类型的元素.所以我这样做:
public class Foo<T extends Comparable<T>>
在课堂上Foo我有类似的东西:
public void bar(T t)
我确信我可以写这样的代码:t.compareTo(v).
问题1:为什么在使用泛型时我们extends不implements使用接口?Comparable不是一个阶级.
\\
现在假设我想创建另一个与上面类似的类也实现该bar方法.我想创建这个界面:
public interface Face<T extends Comparable<T>> {
public void bar(T t);
}
Run Code Online (Sandbox Code Playgroud)
然后我改变类Foo来实现Face(public class Foo<T extends Comparable<T>> implements Face).
问题2:执行此操作时,我收到以下编译错误:
Foo类型的方法栏(T)必须覆盖或实现超类型方法.
为什么是这样?
当我告诉Eclipse add the unimplemented methods我得到:
public void bar(Comparable t)而不是... bar(T t).
我有一个针对自动机的许多状态的泛型类.它声明如下:
#ifndef STATE_H_
#define STATE_H_
#include "Automat.h"
class State {
public:
virtual void readChar(char c, Automat* automat) = 0;
virtual ~State(){};
};
#endif /* STATE_H_ */
Run Code Online (Sandbox Code Playgroud)
我在eclipse中遇到这个错误:
此行有多个标记
我的自动贩卖机如下:
#ifndef Automat_H_
#define Automat_H_
#include "../../Scanner/src/IScanner.h"
#include "./States/State.h"
class Automat {
public:
int count;
State* current;
IScanner* scanner;
Automat(IScanner *s);
void readChar(char c);
void setState(State *s);
void error();
~Automat();
};
#endif /* Automat_H_ */
Run Code Online (Sandbox Code Playgroud)
最后实现了Automat,我将省略一些方法.
#include "Automat.h"
#include "./States/StartState.h"
Automat::Automat(IScanner *s) {
current = …Run Code Online (Sandbox Code Playgroud) 我为Control DLL创建了一个DLL加载器(这些控件具有相同的接口iControlPlugin),一切都很好用.但是我现在需要为设备驱动程序创建一个DLL加载器.我开始查看我的Control Loader代码,并意识到代码是相同的,除非定义了接口类型.因此,我想使我的控制加载器功能更通用,以便它可以用于不同的接口类型.
我修改了函数,以便它返回一个字典,我遇到的问题是我做了一个检查,以确保DLL文件包含匹配的类型,在我的情况下,这是两种接口类型之一.但我总是无效.
这是我原来的工作功能,专门针对一种类型(iLPPControlPlugin)
public Dictionary<string, iLPPControlPlugin> LoadPlugins(string folder)
{
/* Create a Dictionary object to store the loaded DLL's */
Dictionary<string, iLPPControlPlugin> plugsins = new Dictionary<string,iLPPControlPlugin>();
/* Get all the DLL files from the path specified */
string[] files = Directory.GetFiles(folder, "*.dll");
/* Go through each DLL... */
foreach (string file in files)
{
/* Load the file, as an assembly. */
Assembly assembly = Assembly.LoadFile(file);
/* Get the type, can be more than one */
var types …Run Code Online (Sandbox Code Playgroud) 我试图创建一个泛型类,它接收基于接口的泛型参数,代码是异常
所以我测试了,我有以下内容
interface InterfaceA { }
interface InterfaceB<T> where T:InterfaceA { }
class ClassA : InterfaceA { }
class ClassB : InterfaceB<ClassA> { }
Run Code Online (Sandbox Code Playgroud)
基本测试
InterfaceB<InterfaceA> my_object_1 = new ClassB(); // Not Working (Compile error)
InterfaceB<InterfaceA> my_object_2 = (InterfaceB<InterfaceA>)new ClassB(); // Not Working (Throw Exception)
InterfaceB<InterfaceA> my_object_3 = new ClassB() as InterfaceB<InterfaceA>; // Working but still null
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么my_object_1,my_object_2和my_object_3出现问题?
我知道静态意味着,内存中只有一个实例.我知道final意味着它不能被改变或子类化,我也知道在java接口中定义的任何变量都是静态final
所以现在问题是,为什么我可以在"XFace"类的界面"MyFace"中超越最终的静态变量"a"?
例:
public interface MyFace {
static final int a = 15;
void smile();
}
Run Code Online (Sandbox Code Playgroud)
然后在课堂上,我可以轻松地骑过a,与当地人一起,
public class XFace implements MyFace {
@Override
public void smile() {
int a=3; // over riding interface's a variable and suprsingly it works !
System.out.println(a*2); // will print 6
}
Run Code Online (Sandbox Code Playgroud)
为什么我可以在smile()方法中定义int a = 3?是不是"一个"最终和静态?它怎么可能被覆盖?
我有以下要求,
对不起,我找不到一个简单的方法来定义我的问题.感觉就像Spring框架提供的解决方案.但我需要知道如何从Java/C#应用程序中实现这一点.
List接口允许我们在索引处使用get()方法获取对象.
我们如何在像LinkedHashSet这样的set接口中获取特定索引处的对象
我正在使用laravel orm组件,并在部署生命服务器时观察到以下内容.
我收到了错误
Declaration of Illuminate\Database\Eloquent\Collection::toJson()
must be compatible
with Illuminate\Support\Contracts\JsonableInterface::toJson($options = 0)
Run Code Online (Sandbox Code Playgroud)
我部署在安装了php v5.4.6的ubuntu服务器上.在安装了php v5.3.5的本地机器上工作时没有得到这个.
接口声明如下
public function toJson($options = 0);
Run Code Online (Sandbox Code Playgroud)
而抽象类的实现是
public function toJson()
{
return json_encode($this->toArray());
}
Run Code Online (Sandbox Code Playgroud)
我不认为这会是一个问题,因为接口声明的参数是可选的,抽象类实现它而不需要参数.
这是php v5.4的一个东西,它提供了什么优势?
另外,解决这个问题的最佳方法是什么?接口是由很多类实现的,所以我试图不破坏依赖; 你会建议在生命服务器上降级我的php版本吗?