您的位置:首页 > 其它

附录A培训实习生-面向对象基础类和实例(1)

2014-08-14 20:52 288 查看
对象是一个自包含的实体,用一组可识别的特性和行为来标识.

面向对象编程,Object-Oriented Programming,其实就是针对对象进行编程的意思.

类就是具有相同属性和功能的对象的抽象的集合.

在编程过程中注意:

第一,类名称首字母记着要大写.多个单词则各个首字母大写.

第二,对外公开的方法需要用public修饰符.

实例,就是一个真实的对象.

实例化就是创建对象的过程,使用new关键字来创建.

下面是一个例子:

这是一个类,

using System;
using System.Collections.Generic;
using System.Text;

namespace AnimalGames
{
class Cat
{
public string Shout()
{
return "瞄";
}
}
}


调用这个类,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AnimalGames
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Cat cat = new Cat();//将cat实例化
//注意:Cat cat = new Cat();其实做了两件事,
//Cat cat;声明一个Cat的对象,对象名是cat
//cat = new Cat();将此cat对象实例化
MessageBox.Show(cat.Shout());
}

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