资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
实验内容(步骤、表格、算法或流程图等)1. 演示抽象类与抽象属性的使用2. 演示类的多态性源程序及注释(不足可加附页)1 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1public abstract class Shape /表示“形状” 的抽象类型private string m_id; /定义形状的m_id,一个私有字段/声明构造函数,用来设置属性IDpublic Shape(string s)ID=s;public string ID /定义属性,用来操作私有字段m_idgetreturn m_id;setm_id=value;/定义一个只读的抽象类型,表示形状的面积public abstract double Areaget;/重载从Object类继承来的方法public override string ToString()return ID +面积= +string.Format(0:F2,Area);public class Square:Shape /正方形,从从抽象类Shapeprivate int m_side;/正方形的边长 public Square (int side,string id):base(id)m_side =side;/重载继承的抽象属性,取得正方形的面积public override double Areagetreturn m_side*m_side ;class TestClassstatic void Main(string args)/创建一个边长为5的正方形对象Square s=new Square(5,正方形);string result=s.ToString();Console .WriteLine (result);2. using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1public class Personpublic string Name;public char Sex;public Person(string name, char sex)Name = name; Sex = sex;public virtual string Answer()return string.Format(姓名:0,宁别:1., Name, Sex);public class Student : Personpublic string School;public int Score;public Student(string name, char sex, string school, int score): base(name, sex)School = school;Score = score;public override string Answer()return string.Format(姓名:0,性别:1n学校:2,成绩3分.,Name, Sex, School, Score);public class Worker : Personpublic string Department;public float Salary;public Worker(string name, char sex, string depart, float salary): base(name, sex)Department = depart;Salary = salary;public override string Answer()return string.Format(姓名:0,性别:1n部门:2,薪水:3元.,Name, Sex, Department, Salary); class Programstatic void Main(string args)Student s = new Student(张伟, 男, 电子科大成都学院, 480);Console.WriteLine(该学生信息如下:);Console.WriteLine(s.Answer();Worker w = new Worker(王刚, 男, 生产车间, 1500);Console.WriteLine(该员工信息如下:);Console.WriteLine(w.Answer();运行结果及分析1.2.
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号