今天开始学C#里面的面相对像,老师弄了一个例子给们讲了一下怎样创建类,好多人没怎么听明白,
我觉得那个例子有问题。
谁可以给我几个学C#类的例子啊。谢谢了!

解决方案 »

  1.   

    看看MSDN和面向对象书籍
    实例51aspx.com里很多
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
       public  class Person
        {
           protected    string name, sex, id;
          protected   int age;
          public Person()
          { ; }
            public Person(string name, string id, string sex, int age)
            {
                this.name = name;//对成员变量进行赋值(通过构造函数(重载))
                this.id = id;
                this.sex = sex;
                this.age = age;
            }
        }
        public class Student : Person//Student继承Person这个类
        {
           protected  string major;
            public Student ()
            { ; }
            public Student(string name, string id, string sex, int age, string major)
            {
                this.name = name;
                this.id = id;
                this.sex = sex;
                this.age = age;
                this.major = major;
                Console.WriteLine("Student类的构造函数!");
            }
            public void say()//一个方法!
            {
                Console.WriteLine("姓名={0}\n学号={1}\n性别={2}\n年龄={3}\n专业={4}", name, id, sex, age, major);
            }
        }
        public class Teacher : Person//Teacher继承Person这个类
        {
           protected  string position;
           public Teacher()
           { ; }
            public Teacher(string name, string id, string sex, int age, string position)
            {
                this.name = name;
                this.id = id;
                this.sex = sex;
                this.age = age;
                this.position = position;
                Console.WriteLine("Teacher类的构造函数!");
            }
            public void dispaly()
            {
                Console.WriteLine("姓名={0}\n学号={1}\n性别={2}\n年龄={3}\n职位={4}", name, id, sex ,age , position);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {     //创建一个Student类的对象实例stu
                Student stu = new Student("jm", "23","男",20, "计算机");
                   //通过实例来调用方法
                stu.say();
                Teacher teacher = new Teacher("cg", "100","女",30, "班主任");
                teacher.dispaly();
                Console.Read();
            }
        }
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
       public  class Person
        {
           protected    string name, sex, id;
          protected   int age;
          public Person()
          { ; }
            public Person(string name, string id, string sex, int age)
            {
                this.name = name;//对成员变量进行赋值(通过构造函数(重载))
                this.id = id;
                this.sex = sex;
                this.age = age;
            }
        }
        public class Student : Person//Student继承Person这个类
        {
           protected  string major;
            public Student ()
            { ; }
            public Student(string name, string id, string sex, int age, string major)
            {
                this.name = name;
                this.id = id;
                this.sex = sex;
                this.age = age;
                this.major = major;
                Console.WriteLine("Student类的构造函数!");
            }
            public void say()//一个方法!
            {
                Console.WriteLine("姓名={0}\n学号={1}\n性别={2}\n年龄={3}\n专业={4}", name, id, sex, age, major);
            }
        }
        public class Teacher : Person//Teacher继承Person这个类
        {
           protected  string position;
           public Teacher()
           { ; }
            public Teacher(string name, string id, string sex, int age, string position)
            {
                this.name = name;
                this.id = id;
                this.sex = sex;
                this.age = age;
                this.position = position;
                Console.WriteLine("Teacher类的构造函数!");
            }
            public void dispaly()
            {
                Console.WriteLine("姓名={0}\n学号={1}\n性别={2}\n年龄={3}\n职位={4}", name, id, sex ,age , position);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {     //创建一个Student类的对象实例stu
                Student stu = new Student("jm", "23","男",20, "计算机");
                   //通过实例来调用方法
                stu.say();
                Teacher teacher = new Teacher("cg", "100","女",30, "班主任");
                teacher.dispaly();
                Console.Read();
            }
        }
    }
      

  4.   

    最近总结的几个例子:C# word控件编程C#调用WebServer示例(图文) 好像是个新手,看能看懂吗?