您的位置:首页 > 编程语言 > C语言/C++

初识.net界面程序(6)——类及其属性和方法的实现练习

2017-04-29 21:38 459 查看
编写一个windows窗体应用程序,实现以下功能:

(1)定义一个Course Info类,该类包含一下成员

+ 具有Course Name(课程名)、CourseTime(开课时间)、BookName(书名)、Price(定价)4个属性,其中开课时间为枚举类型(秋季、春季)

+ 具有一个静态变量Counter,每创建一个Course实例,该变量自动加一

+ 提供无参构造函数和有参构造函数,在构造函数中设置相关属性

+ 提供一个Print方法,显示该实例的4个属性值

(2)在主窗体的代码中,分别创建courseInfo实例,测试该类提供的功能,并将结果在List Box中显示出来



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

namespace WindowsFormsApplication1
{
enum Course_time{春季,秋季 }

class CourseInfo
{
public string CouserName { get; set; }
public string CourseTime { get; set; }
public string BookName { get; set; }
public string Price { get; set; }

public CourseInfo()
{
this.CouserName = "数据结构";
this.CourseTime = "春季";
this.BookName = "《数据结构》";
this.Price = "40";
}

public CourseInfo(string Coursename,string Coursetime,string Bookname,string Prices)
{
this.CouserName = Coursename;
this.CourseTime = Coursetime;
this.BookName = Bookname;
this.Price = Prices;
}
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class WebCam : Form
{
public WebCam()
{
InitializeComponent();
}

static int Counter=0;

string Print(CourseInfo cour)
{

return cour.CouserName+"   "+cour.CourseTime+"    " + cour.BookName+"   "+ cour.Price;
}

private void WebCam_Load(object sender, EventArgs e)
{
CourseInfo cour1 = new CourseInfo();
CourseInfo cour2 = new CourseInfo("操作系统", "秋季", "《操作系统》", "45");
CourseInfo cour3 = new CourseInfo("软件工程", "春季", "《软件工程》", "30");
courselist.Items.Add(Print(cour1));
courselist.Items.Add(Print(cour2));
courselist.Items.Add(Print(cour3));
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息