推荐几个学习C#类的几个例子 今天开始学C#里面的面相对像,老师弄了一个例子给们讲了一下怎样创建类,好多人没怎么听明白,我觉得那个例子有问题。谁可以给我几个学C#类的例子啊。谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看MSDN和面向对象书籍实例51aspx.com里很多 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(); } }} 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(); } }} 最近总结的几个例子:C# word控件编程C#调用WebServer示例(图文) 好像是个新手,看能看懂吗? C#前台使用后台变量时有引号被转义 问一个没有技术含量的问题 急!!!treeView前面的“+”号不点击,不显示! 动态加载36个Panel,在上面放置几个Lable,如何清除缓冲 很久没来了,问一个比较有挑战性的问题 请大哥大姐帮帮忙,要怎么才可以,最好的方法用子窗体修改父窗体的菜单属性呢,使主窗体菜单有效.. 函数中的static 变量怎么使用 checkbox选择问题 TCP通讯中字符设计问题 关于out参数的使用 又一个字符串截取问题 有办法只显示控件的一部分吗?
实例51aspx.com里很多
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();
}
}
}
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();
}
}
}