简体   繁体   English

使用c#进行OOPS查询

[英]OOPS query using c#

I have some doubt to implementation of class and interface 我对类和接口的实现有一些疑问

I have 2 class like this 我有2个这样的课

Public Class A:IFinal
  {
      private string name=string.Empty;

        A()
        {
            name = "Pankaj";
        }

        public string MyName()
        {
            return name;
        }

        public string YourName()
        {
            return "Amit";
        }
   }

Public  Class B:IFinal
 {
     private string name=string.Empty;

        B()
        {
            name = "Amit";
        }

        public string GetNane()
        {
            return name;
        }

        public string YourName()
        {
            return "Joy";
        }
   }

Question: 题:

  1. Now i have a interface IFinal and i want to implement this interface in class A & B for method YourName() like this 现在我有一个接口IFinal,我想在类A和B中实现此接口,方法YourName()就像这样

    public interface IFinal { 公共接口IFinal {

      string YourName();// Class A & Class B } 

Is it possible to implement on this way? 是否有可能以这种方式实施? if yes then How can i declare YourName() in interface and how can i use this? 如果是,那么我如何在界面中声明YourName(),我该如何使用它?

  1. Is it possible to declare virtual method in interface?like in class A & B we have a virtual method which need to be declare in interface. 是否可以在接口中声明虚方法?就像在类A和B中一样,我们有一个需要在接口中声明的虚方法。

You can make the method virtual in your implementation eg: 您可以在实现中将方法设为虚拟,例如:

interface IFinal
{
    string YourName();
}

class A: IFinal
{
    public virtual string YourName() { return "Amit"; }
}

class B: IFinal
{
    public virtual string YourName() { return "Joy"; }
}

Or you could use a common base implementation which both A and B derive from, eg 或者你可以使用A和B派生的公共基础实现,例如

interface IFinal
{
    string YourName();
}

abstract class FinalBase : IFinal
{
    public virtual string YourName() { return string.Empty; }
}

class A : FinalBase
{
    public override string YourName()
    {
        return "A";
    }
}

class B : FinalBase
{
    public override string YourName()
    {
        return "B";
    }
}

class C : A
{
    public override string YourName()
    {
        return "C";
    }
}

new A().YourName(); // A
new B().YourName(); // B

IFinal b = new B();
b.YourName(); // B

FinalBase b = new C();
b.YourName(); // C

Pankaj - the code formatting and values in the IFinal are making it pretty hard to figure out what you're attempting to do. Pankaj - IFinal中的代码格式和值使得很难弄清楚你想要做什么。 based on what is supplied, then the sample simply would not compile for the obviuos reason that you've got the same property (string YourName();) defined twice. 根据提供的内容,然后样本根本无法编译,因为您已经定义了两次相同的属性(字符串YourName();)。

can you redo the question to clarify your intentions plz... thanks 你可以重做这个问题来澄清你的意图吗...谢谢

[edit] - i think i maybe 'understand' what you're asking - ie HOW to define the interface. [编辑] - 我想我可能'理解'你在问什么 - 即如何定义界面。 here you go: 干得好:

public interface IFinal
{   
    string YourName{ get; set; }
}

then, declare your variables along the lines of: 然后,声明你的变量:

IFinal classA = new A();
IFinal classB = new B();

then, party hard :) 派对很难:)

If you declare your interface as 如果您将接口声明为

interface IFinal {
string YourName();
}

both classes will have to implement that function which I think is what you are asking. 这两个类都必须实现我认为你所要求的功能。

You have the reverse view of an Interface. 您有一个接口的反向视图。 First you have to declare the method in the Interface and then you have to move to implementation in the Classes that implements the Interface. 首先,您必须在接口中声明方法,然后必须转到实现接口的类中的实现。 You have taken the other way. 你采取了另一种方式。

First declare the methods in the Interface 首先在Interface中声明方法

interface IFinal { 
string YourName(); 
}

Then 然后

Public Class A:IFinal
{
    public string YourName()
    {
        return "Amit";
    }

} }

and

Public Class B:IFinal
{
    public string YourName()
    {
        return "Joy";
    }
}

Here the Class A and B implements the interface IFinal and its method YourName(). 这里A类和B类实现了IFinal接口及其方法YourName()。

To call the methods in the respective classes 调用各个类中的方法

IFinal clsA= new A();
IFinal clsB= new B();

If you're trying to make IFinal have two methods, one that returns YourName from class A and one that returns YourName from class B then you'll need to introduce a third class (let's call it C ) and then change the YourName methods on IFinal to be distinct - you can't have two methods with exactly the same signature on an interface. 如果你想让IFinal有两个方法,一个从A类返回YourName ,另一个从B类返回YourName ,那么你需要引入第三个类(让我们称之为C )然后更改YourName方法IFinal是不同的 - 您不能在接口上使用两个具有完全相同签名的方法。

So: 所以:

public interface IFinal
{
    string YourNameA();
    string YourNameB();
}

public class C : IFinal
{
    public A A {get; set;}
    public B B {get; set;}

    public string YourNameA()
    {
        return this.A.YourName();
    }

    public string YourNameB()
    {
        return this.B.YourName();
    }
}

Now, having said that, I'm not really sure if that's what you're asking or if doing this makes sense. 现在,说了这些,我不确定这是你要问的,或者这样做是否有意义。 You'll have to let us know more detail about what you're trying to do. 您必须让我们了解您正在尝试做的更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM