C#面向对象初步创建类的对象

 时间:2024-10-13 22:54:03

1、打开VS2015,在‘ObjectLearn’解决方案下,新建一个控制台项目‘ObjectLearn1’

C#面向对象初步创建类的对象

2、在项目‘ObjectLearn1’下,新建一个类‘animal’

C#面向对象初步创建类的对象

3、在新建的‘animal.cs’类中写如下代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObjectLearn{ class animal { public string _name; public int _age; public void eat() { System.Console.WriteLine("I am hungry"); } }}注意几点1 命名空间名要与Program.cs文件的一致,不然无法识别2 animal类的字段及方法要public,不然无法外部无法访问

C#面向对象初步创建类的对象

4、在Program.cs文件写代码实例化animal类,并通过对象调用方法如下:us坡纠课柩ing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ObjectLearn{ class Program { static void Main(string[] args) { animal ani = new animal(); ani._name = "xiaohong"; ani._age = 25; ani.eat(); System.Console.ReadKey(); } }}

C#面向对象初步创建类的对象

5、在项目名‘ObjectLearn1’右键,在弹出菜单,选择设为启动项目

C#面向对象初步创建类的对象

6、点击vs2015上方的启动按钮,运行程序

C#面向对象初步创建类的对象

7、程序运行,弹出命令行窗口,打印出I am hungry命令行窗口不会消失,是因为System.Console.ReadKey();不然程序会一闪而过

C#面向对象初步创建类的对象
  • 用Axure rp实现在当前窗口打开链接
  • 完美钢琴手机版如何更换皮肤
  • 关于html内嵌video的一些参数设置
  • 幻塔体力有什么用
  • javascript怎样获取动态生成div的id值
  • 热门搜索
    京东便利店怎么样 怎么存钱利息最高 只读文件怎么修改 飞机上可以带烟吗 绍兴旅游景点 龙岩旅游 深圳市旅游景点大全 活螃蟹怎么保存过夜 国家5a旅游景区名单 痘疤怎么去除