面向对象--继承练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace winformjicheng
{
class Program
{
static void Main(string[] args)
{
student stu =new student("张飞","001","男",80);
stu.studentsayscore();
program1 pro = new program1("王五","008","男",7);
pro.program1say();
}
}

// 基类(父类)
class person
{


private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private string code;

public string Code
{
get { return code; }
set { code = value; }
}
private string sex;

public string Sex
{
get { return sex; }
set { sex = value; }
}
// 构造函数(字段初始化)
public person(string name, string code, string sex)
{

this.name = name;
this.code = code;
this.sex = sex;
}
}


//程序员类、、、、、、、、、子类 派生类、、、、、、、、、、、、、、、

 

class program1 : person // 继承person
{
//成员字段
private int year;
//封装字段(ctrl+r +e)
public int Year
{
get { return year; }
set { year = value; }
}
//显示调用基类属性
public program1(string name, string code, string sex, int year)
: base(name, code, sex)
{
this.year = year;

}
// 方法
public void program1say()
{
Console.WriteLine("我叫{0},学号是{1},我是{2}生,我工作{3}年了", this.Name, this.Code, this.Sex, this.year);
}


}
// 学生类、、、、、、、、、、、、子类派生类、、、、、、、、、、、、、、
class student : person
{
private decimal score;

public decimal Score
{
get { return score; }
set { score = value; }
}

public student(string name, string code, string sex, decimal score)
: base(name, code, sex)
{
this.score = score;

}

public void studentsayscore()
{
Console.WriteLine("我叫{0},学号是{1},我是{2}生,今年考了{3}分", this.Name, this.Code, this.Sex, this.score);

}


}

 


}

posted on 2015-07-27 11:40  a-po  阅读(194)  评论(0编辑  收藏  举报