我试图让我做的新学生阅读about方法,但是about方法中的变量表示它们没有被分配。我以为他们是因为我把它们设置为我制作的每个班级的默认值。
namespace studentHeirarchy
{
class Program
{
static void Main(string[] args)
{
runStudent();
}
static void runStudent()
{
Student one = new Student();
Console.WriteLine(one.About());
Console.ReadLine();
}
}
}
namespace studentHeirarchy
{
public class ColumbiaStudent : Student
{
public bool isInOOP;
public ColumbiaStudent()
{
isInOOP = true;
}
public String About()
{
string About = "my favorite course is OOP";
return About;
}
public void GetOOPString()
{
throw new System.NotImplementedException();
}
}
}
namespace studentHeirarchy
{
public class Student : Person
{
public int NumCourses;
public string SchoolName;
public Student()
{
NumCourses = 1;
SchoolName = "Columbia";
}
public string About()
{
string About = string.Format("my name is{0}, I am {1} years old, I attend {2} and am taking {3} courses."), Name, Age, SchoolName, NumCourses;
return About;
}
public void AddCourse()
{
throw new System.NotImplementedException();
}
public void DropCourse()
{
throw new System.NotImplementedException();
}
}
}
namespace studentHeirarchy
{
public class Person
{
public int Age;
public string Name;
public Person()
{
Age = 17;
Name = "Jeff";
}
public string About()
{
throw new System.NotImplementedException();
}
}
}
答案 0 :(得分:2)
您的问题出在string.Format
上。您关闭了括号,然后它认为您正在声明其他变量:
string About = string.Format("my name is{0}, I am {1} years old, I attend {2} and am taking {3} courses."), Name, Age, SchoolName, NumCourses;
应该是:
string About = string.Format("my name is{0}, I am {1} years old, I attend {2} and am taking {3} courses.", Name, Age, SchoolName, NumCourses);
修复此问题会产生输出:
我的名字是杰夫,我17岁,我参加哥伦比亚大学并且正在修读1门课程。
另外,我建议你看一下C#Polymorphism。这是另一个问题的核心,而其他所有人的答案都会受到影响。具体而言,您需要了解virtual
,override
和new
(new
,因为它与方法签名有关,而非实例化。)
答案 1 :(得分:0)
你需要声明你的方法是虚拟的,在.net中,默认情况下它不是虚拟的。基本上,你隐藏了这个方法。
答案 2 :(得分:0)
您没有调用父类的基础构造函数。 About
方法也存在问题,必须是虚拟的,否则可能会调用错误的About
。
以下是更正后的代码:
namespace studentHeirarchy
{
class Program
{
static void Main(string[] args)
{
runStudent();
}
static void runStudent()
{
Student one = new Student();
Console.WriteLine(one.About());
Console.ReadLine();
}
}
}
namespace studentHeirarchy
{
public class ColumbiaStudent : Student
{
public bool isInOOP;
public ColumbiaStudent() : base()
{
isInOOP = true;
}
public override String About()
{
string About = "my favorite course is OOP";
return About;
}
public void GetOOPString()
{
throw new System.NotImplementedException();
}
}
}
namespace studentHeirarchy
{
public class Student : Person
{
public int NumCourses;
public string SchoolName;
public Student() : base()
{
NumCourses = 1;
SchoolName = "Columbia";
}
public override string About()
{
string About = string.Format("my name is{0}, I am {1} years old, I attend {2} and am taking {3} courses."), Name, Age, SchoolName, NumCourses;
return About;
}
public void AddCourse()
{
throw new System.NotImplementedException();
}
public void DropCourse()
{
throw new System.NotImplementedException();
}
}
}
namespace studentHeirarchy
{
public class Person
{
public int Age;
public string Name;
public Person()
{
Age = 17;
Name = "Jeff";
}
public virtual string About()
{
throw new System.NotImplementedException();
}
}
}