Dumb question.
Lets say I have a bunch of person objects with their fields all filled in with data and I have an employee type that derives from the person class and has extra fields related to being an employee. How do I get a employee object for a particular existing person object? i.e. How do I pass in the person object to the employee?
From stackoverflow
-
If the person was created as an employee, then just cast:
Person person = new Employee(); // for some reason ... Employee emp = (Employee)person;If the person is just a person: you can't; you could have the employee encapsulate the Person - or you can copy the fields:
class Employee { // encapsulation private readonly Person person; public Person {get {return person;}} public Employee(Person person) {this.person = person;} public Employee() : this(new Person()) {} }or
class Employee : Person { // inheritance public Employee(Person person) : base(person) {} public Employee() {} } class Person { public Person(Person template) { this.Name = template.Name; // etc } public Person() {} }Christopher Edwards : I thought as much; thanks for the answer.
0 comments:
Post a Comment