Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得about方法来读取变量?_C#_Class_Oop_Inheritance - Fatal编程技术网

C# 如何获得about方法来读取变量?

C# 如何获得about方法来读取变量?,c#,class,oop,inheritance,C#,Class,Oop,Inheritance,我试图让我的新同学阅读about方法,但是about方法中的变量表示它们没有赋值。我认为它们是因为我在每个类中都将它们设置为默认值 namespace studentHeirarchy { class Program { static void Main(string[] args) { runStudent(); } static void runStudent() {

我试图让我的新同学阅读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();
        }
    }
}

您需要将您的方法声明为虚拟的,在.net中,默认情况下它不是虚拟的。基本上,您正在隐藏该方法。

您没有调用父类的基构造函数。
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();
        }
    }
}

您的问题在于
字符串。格式
。关闭括号后,它会认为您在声明其他变量:

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。这是另一个问题的核心,其他人的答案都被这一问题所偏离。具体来说,您需要了解
virtual
override
new
new
,因为它与方法签名相关,而不是实例化)。

这样做之后,名称、年龄、NumCourses和SchoolName变量在我的about method中仍然没有使用。非常感谢,你知道有关于多态性的视频吗?当我在doneHa看到它时,我觉得我更明白了,那是一个讨厌的打字错误:)