您的位置:首页 > 博客中心 > 互联网 >

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

时间:2022-04-22 01:07

对象是一个自包含的实体,用一组可识别的特性和行为来标识.

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

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

在编程过程中注意:

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

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

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

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

下面是一个例子:

这是一个类,

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace AnimalGames
 6 {
 7     class Cat
 8     {
 9         public string Shout()
10         {
11             return "瞄";
12         }
13     }
14 }

调用这个类,

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace AnimalGames
10 {
11     public partial class Form1 : Form
12     {
13         public Form1()
14         {
15             InitializeComponent();
16         }
17 
18         private void button1_Click(object sender, EventArgs e)
19         {
20             Cat cat = new Cat();//将cat实例化
21             //注意:Cat cat = new Cat();其实做了两件事,
22             //Cat cat;声明一个Cat的对象,对象名是cat
23             //cat = new Cat();将此cat对象实例化
24             MessageBox.Show(cat.Shout());
25         }
26 
27     }
28 }

附录A培训实习生-面向对象基础(1):类和实例,布布扣,bubuko.com

本类排行

今日推荐

热门手游