标签: interface-implementation

XmlResourceParser实现

我需要从服务器动态加载xml布局.LayoutInflater具有使用XmlPullParser的inflate方法.我试过了,但它不起作用.

查看Android源代码,结果发现这些inflate方法是使用XmlResourceParser调用的.Android使用的实现是XmlBlock.Parser,但这不是公共API.

我可以使用XmlResourceParser公共实现吗?

android interface-implementation

6
推荐指数
1
解决办法
1192
查看次数

如何自动生成已实现接口的方法

在PhpStorm中是否有一种方法可以自动生成给定类正在实现的接口所需的空方法?

假设我们有一个带有3个方法的接口 - 在定义实现此接口的新类时 - 为所有必需方法自动生成容器的一些选项.

interface interface-implementation phpstorm

6
推荐指数
2
解决办法
4500
查看次数

PHP接口实现拒绝参数的子类

考虑一下:

class A{}

class B extends A{}

interface I{
 // expects object instanceof A
 function doSomething(A $a);
}

class C implements I
{
 // fails ????
 function doSomething(B $b){}
}
Run Code Online (Sandbox Code Playgroud)

在我的构思中,上述应该有效,但它并不像php那样拒绝那种要求第一个参数与接口(I)中定义的完全相同的类型(A)的实现.既然B是A的子类,我看不出问题是什么.我在这里错过了什么吗?

php interface subclass type-hinting interface-implementation

5
推荐指数
1
解决办法
2577
查看次数

基类实现基接口而派生/具体类实现扩展接口,为什么?

我正在读一本书"用.NET编写的.NET域驱动设计".

问题基于以下类图所示的场景:

图:http: //screencast.com/t/a9UULJVW0

在此图中,

A)IRepository接口由(抽象基类)RepositoryBase实现,而

B)IRepository接口也由接口ICompanyRepository(ICompanyRepository : IRepository)扩展.

C)ICompanyRepository由CompanyRepository实现,它派生自SQLRepositoryBase,它源自RepositoryBase(;如A点所述),实现IRepository,如果是ICompanyRepository则为父级.

D)我创建了一个接口ICompanyRepository的变量,它引用了clas CompanyRepository的对象,如下所示:

ICompanyRepository comRep = new Company Repository();
Run Code Online (Sandbox Code Playgroud)

现在,如果我用ICompanyRepository变量comRep调用Add()函数...

comRep.Add(); 
Run Code Online (Sandbox Code Playgroud)

然后调用RepositoryBase类(它是CompanyRepository的父级)中的Add()函数.

我的问题: 由于调用(抽象 - 基础)类"RepositoryBase"中的函数Add()被调用,确切的基于面向对象的规则/机制是什么?为方便起见,我在下面陈述两种可能的机制:(请告诉我下面两个中哪一个是正确的潜在机制)

机制-1 是否调用了基类"RepositoryBase"中的Add()函数,因为"RepositoryBase"实现了IRepoitory?(因此强制RepositoryBase类实现IRepository以调用Add())

要么

机制-2: 调用基类"RepositoryBase"中的Add()函数是因为CompanyRepository实现了ICompanyRepository,它实现了包含Add()函数定义的IRepository,这样当调用Add()函数(带有变量)ICompanyRepository时,它首先找到定义在ICompanyRepository中添加然后在父接口IRepository中然后跳转到CompanyRepository类以查找Add()函数的实现而没有找到Add()函数的定义它向上遍历到父类SQLRepositoryBase以查找Add()函数等等,并且它在RepositoryBase类中找到函数Add(),因此它调用RepositoryBase中的Add()函数.这意味着如果它在RepositoryBase的任何派生类中找到了Add()函数,它没有进一步向上(在父类中).所有这些也意味着,为了从派生类遍历类链中的父类只是为了找到Add()函数,RepositoryBase类真的不需要直接从IRepository继承吗?


在我的问题中还有其他的事情,我无法理解在我的案例中适用的OO规则如下所述:

在我的问题中有两个接口,一个是父级,即IRepository,另一个是扩展它,即ICompanyRepository.父接口IRepository包含Add()函数的定义,但不包含子接口ICopmanyRepository.

类Hierarchy"CompanyRepository"链中的最后派生类实现ICompanyRepository(CompanyRepository不实现IRepository接口的Add()函数)而root(最顶层父)(抽象基础)类即RepositoryBase实现Add()函数.

所以结构就像http://screencast.com/t/a9UULJVW0中显示的图像.

现在,如果我调用Add()函数:

codeICompanyRepository lastDerived = new CompanyRepository(); ICompanyRepository->添加();code

然后根据您在回答中所述的OO规则,查找将从CompanyRepository类开始,期望CompanyRepository将已实现Add()函数作为 codeIRepository.Add(){} //从[link]中的P17和P18推导出来http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners[link]code

但是,在我的案例类中,CompanyRepository没有实现IRepository.Add(){},尽管控制流(在跟踪时)成功跳转到基类中的Add()函数(并且代码工作正常).我无法理解哪个OO规则适用于此处?

如果您需要我使用代码显示上述方案,请告诉我.

c# oop inheritance interface interface-implementation

5
推荐指数
1
解决办法
3415
查看次数

理解这个C++代码.继承和范围更改

我在SO的一个帖子中看到了这个.我很难理解以下代码.

