How is the ModelDuck constructor able to access the fields in the Duck class - flyBehaviour and quackBehaviour. Thanks
namespace HeadFirst
{
public class ModelDuck : Duck
{
public ModelDuck()
{
flyBehaviour = new FlyNoWay();
quackBehaviour = new Quack();
}
public override void display()
{
Console.WriteLine("I am a model duck");
}
}
}
public abstract class Duck
{
public FlyBehaviour flyBehaviour;
public QuackBehaviour quackBehaviour;
public Duck()
{
}
}
The properties belong the the base class.
You should always be able to acces the properties and methods of your base class
Related
Is there any way to use an abstract class as a generic type? I've given a very simple example below to simulate what I need. I get an error that an implicit conversion cannot be made for variable genericBehavior.
public abstract class AnimalBase { } // Cannot edit
public class Dog : AnimalBase { } // Cannot edit
public class Cat : AnimalBase { } // Cannot edit
public interface IAnimalBehavior<T> where T : AnimalBase { }
public class CatBehavior : IAnimalBehavior<Cat> { }
public class DogBehavior : IAnimalBehavior<Dog> { }
public class Startup
{
public Startup()
{
IAnimalBehavior<Cat> catBehavior = new CatBehavior();
IAnimalBehavior<AnimalBase> genericBehavior = new CatBehavior(); // This doesn't work
}
}
Ultimately, in my .NET Core Startup, I'd like to be able to have the following:
services.AddScoped<IAnimalBehavior<AnimalBase>, CatBehavior>();
or
services.AddScoped<IAnimalBehavior, CatBehavior>();
What's the best approach?
Since are using IAnimalBehavior<AnimalBase>, and not IAnimalBehavior<Cat>, then this structure works:
public interface IAnimalBehavior{ }
public class CatBehavior : IAnimalBehavior { }
public class DogBehavior : IAnimalBehavior { }
...
services.AddScoped<IAnimalBehavior, CatBehavior>();
Having IAnimalBehavior<T> makes sense if you differentiate on T:
public class CatBehavior : IAnimalBehavior<Cat> { }
public class DogBehavior : IAnimalBehavior<Dog> { }
...
services.AddScoped<IAnimalBehavior<Cat>, CatBehavior>();
services.AddScoped<IAnimalBehavior<Dog>, CatBehavior>();
So I have a maybe naive question about object inheritance and constructors. Basically, a class has an object:
public class ParentClass{
protected Parent item;
And the accessor goes as follows:
public Parent ItemValue
{
set
{
item = value;
}
get
{
return item;
}
}
Now I want to inherit the class:
public class ChildClass:ParentClass
{
public new Child item;
}
Now, whenever I access the Child item through the inherited accessor it, of course, returns the item as the Parent class instead of Child class. Is there a way to make it return the item as Child class without overwriting the accessor in the ChildClass?
No, you can't change type of base property to return different (derived) type.
Standard workaround if you don't need inheritance - generic class:
public class ParentClass<T> {
public T ItemValue { get; set; }
...
}
public class ChildClass : ParentClass<ChildClass>
{
...
}
Note that if you just need access to item in its own class you can just have virtual property:
public class Parent { }
public class Child:Parent { public string ChildProperty; }
public abstract class ParentClass
{
public abstract Parent ItemValue { get; }
}
public class ChildClass : ParentClass
{
Child item;
public override Parent ItemValue { get {return item;} }
public void Method()
{
// use item's child class properties
Console.Write(item.ChildProperty);
}
}
If you are just wanting to have Item of a type defined by your descendent class, you can do this
public class ParentClass<T>{
protected T item;
public T ItemValue
{
set
{
item = value;
}
get
{
return item;
}
}
}
public class ChildClass:ParentClass<Child>
{
// No need to create a new definition of item
}
However depending on your problem, your next question will be how can I add ChildClass1 and ChildClass2 to the same List/Array/Dictionary/etc when they have different T's.
Take a step back for a minute. Does your ParentClass really need to know what item is?
(Ab)Using the Animal example above, your Horse might have a Walk(), Trot(), Canter() or Gallop() methods but a Duck might have a Swim() or Waddle() methods.
Perhaps your logic says something like, iterate my animal collection and tell the swimmers to swim. In this case, you could declare:
using System;
using System.Collections.Generic;
public class Program
{
public class Location {}
public interface ISwimmer{
void SwimTo(Location destination);
}
public class Animal {} // whatever base class properties you need
public class Duck : Animal, ISwimmer
{
public void SwimTo(Location destination)
{
Console.WriteLine("Implement duck's swim logic");
}
}
public class Fish : Animal, ISwimmer
{
public void SwimTo(Location destination)
{
Console.WriteLine("Implement fish's swim logic");
}
}
public class Giraffe : Animal {}
public static void Main()
{
List<Animal> animals = new List<Animal>
{
new Duck(),
new Fish(),
new Giraffe()
};
foreach (Animal animal in animals)
{
ISwimmer swimmer = animal as ISwimmer;
if (swimmer==null) continue; // this one can't swim
swimmer.SwimTo(new Location());
}
}
}
Sorry I'm kinda new to c# how would I make a class where I can access it like this:
Myclass.subclass.method();
This is what I have now:
namespace zzcore
{
class myclass
{
class subclass
{
public static void method() { }
}
}
}
What happens here is that a nested class without a visibility modifier is implicitly private. In this context, private means that only the parent class can see it.
Declare both classes as public and you will be able to call myclass.subclass.method();
namespace zzcore
{
public class myclass
{
public class subclass
{
public static void method() { }
}
}
}
Working example: http://ideone.com/tJVKJ
I have a need where I have to add some new fields to an existing class along with all its existing fields/attributes.
So whenever my derived class is filled by DAL, I will be filling all fields of base class as well. Currently, I am doing it like this but not sure this is the right way ? Please give me an example. Also I am not sure whether the base class object will be a new one each time a derived class is initialized ?
public class Employee
{
private int _id;
private int _name;
public int ID
{
set { _id=value;}
get { return _id;}
}
public int Name
{
set { _name=value;}
get { return _name;}
}
protected void SetName ()
{
_name=value;
}
protected void SetID()
{
_id=value;
}
}
public class EmployeeWithDepartmentName:Employee
{
private string _deptName;
public string DeptName
{
set { _deptName=value; }
}
public setBaseEmpName()
{
base.SetName();
}
public setBaseID()
{
base.SetID();
}
}
Everything in a base class can automagically be accessed from derived classes without doiing anything, just use the property/method name directly.
public class MyBase
{
public string UserName {get;set;}
}
public class MyClass : MyBase
{
public void DoSomething()
{
Console.WriteLine("UserName: {0}", UserName);
UserName = "Anders";
}
}
You can also do this:
MyClass myClass = new MyClass();
myClass.UserName = "Rune";
Protected means that only derived classes can access the property/method. Public means that everyone can access the properties/methods.
Also I am not sure whether the base class object will be a new one each time a derived class is initialized ?
It's not two objects, it's one object created from two different classes (that's how inheritance works).
Read this article about inheritance: http://www.csharp-station.com/Tutorials/lesson08.aspx
I can't figure out the use for this code. Of what use is this pattern?
[code repeated here for posterity]
public class Turtle<T> where T : Turtle<T>
{
}
This pattern essentially allows you to refer to a concrete subclass within the parent class.
For example:
public abstract class Turtle<T> where T : Turtle<T>
{
public abstract T Procreate();
}
public class SeaTurtle : Turtle<SeaTurtle>
{
public override SeaTurtle Procreate()
{
// ...
}
}
Versus:
public abstract class Turtle
{
public abstract Turtle Procreate();
}
public class SnappingTurtle : Turtle
{
public override Turtle Procreate()
{
// ...
}
}
In the former, it's specified that a SeaTurtle's baby will be a SeaTurtle.
There is no use that I can see. Basically, it's the same as
public class Turtle
{
}