您当前的位置: 首页 >  c#

C#中的类声明和对象定义方法实验

发布时间:2021-04-16 10:43:30 ,浏览量:5

功能

设计控制台应用程序,声明一个人类Person和一个动物类Animal,它们都包含有公有字段legs(腿的数目)和保护字段weight(重量),定义它们的对象并输出相关数据。

程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Person { // int x,y; public int legs = 2; protected int weight = 50; public void print() { Console.WriteLine("某人有{0}只腿,重量为{1}kg", legs, weight); } } class Animal { protected int weight = 120; public const int legs = 4; public void print() { Console.WriteLine("某动物有{0}只腿,重量为{1}kg", legs, weight); } } class Program { static void Main(string[] args) { Person ren = new Person(); Animal shou = new Animal(); ren.print(); shou.print(); } } } 
运行结果

在这里插入图片描述

后续

如果想了解更多物联网、智能家居项目知识,可以关注我的程序设计专栏。 或者关注公众号。 在这里插入图片描述

编写不易,感谢支持。

关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.5481s