class A 
{
public:
    virtual void foo() = 0;
private:
    virtual void bar() = 0;
};

class B : public A 
{
private:
    virtual void foo() 
    {
        std::cout << "B in Foo \n";
    } 
public:
    virtual void bar() 
    {
        std::cout << "bar in Foo \n";
    } 
};

void f(A& a, B& b)
{
    a.foo(); // ok --->Line A
    //b.foo(); // error: B::foo is private
    //a.bar(); // error: A::bar is private
    b.bar(); // ok (B::bar is public, even though A::bar …
Run Code Online (Sandbox Code Playgroud)

c++ oop inheritance interface interface-implementation

5
推荐指数
0
解决办法
78
查看次数

即使定义了函数,我也必须在类中实现一个函数

我收到错误: Class 'QueryParameterComparer' must implement 'Function Compare(x As QueryParameter, y As QueryParameter) As Integer' for interface 'System.Collections.Generic.IComparer(Of QueryParameter)'.

在这个类定义上:

    Protected Class QueryParameterComparer
        Implements IComparer(Of QueryParameter)

        Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer
            If x.Name = y.Name Then
                Return String.Compare(x.Value, y.Value)
            Else
                Return String.Compare(x.Name, y.Name)
            End If
        End Function

    End Class
Run Code Online (Sandbox Code Playgroud)

我也尝试过完全写出来:

    Protected Class QueryParameterComparer
        Implements System.Collections.Generic.IComparer(Of QueryParameter)

        Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer
            If x.Name = y.Name Then
                Return String.Compare(x.Value, y.Value)
            Else
                Return …
Run Code Online (Sandbox Code Playgroud)

vb.net interface class interface-implementation

5
推荐指数
1
解决办法
3039
查看次数

具有接口属性的 C# 接口实现

我开始更深入地研究 C# 编程,并且一直在研究接口。

我了解接口的基础知识,因为它们应该是实现它们的任何类的“契约”。接口中定义的任何内容都需要在继承(不确定这是否是正确的术语)它们的任何类中实现。

因此,我向接口添加了一个属性,该属性本身就是一个接口。当尝试实现父接口时,我向我的类添加了一个属性,该类实现了父接口中的属性接口。这个解释可能有点令人困惑,所以我在下面添加了一些代码。

interface IPropertyThatIsAnInterface
{
    public int X { get; set; }
}

class ClassThatImplementsIPropertyThatIsAnInterface : IPropertyThatIsAnInterface
{
    public int X { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
interface IAnInterface
{
    public IPropertyThatIsAnInterface InterfaceProperty { get; set; }
}

class ClassThatImplementsIAnInterface : IAnInterface
{
    public ClassThatImplementsIPropertyThatIsAnInterface InterfaceImplmentationProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio(在 .Net Core 3.0 [即 C# 8.0] 中)出现错误,指出我的类ClassThatImplementsIAnInterface未实现接口属性 ,public IPropertyThatIsAnInterface InterfaceProperty { get; set; }(这是一个接口属性)。这是 C# 的限制还是我对接口理解的限制?

我这样做的原因是因为我想IPropertyThatIsAnInterfaceClassThatImplementsIAnInterface我创建的每个对象实现自定义。IE …

c# interface-implementation .net-core

5
推荐指数
1
解决办法
1万
查看次数

使用额外的接口实现在Java中实例化匿名内部类

假设我有以下两个类/接口定义:

public abstract class FooClass {
    public abstract void doFoo();
}
Run Code Online (Sandbox Code Playgroud)

public interface BarInterface {
    public void doBar();
}
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个扩展/实现两者的匿名内部类,我是否需要这样做:

public abstract class BothClass extends FooClass implements BarInterface {}

...

new BothClass() {
    public void doFoo() {
        System.out.println("Fooooooooo!!!!");
    }

    public void doBar() {
        System.out.println("Baaaaaaaar!!!!");
    }
}.doBar();
Run Code Online (Sandbox Code Playgroud)

或者是否有一个允许我不定义的捷径BothClass?这样的事情,也许:

new (FooClass implements BarInterface)() {
    public void doFoo() {
        System.out.println("Fooooooooo!!!!");
    }

    public void doBar() {
        System.out.println("Baaaaaaaar!!!!");
    }
}.doBar();
Run Code Online (Sandbox Code Playgroud)

(这个想法给了我几个错误,这些都没有帮助)

java anonymous-inner-class subclassing interface-implementation

4
推荐指数
1
解决办法
529
查看次数

在C#中实现ICollection.CopyTo:深层还是浅层副本?

我正在写一个实现的自定义类,IDictionary我不知道该怎么做CopyTo.是应该将每个元素复制到目标数组(浅层副本),还是应该复制/克隆每个元素,然后将它放在目标数组中(深层复制)?

c# clone copy interface-implementation

4
推荐指数
1
解决办法
871
查看次数

是否扩展接口,何时基类已经扩展了相同的接口

在C#中,如下面的代码片段所示,在声明类A时扩展接口IFoo是正确/正确的,知道类BaseClass扩展了接口IFoo吗?是否有必要在此处指定接口IFoo,这是最佳做法吗?

class A : BaseClass, IFoo
{
}
Run Code Online (Sandbox Code Playgroud)

可能是一个愚蠢的问题,但在这种情况下适当的做法是什么?

c# inheritance interface base-class interface-implementation

4
推荐指数
1
解决办法
52
查看次数