金陵科技学院实验报告
实验项目名称: 面向对象编程 实验学时: 6 同组学生姓名: 无 实验地点: A104 实验日期: 实验成绩: 批改教师: 马青霞 批改时间:
9
金陵科技学院实验报告
实验2 面向对象编程
一、实验目的、要求
1、理解面向对象程序设计的思想和基本概念; 2、掌握类的定义和使用;
3、掌握类的数据成员,属性的定义和使用;
4、掌握方法的定义,调用和重载以及方法参数的传递; 5、掌握构造函数和析构函数的定义和使用。
7、掌握虚方法的定义以及如何用虚方法实现多态; 8、掌握抽象类的定义以及如何用用抽象方法实现多态; 9、掌握集合的创建和操作方法; 10、掌握接口的定义及使用方法。 二、实验要求
1、编写程序要规范、正确,上机调试过程和结果要有记录; 2、做完实验后给出本实验的实验报告。 三、实验设备、环境
安装有VS.Net 2005以上版本软件。 四、实验内容
1、利用方法的重载两个整数和两个双精度类型数据求和。
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
namespace _1 {
class Program {
static void Main(string[] args) {
Console.WriteLine(\整型数据相加:\ Add intadd = new Add(1, 2); intadd.Result();
Console.WriteLine(\双精度数据相加:\ Add doubleadd = new Add(1.1, 2.5); doubleadd.Result(); } }
class Add {
public double add = 0;
public Add(double x, double y) {
add = x + y;
10
金陵科技学院实验报告
}
public Add(int x, int y) {
add = x + y; }
public void Result() {
Console.WriteLine(add); } } }
2、定义一个Area类,用构造函数重载,实现矩形的面积,圆的面积,梯形的面积,定义一个ShowArea方法,显示结果。
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
double len = double.Parse(txtlength.Text);
11
金陵科技学院实验报告
double wid = double.Parse(txtwidth.Text); double r = double.Parse(txtr.Text); double sd = double.Parse(txtsd.Text); double xd = double.Parse(txtxd.Text); double hei = double.Parse(txtheight.Text); Area a1 = new Area(len,wid); Area a2 = new Area(r);
Area a3 = new Area(sd, xd, hei); txtrec.Text = a1.ShowArea(len,wid); txtcir.Text = a2.ShowArea(r);
txtech.Text = a3.ShowArea(sd, xd, hei); } } }
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace WindowsFormsApplication4 {
class Area {
double len, wid, r, sd, xd, hei;
public Area(double len, double wid) {
this.len = len; this.wid = wid; }
public Area(double r) {
this.r = r; }
public Area(double sd, double xd, double hei) {
this.sd = sd; this.xd = xd; this.hei = hei; }
public string ShowArea(double len,double wid) {
return (len * wid) + \ }
public string ShowArea(double r) {
12
金陵科技学院实验报告
return (3.14 * r * r) + \ }
public string ShowArea(double sd, double xd, double hei) {
return ((sd + xd) * hei * 0.5) + \ } } }
3、定义一个Students类,包括学号、姓名、性别、年龄4个属性,要求在性别属性中增加....对性别的判断(只能输入男和女),在年龄属性中提供对不合法(年龄在10至50之间)输....入的判断,并提供方法显示学生信息。构建该类,并测试。 using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
namespace _3 {
class Student {
int id, age;
string name, sex; public int _id
13