标签:style blog ar sp div on log bs ad
今天在云和学院学习了类
[访问修饰符] class 类名
{
成员;
......
}
类中可以包含变量的定义、方法
public: 公共成员,完全公开,没有访问限制。
private : 私有成员, 在类的内部才可以访问。
面向对象的原则:封装、继承、多态。
实例:定义一个学生类,有六个属性,分别为姓名、性别、年龄、语文、数学、英语成绩。
class student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string gender;
public string Gender
{
get { return gender; }
set { gender = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private int chinese;
public int Chinese
{
get { return chinese; }
set { chinese = value; }
}
private int math;
public int Math
{
get { return math; }
set { math = value; }
}
private int english;
public int English
{
get { return english; }
set { english = value; }
}
}
static void Main(string[] args)
{
student s = new student() {Name="王丽",Gender="女",Age=18,Chinese=90,Math=95,English=80 };
student s1 = new student() { Name = "张伟", Gender = "男", Age = 16, Chinese = 95, Math = 85, English = 100 };
Console.WriteLine("{0},{1},{2},总和为{3},平均分{4}",s.Name,s.Gender,s.Age,s.Chinese+s.Math+s.English,(s.Chinese+s.Math+s.English)/3);
Console.WriteLine("{0},{1},{2},总和为{3},平均分{4}", s1.Name, s1.Gender, s1.Age, s1.Chinese + s1.Math + s1.English,(s1.Chinese + s1.Math + s1.English)/3);
Console.ReadKey();
}
如果你自己没有定义一个构造函数,那么系统会给我们提供构造函数。
标签:style blog ar sp div on log bs ad
原文地址:http://www.cnblogs.com/songfang/p/4095739.html