public class Teacher : Person被定义在public class Student : Person的{}里面了,把他移到外面去

解决方案 »

  1.   

    上面有几个小错误,改了大家再看看using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication2
    {
        class Person
    {
        protected int id;
        protected string name;
        public Person(int pid, string pname)
        {
            id = pid;
            name = pname;
        }
        public virtual void infoPrint()
        {
            Console.WriteLine("Person's Id: {0}", id);
            Console.WriteLine("Person's Name: {0}", name);
        }
    }
          public class Student : Person
    {
        protected int class_id;
        protected int score;
        public Student(int pid, string pname,int sclass_id,int sscore)
        {
            id = pid;
            name = pname;
            class_id = sclass_id ;
            score = sscore;
        }
        public new void infoPrint()
        {
            Console.WriteLine("Person's Id: {0}", id);
            Console.WriteLine("Person's Name: {0}", name);
            Console.WriteLine("Person's Class_id: {0}", class_id);
            Console.WriteLine("Person's Score: {0}", score);    }
        public class Teacher : Person
        {
            protected string position;
            protected string department;
            public Teacher(int pid, string pname, string tposition,string tdepartment)
            {
               id = pid;
                name = pname;
              position = tposition;
                department = tdepartment;
            }
            public override void infoPrint()
            {
                Console.WriteLine("Person's Id: {0}", id);
                Console.WriteLine("Person's Name: {0}", name);
                Console.WriteLine("Person's Position: {0}", position);
                Console.WriteLine("Person's Department: {0}", department);        }
        }
    }static void Main(string[] args)
            {
        Person person=new Person();
        Teacher teacher=new Teacher(86,"王宁","jiaoshou","jisuanji");
        teacher.infoPrint();
        Student student=new Student(101,"李刚",2,90);
        student.infoPrint();
            }
        
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication2
    {
        public class Person
    {
        protected int id;
        protected string name;
        public Person(int pid, string pname)
        {
            id = pid;
            name = pname;
        }
        public virtual void infoPrint()
        {
            Console.WriteLine("Person's Id: {0}", id);
            Console.WriteLine("Person's Name: {0}", name);
        }
    }
        public class Student : Person
        {
            protected int class_id;
            protected int score;
            public Student(int pid, string pname, int sclass_id, int sscore)
                : base(pid, pname)
            {
                class_id = sclass_id;
                score = sscore;
            }
            public new void infoPrint()
            {
                Console.WriteLine("Person's Id: {0}", id);
                Console.WriteLine("Person's Name: {0}", name);
                Console.WriteLine("Person's Class_id: {0}", class_id);
                Console.WriteLine("Person's Score: {0}", score);        }
        }
        public class Teacher : Person
        {
            protected String position;
            protected string department;
            public Teacher(int pid, string pname, String tposition,String tdepartment) : base(pid, pname)
            {
              position = tposition;
                department = tdepartment;
            }
            public override void infoPrint()
            {
                Console.WriteLine("Person's Id: {0}", id);
                Console.WriteLine("Person's Name: {0}", name);
                Console.WriteLine("Person's Position: {0}", position);
                Console.WriteLine("Person's Department: {0}", department);
     
            }
        }
         
          public class Program
          {
              static void Main(string[] args)
              {
                  Person person = new Person(1, "a");
                  Teacher teacher = new Teacher(86, "王宁", "jiaoshou", "jisuanji");
                  Student student = new Student(101, "李刚", 2, 90);          }
          }
         
    }
      

  3.   

    1、构造函数不继承,派生类会自动调用基类构造函数。
    2、若类内没有定义构造函数,系统会自动隐式生成一个不带参数的构造函数,比如定义一个类:
    public class A
    {
     }
    可以理解为它已经存在一个如下的构造函数public class A
    {
        public A()
        {
           
        }
    }
    3、派生类构造函数自动调用基类的不带参数的构造函数,注意下面的格式
    public class B:A
    {
        public B()
        {
           
        }
    }
    相当于
    public class B:A
    {
        public B():base()
        {
           
        }
    }3、基类中带参数的构造函数必须显式调用,比如:
    public class A
    {
        public A()
        {
           
        }
        public A(string str)
        {
           
        }}
    public class B:A
    {
        public B():base("aaa")
        {
           
        }
    }
      

  4.   

    为什么还显示错误 1 应输入 class、delegate、enum、interface 或 struct C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\projects\ConsoleApplication2\ConsoleApplication2\Program.cs 35 17 ConsoleApplication2
    这个错误呢
      

  5.   

    那只能说明你连复制粘贴都不会了。我的代码编译通过,直接从IDE粘贴出来,你都能搞出错误。那还能说什么好。
      

  6.   

    那只能说明你连复制粘贴都不会了。我的代码编译通过,直接从IDE粘贴出来,你都能搞出错误。那还能说什么好。看了下,是没添加program类,错误是没有,为什么执行完是请按任意键继续呢?
      

  7.   

    那只能说明你连复制粘贴都不会了。我的代码编译通过,直接从IDE粘贴出来,你都能搞出错误。那还能说什么好。看了下,是没添加program类,错误是没有,为什么执行完是请按任意键继续呢?难道有什么不对么?
      

  8.   

    那只能说明你连复制粘贴都不会了。我的代码编译通过,直接从IDE粘贴出来,你都能搞出错误。那还能说什么好。看了下,是没添加program类,错误是没有,为什么执行完是请按任意键继续呢?现在可以了,原来是因为没调用成员方法。那可以不用program类么,直接放个主函数在程序里不行吗?
      

  9.   

    那只能说明你连复制粘贴都不会了。我的代码编译通过,直接从IDE粘贴出来,你都能搞出错误。那还能说什么好。看了下,是没添加program类,错误是没有,为什么执行完是请按任意键继续呢?现在可以了,原来是因为没调用成员方法。那可以不用program类么,直接放个主函数在程序里不行吗?
    你可以把Main写在任意类中,但是你觉得写在Teacher、Person、Student中合适么?是不是要再写一个类?那么这个类叫什么好呢?不叫Program你打算叫什么?
      

  10.   

    本帖最后由 caozhy 于 2014-04-23 18:42:46 编辑