C # method: is there a parameter name for inserting a "derived class name" in general?

I want to create a Student base class using the changeName method inside it. I want studentclasses be derived from the Student base class, e.g. collegestudent , etc. I want to be able to change student names.

I have this code:

 public abstract class Student { public virtual void changeName(CollegeStudent s, string name) { s.firstName = name; } public abstract void outputDetails(); } public class CollegeStudent : Student { public string firstName; public string lastName; public string major; public double GPA; public override void outputDetails() { Console.WriteLine("Student " + firstName + "); } } 

I want to know if it is possible to change the parameters of public virtual void changeName to a general parameter that any derived class from Student accepts.

Is this possible in C #?

(Something like this: public virtual void changeName (Anyderivedclass s, string name)

+5
source share
2 answers

If you change your design a little, everything will become much simpler. It seems that you are coming from a Java background, so let's see how C # can improve the code.

First, take the FirstName / LastName fields and move them to the base class, as any student enrollment should provide them. Secondly, C # has a function called Properties (here we can use Auto-Implemented Properties, since we donโ€™t need verification), which is basically syntactic sugar for the get / set methods. You can recreate your student class to look like this:

 public abstract class Student { public string FirstName { get; set; } public string LastName { get; set; } } 

This will allow any instance of the derived class to change the firstname and lastname properties without adding additional methods:

 void Main() { var student = new CollegeStudent(); student.FirstName = "Yuval"; } 

In general, any instance method of an object that you create does not have to accept its own type in order to mutate itself.

+7
source

But changeName () is an instance method of your abstract student. Why are you passing an instance of one of its derivatives as one of its parameters?

Why do you need generics? It should be as simple as ...

 public abstract class Student { public abstract void changeName(string newName); public abstract void outputDetails(); } public class CollegeStudent : Student { public string firstName; public string lastName; public string major; public double GPA; public override void changeName(string newName) { firstName = newName; } public override void outputDetails() { Console.WriteLine("Student " + firstName + "); } } 

And actually (as indicated) the presence of changeName () in the base class assumes that the name properties belong to the base class, so it should be as follows ...

 public abstract class Student { public string firstName; public string lastName; public virtual void changeName(string newName) { firstName = newName; } public abstract void outputDetails(); } public class CollegeStudent : Student { public string major; public double GPA; public override void outputDetails() { Console.WriteLine("Student " + firstName + "); } } 
+5
source

Source: https://habr.com/ru/post/1235603/


All Articles