views:

155

answers:

7

I am wondering whether there is any feature like method inheritance rather than whole class inheritance, let me elaborate what I am trying to explain :

class a {
   public void GetA(){
     // some logic here
}
}

class b {
    public void GetB() : new Class().GetA()
}

I know it looks weird but I was reading how to do delegation in object composition pattern and I thought this pattern for some reason.

+1  A: 

This is not possible. If you want b.GetB to reuse the functionality of a.GetA then you need to instantiate an a and invoke a.GetA.

Jason
Agreed; favor composition, not inheritance.
Robert Harvey
+4  A: 

A common way to do composition is to create a private member variable in class b, of type class a, and in GetB() call a.GetA(). For example:

class a {
   public void GetA(){
     // some logic here
   }
}

class b {
    private a _a=new a();

    public void GetB()
    {
         _a.GetA();
    } 
}

Another option might be to define a delegate member variable called GetB instead of simple method and allow the calling code to supply the code that is executed by GetB().

Ash
Could you tell me what the benefits are of doing this pattern ? Thank you.
Braveyard
@Aaron, the benefits of this are the same as the benefits of using composition in general. It allows you to hide more details about your implementation then inheritance does. In this example callers of class B do not need to know that you are actually using class A to implement GetB(). So it is generally often more flexible and loosely coupled than inheritance would be.
Ash
+3  A: 

The closest thing I can think of is how in C# you can "inherit" a class's constructor in that of its child class (or an overloaded constructor of the same class), like so:

class Animal {
    public string Species { get; set; }

    public Animal(string species) {
        Species = species;
    }
}

class Human : Animal {
    public string Name { get; set; }

    public Human(string name) : base("Homo sapien") {
        Name = name;
    }
}

The behavior of the above code is pretty straightforward: the constructor for the Human class essentially calls the constructor for the Animal class before doing anything else. Why this same functionality isn't available to non-constructor methods, I'm not sure.

Dan Tao
The `Home Sapien` is an argument there, right ?
Braveyard
@Aaron: Yes, in the `Human` constructor, "Home sapien" gets passed as the 'species' argument to the `Animal` constructor.
Dan Tao
+4  A: 

If you just want to call GetA() inside of GetB(), but don't want to define or explicitly reference an instance of class a in GetB(), you can pass GetA() in as a delegate.

In C#, there are many predefined delegates such as Action and Func. Or, you could always roll your own delegate to match the method signature.

    class a
    {
        public void GetA()
        {
            Console.WriteLine("Hello World!");
        }
    }

    class b
    {
        // No explicit reference to class a of any kind.
        public void GetB(Action action)
        {
            action();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var a = new a();
            var b = new b();

            b.GetB(a.GetA);
        }
    }
Aaron Daniels
Wow, that looks cool, I didn't know such a Generic solution .net already provides, I was always creating my own delegates...
Braveyard
A: 

A slightly different approach would be to accept an instance of ClassA in the constructor of ClassB, rather than instantiating it directly. This would be applying the Dependency Inversion Principle to @Ash's answer:

public class ClassB
{
    private readonly ClassA _a;

    public b(ClassA a)
    {
        _a = a;
    }

    public void GetB()
    {
        _a.GetA();

        // Other logic
    }
}
Bryan Watts
A: 
public class Article
{

 public object AddOrUpdate(params object[] paras){

   return null;

  }

}

public class Test:Article
{

 public new object AddOrUpdate(params object[] paras){

   //do your logic......

    return base.AddOrUpdate(paras);

 }


}

you mean this?this is inhret a class can do get or set BASE object property.

if you do with delegate ,you must do more job with the same logic.

but delegate can get the diffenent domain object and do more over-app thing.

cjjer
A: 

If GetA() is changed to a static method, you can simply call it within the GetB() function:

class a { 
   public static void GetA() { 
     // some logic here 
   } 
} 

class b { 
    public void GetB() {
      a.GetA();
    }
} 

If GetA() is not static, it doesn't make sense since GetA() needs an object context (eg., the invisible "this" pointer) by definition. You can't pass an instance of object B to class A, as class A does not know anything about class B.

What are you really attempting to do?

tgiphil