c#中怎么定义一个类

有只小跳蛙
浏览 790回答 2
2回答

千万里不及你

using System;public class Desk//基类Desk{ protected int length;//保护成员 protected int width; protected int height; //类Desk的构造函数 public Desk( ) { length = 0; width = 0; height = 0; } //设置Desk的信息 public void SetInfo( int Len, int Wid, int Hei ) { length = Len; width = Wid; height = Hei; } //打印Desk的参数信息 public void ShowInfo( ) { Console.WriteLine("Length={0} \t Width={1} \t Height={2}", length, width, height); }}public class Furniture : Desk//定义基类Desk的派生类Furniture{ private int price;//私有成员 //类Furniture的构造函数 public Furniture ( ) { //这里会隐式调用基类Desk的构造函数 //Desk( );//若显示调用会出现错误 price = 0; } //重载该类的SetInfo函数 public void SetInfo( int Len, int Wid, int Hei, int Pri) { length = Len; width = Wid; height = Hei; price = Pri; } //新增的函数用以设置价格 public void SetPri( int Pri) { price = Pri; } //重定义ShowInfo函数 public new void ShowInfo( ) //必须加上关键字new,否则会引发一个生成错误 { Console.WriteLine("Length={0} \t Width={1} \t Height={2} \t Price={3}",length, width, height, price); }}class Test{ public static void Main() { Furniture fur1 = new Furniture( ); //隐式调用构造函数 Console.WriteLine("Fur1初始化后的值为:"); fur1.ShowInfo( ); //显示家具信息 fur1.SetInfo(80,50,60,350); Console.WriteLine("Fur1设置具体信息后为:"); fur1.ShowInfo( ); fur1.SetPri(288); //重置家具价格 Console.WriteLine("Fur1价格大优惠:"); fur1.ShowInfo( ); }}   Console.ReadKey();        }    }}

杨__羊羊

eg.public class Example{}就定义了一个简单的类,叫做Example,虽然没有任何行为……
打开App,查看更多内容
随时随地看视频慕课网APP