Frage

Im trying to get the new student I made to read the about method, however the variables in the about method say they are not assigned. I thought they were because I had set them as defaults in each class that I made.

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();
        }
    }
}

Keine korrekte Lösung

Andere Tipps

Your problem is with your string.Format. You're closing the parentheses, then it thinks you're declaring other variables:

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;

should be:

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);

Fixing this will yield the output:

my name isJeff, I am 17 years old, I attend Columbia and am taking 1 courses.

Additionally, I would urge you to look at C# Polymorphism. that is the core of another problem and where everybody else's answers are getting sidetracked by. Specifically you need to understand virtual, override, and new (new as it relates to method signatures, not instantiation).

you need declare your method virtual, in .net , it is not virtual by default. Basically , you are hiding the method.

You are not calling the base constructors of parent classes. There is a problem with the About method as well which has to be virtual, otherwise the wrong About might be called.

Below is the corrected code:

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();
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top