黑马程序员技术交流社区

标题: 输出类中无参方法的问题 [打印本页]

作者: 9881008杜鹏    时间: 2011-11-27 00:14
标题: 输出类中无参方法的问题
本帖最后由 9881008杜鹏 于 2011-11-27 00:28 编辑

abstract class Person
    {
        public Person(string vocation, string name, int age)
        {
            this.vocation = vocation;
            this.name = name;
            this.age = age;
        }
        protected string vocation;
        protected string name;
        protected int age;
        public string Vocation { set { vocation = value; } get { return vocation; } }
        public string Name { set; get; }
        public int Age { set; get; }
       public  abstract void  SayHello();
       public  abstract void StandUp();
    }
class Teacher: Person
    {
        public Teacher(string vocation, string name, int age, int salary):base(vocation ,name,age)//在被继承的构造函数中(base中没有类型(如string int))
        {
            ////this.vocation = base.vocation;
            ////this.Name = base.name;
            //this.age =  base.age;
            this.salary = salary;
            Console.WriteLine("职业类型:{0} \n 姓名:{1} \n 年龄:{2} \n 工资:{3}RMB(月薪)。",vocation ,name ,age ,salary );
        }
        private int salary;
        public int Salary { set; get; }
        public override void SayHello()
        {
            Console.WriteLine("Good morning every one  !");
        }
        public override void StandUp()
    {
      
        Console.WriteLine(" Please sit down !");
    }
    }
class Student: Person
    {
        public Student(string vocation, string name, int age, int grade)
            : base(vocation, name, age)
        {
            this.grade = grade;
            Console.WriteLine("职业类型:{0} \n 姓名:{1} \n 年龄:{2} \n 成绩:{3}", vocation, name, age, grade );
        }
        private int grade ;
        public int Grade{get;set;}
        public override void SayHello()
        {
            Console.WriteLine("Good morning teacher ! ");
        }
        public override void StandUp()
        {
            Console.WriteLine(" Please stand up ! ");
        }
    }
static void Main(string[] args)
        {
            Person [] person = new Person[2];
            person[0] = new Student("学生","Tom",12,399);
            person[1] = new Teacher ("老师", "杨中科", 12, 99999);
            person[0].StandUp();
            person[1].SayHello();
            person[0].SayHello();
            person[1].StandUp();
            for (int i = 0; i < person.Length; i++)
            {
                Console.WriteLine(person.SayHello(), person.StandUp());//最匹配的重载方法具有一些无效的参数        
      }
            Console.ReadKey();
怎么回事呢?


作者: 李荣壮    时间: 2011-11-27 01:35
Console.WriteLine(person.SayHello(), person.StandUp());

不能重载, 是因为Console.WriteLine()  没有这个输出方法,
你要输入两个以上 就要使用占位符了

Console.WriteLine("{0} {1}",person.SayHello(), person.StandUp());




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2