:
,如下所示:
class 派生类 : 基类{
... ...
}
using System; namespace task.lmcjl.com { class Demo { static void Main(string[] args) { Rectangle oblong = new Rectangle(); oblong.setWidth(3); oblong.setHeight(4); int area = oblong.getArea(); Console.WriteLine("长方形的面积为:{0}", area); } } // 基类 class Shape{ protected int width, height; public void setWidth(int w){ width = w; } public void setHeight(int h){ height = h; } } // 派生类 class Rectangle : Shape{ public int getArea(){ return width*height; } } }运行结果如下:
长方形的面积为:12
using System; namespace task.lmcjl.com { class Demo { static void Main(string[] args) { Rectangle oblong = new Rectangle(); oblong.setWidth(3); oblong.setHeight(4); int area = oblong.getArea(); int girth = oblong.getGirth(); Console.WriteLine("长方形的面积为:{0}", area); Console.WriteLine("长方形的周长为:{0}", girth); } } // 基类 class Shape{ protected int width, height; public void setWidth(int w){ width = w; } public void setHeight(int h){ height = h; } } // 定义接口 public interface Perimeter{ int getGirth(); } // 派生类 class Rectangle : Shape, Perimeter{ public int getArea(){ return width*height; } public int getGirth(){ return (width+height)*2; } } }运行结果如下:
长方形的面积为:12
长方形的周长为:14
本文链接:http://task.lmcjl.com/news/5155.html