Domanda

I have the following two classes:

abstract class LogItem    {       
    public String payload { get; set; }       
    public String serverId { get; set; }     
    public DateTime timeRecieved { get; set; }

}

  class MyLogItem : LogItem
{
  //No I want this to have to have the members from the abstract class above, as if it where an interface?
}

So in other words I am wanting a type if interface that can have definitions or variables which all classes that implement it have to have, but they could add more if they required ?

The above example builds, even if i dono add the members from the abstract class.

È stato utile?

Soluzione

edit Forget what I've said before. These are attributes, not methods. For them to be accessible on derived classes, you make them protected or public. The difference is that public members are visible to the world, while protected ones are visible to the class and subclasses.

Any class derived from your LogItem may have other variables.

abstract class LogItem    {       
    public String payload { get; set; }       
    public String serverId { get; set; }     
    public DateTime timeRecieved { get; set; }

}

 class MyLogItem : LogItem
{
  //No I want this to have to have the members from the abstract class above, as if it where an interface?
   private void TestMethod(){
     String test = payload;
   }
}

check out this post for more information

Your MyLogItem class can reference any of the above members directly. They are accessible

You may declare an interface with those

 public interface MyInterface {
     public String payload { get; set; }       
     public String serverId { get; set; }     
     public DateTime timeRecieved { get; set; }
 }

and your class

 public class MyLogItem : MyInterface 
{
   String _payload;
   public String payload { get{ return _payload; } set {_payload=value;} }
   ...
}

Altri suggerimenti

The abstract keyword can also be applied to methods, as described here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top