英文:
Constructor prints text when object is created
问题
我在一个C#控制台应用程序中工作,正在学习面向对象编程。首先,我有一个Person类
,
class Person
{
public string Name;
public string LastName;
public void Comment()
{
Console.WriteLine("来自Person类的评论");
}
和Student类
class Student : Person
{
public int IndexNr = 171124;
public int totalSubjects;
public Student()
{
Console.WriteLine("索引号=" + IndexNr);
}
public Student(int subjectsTotal, int nr)
{
totalSubjects = nr;
Console.WriteLine("平均成绩= " + subjectsTotal / totalSubjects);
}
public void Comment()
{
Console.WriteLine("来自Student类的评论");
}
}
在Program.cs的Main函数中,代码如下:
static void Main(string[] args)
{
Console.WriteLine("学生信息");
Student st2 = new Student();
st2.Name = "name1";
st2.LastName = "lastname1";
Console.WriteLine("姓名=" + st2.Name + " 姓氏=" + st2.LastName);
Student st1 = new Student(90, 10);
//多态示例 -> 这里是我需要帮助的地方
Person p1 = new Person();
Person s1 = new Student();
p1.Comment();
s1.Comment();
}
所以,当我创建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
,
class Person
{
public string Name;
public string LastName;
public void Comment()
{
Console.WriteLine("Comment from Person Class");
}
and Student class
class Student : Person
{
public int IndexNr = 171124;
public int totalSubjects;
public Student()
{
Console.WriteLine("Index number=" + IndexNr);
}
public Student(int subjectsTotal,int nr)
{
totalSubjects= nr;
Console.WriteLine("Average grade= " + subjectsTotal/ totalSubjects);
}
public void Comment()
{
Console.WriteLine("Comment from Student class");
}
}
And in the Program.cs in the Main function the code goes like this:
static void Main(string[] args)
{
Console.WriteLine("Student Info");
Student st2= new Student();
st2.Name= "name1";
st2.LastName = "lastname1";
Console.WriteLine("Name=" + st2.Name+ " LastName=" + st2.LastName);
Student st1 = new Student(90,10);
//Polymorphism example -> here is where I need help
Person p1 = new Person();
Person s1 = new Student();
p1.Komenti();
s1.Komenti();
}
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的实现。
public class Student : Person
{
public int IndexNr = 171124;
public int totalSubjects;
public Student()
{
Console.WriteLine("Index number=" + IndexNr);
}
public Student(int subjectsTotal, int nr)
{
totalSubjects = nr;
Console.WriteLine("Average grade= " + subjectsTotal / totalSubjects);
}
public override void Comment()
{
Console.WriteLine("Comment from Student class");
}
}
英文:
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
public class Student : Person
{
public int IndexNr = 171124;
public int totalSubjects;
public Student()
{
Console.WriteLine("Index number=" + IndexNr);
}
public Student(int subjectsTotal,int nr)
{
totalSubjects= nr;
Console.WriteLine("Average grade= " + subjectsTotal/ totalSubjects);
}
public override void Comment()
{
Console.WriteLine("Comment from Student class");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论