4 Clases con las mismas propiedades y métodos: ¿es posible crear solo una?

i have another question open here on SO and after thinking about it, i may be approaching this in the wrong way.

i have 4 classes, that have the same properties and methods. some of the classes, have their own properties and methods ( not overrides of the existing ones ).

currently i create each class as:

public class ClassOne
{
    public ClassOne()
    {
    }

    public int ID {get;set;}
    // More properties here

    public void Set(){
        // Do Stuff to save this
    }
    // More Methods here
}

cant i create one class that will generate all of the 4 classes? and in the classes themselfs i only create specific properties/methods for that class?

repeating the code seems very odd to me, im sure there must be a way to do this, just dont know how.

preguntado el 01 de febrero de 12 a las 22:02

Can you provide specific details? What are you wish to accomplish? -

Create an interface, which defines the common method and property names. And create an abstract base class, which contains the common code for all these classes, and implements the interface. Each of your four classes then inherit from the abstract base class, overriding/implementing what is different. -

@chad since i am new to this, could you provide some sample code or refrences ? -

5 Respuestas

Your situation is one of the main reasons why herencia was invented. So with that, you can write

public class Base
{
    // Properties and methods common to all
}
public class ClassOne : Base
{
    // Properties and methods specific to ClassOne
}
public class ClassTwo : Base
{
    // Properties and methods specific to ClassTwo
}
public class ClassThree : Base
{
    // Properties and methods specific to ClassThree
}
public class ClassFour : Base
{
    // Properties and methods specific to ClassFour
}

Respondido 02 Feb 12, 02:02

As requested, more code, using interfaces and abstract classes:

An interface is just a blueprint, defining what properties and methods are required to be compatible with other "BaseClasses"

public interface IBaseClass
{
    public int ID {get;set;}    
    public void Set();
}

Abstract classes can contain code, but can not be instantiated, they are form of starting point for a class, but not a complete class themselves.

public abstract class ABaseClass : IBaseClass
{
    public int ID {get;set;}

    public void Set(){
        // Do Stuff to save 
    }    
}

Each class inherits from the abstract class and can then override and implement whatever it wants, customizing it however is necessary.

public class ClassOne : ABaseClass
{

}
public class ClassTwo : ABaseClass
{

}
public class ClassThree : ABaseClass
{

}
public class ClassFour : ABaseClass
{

}

ps. not entirely sure if my syntax is 100% correct

Respondido 02 Feb 12, 02:02

Simply explained combined with the simple code makes it an awesome answer. Thank you! - Dementico

one more question if i may, cant i define my methods directly in the interface? like Set() ? since it is common to all classes also, do i must override it in each class?, what i meen is, i can do the same without the interface and the abstract class, just by inheritance, so why is it preferd? - Dementico

An interface is simply that, an interface. It says nothing about the implementation. You use an abstract class to define all your common code because it's not complete. It's just a starting point, and while you could use a real class, if this class isn't functionally complete, you really don't want it to be instantiable. - CaffGeek

what i ment to say is, i can inherit from a class with all the properties and methods, which makes the interface seem useless, so my real question would be what is the benefit of adding the interface? - Dementico

I'm not sure how your app works, but lets say you have a method, that could in theory, take any one of your Classes (One, Two, Three, or Four). It doesn't care about the extras, or the implementation, but it does care that whatever is passed to it has a specific property or function. Interfaces provide that ability. It allows you to "group" classes together, and tell a function that it doesn't matter what class is passed to it, as long as it implements this interface, we can work with it. - CaffGeek

Could you simply make a base class with your properties and inherit from that class?

Respondido 02 Feb 12, 02:02

Why not use inheritance??

 public class ClassOne 
 {
     public ClassOne()
     {
     }

     public virtual int ID {get;set;}
     // More properties here

     public virtual void Set(){
         // Do Stuff to save this
     }
     // More Methods here }

 public class ClassTwo : ClassOne 
 { 
    public string ClassTwoString { get; set; }
 }

 public class ClassThree : ClassOne 
 { 
    public string ClassThreeString { get; set; }
 }

Respondido 02 Feb 12, 02:02

Instead of inheriting from ClassOne, an abstract base class and an interface would be better. - CaffGeek

@Chad, you're right, it's just a matter of convention naming convention I suppose, though. I doubt he's really named his classes "ClassOne", "ClassTwo", etc. - Steve Danner

Can you make them all inherit off of the same class? If so, that sounds ideal.

Barring the possibility of making them inherit, you could write an interface that describes the methods and properties which each of them use. Then you can call each instance of the class through the same interface.

Barring again that possibility, you could write a reflective assignor/accessor. But you shouldn't do that.

Respondido 02 Feb 12, 02:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.