构造函数在对象创建时打印文本。

huangapple go评论113阅读模式
英文:

Constructor prints text when object is created

问题

我在一个C#控制台应用程序中工作,正在学习面向对象编程。首先,我有一个Person类

  1. class Person
  2. {
  3. public string Name;
  4. public string LastName;
  5. public void Comment()
  6. {
  7. Console.WriteLine("来自Person类的评论");
  8. }

Student类

  1. class Student : Person
  2. {
  3. public int IndexNr = 171124;
  4. public int totalSubjects;
  5. public Student()
  6. {
  7. Console.WriteLine("索引号=" + IndexNr);
  8. }
  9. public Student(int subjectsTotal, int nr)
  10. {
  11. totalSubjects = nr;
  12. Console.WriteLine("平均成绩= " + subjectsTotal / totalSubjects);
  13. }
  14. public void Comment()
  15. {
  16. Console.WriteLine("来自Student类的评论");
  17. }
  18. }

在Program.cs的Main函数中,代码如下:

  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("学生信息");
  4. Student st2 = new Student();
  5. st2.Name = "name1";
  6. st2.LastName = "lastname1";
  7. Console.WriteLine("姓名=" + st2.Name + " 姓氏=" + st2.LastName);
  8. Student st1 = new Student(90, 10);
  9. //多态示例 -> 这里是我需要帮助的地方
  10. Person p1 = new Person();
  11. Person s1 = new Student();
  12. p1.Comment();
  13. s1.Comment();
  14. }

所以,当我创建Person s1 = new Student()对象时,它只打印来自Person类的Comment函数,而不是来自Person和Student类的两个Comment函数。

我在Java中尝试了相同的操作,那里一切都运行得很好。您能告诉我我在哪里出错或者遗漏了什么吗?谢谢。

英文:

I am working in a C# console application and I am learning OOP.
First, I have a Person Class,

  1. class Person
  2. {
  3. public string Name;
  4. public string LastName;
  5. public void Comment()
  6. {
  7. Console.WriteLine("Comment from Person Class");
  8. }

and Student class

  1. class Student : Person
  2. {
  3. public int IndexNr = 171124;
  4. public int totalSubjects;
  5. public Student()
  6. {
  7. Console.WriteLine("Index number=" + IndexNr);
  8. }
  9. public Student(int subjectsTotal,int nr)
  10. {
  11. totalSubjects= nr;
  12. Console.WriteLine("Average grade= " + subjectsTotal/ totalSubjects);
  13. }
  14. public void Comment()
  15. {
  16. Console.WriteLine("Comment from Student class");
  17. }
  18. }

And in the Program.cs in the Main function the code goes like this:

  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Student Info");
  4. Student st2= new Student();
  5. st2.Name= "name1";
  6. st2.LastName = "lastname1";
  7. Console.WriteLine("Name=" + st2.Name+ " LastName=" + st2.LastName);
  8. Student st1 = new Student(90,10);
  9. //Polymorphism example -> here is where I need help
  10. Person p1 = new Person();
  11. Person s1 = new Student();
  12. p1.Komenti();
  13. s1.Komenti();
  14. }

So, when I create Person s1 = new Student() object it prints out the the Comment function from only Person class, and not 1 from Person and 1 from Student class.

I have tried the same in Java as well and there it works very well.
Can you please tell me and enlighten me where am I making mistakes or what am I missing?
Thank you.

答案1

得分: 1

请尝试在Student类的Comment方法上使用override修饰符,以覆盖Person类的实现。您可以参考以下链接:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override 然后,这将覆盖person的实现。

  1. public class Student : Person
  2. {
  3. public int IndexNr = 171124;
  4. public int totalSubjects;
  5. public Student()
  6. {
  7. Console.WriteLine("Index number=" + IndexNr);
  8. }
  9. public Student(int subjectsTotal, int nr)
  10. {
  11. totalSubjects = nr;
  12. Console.WriteLine("Average grade= " + subjectsTotal / totalSubjects);
  13. }
  14. public override void Comment()
  15. {
  16. Console.WriteLine("Comment from Student class");
  17. }
  18. }
英文:

Try using the override modifier on the Comment method in the Student class https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override This should then override the person implementation

  1. public class Student : Person
  2. {
  3. public int IndexNr = 171124;
  4. public int totalSubjects;
  5. public Student()
  6. {
  7. Console.WriteLine("Index number=" + IndexNr);
  8. }
  9. public Student(int subjectsTotal,int nr)
  10. {
  11. totalSubjects= nr;
  12. Console.WriteLine("Average grade= " + subjectsTotal/ totalSubjects);
  13. }
  14. public override void Comment()
  15. {
  16. Console.WriteLine("Comment from Student class");
  17. }
  18. }

huangapple
  • 本文由 发表于 2023年2月19日 05:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定