面向对象编程
来源:互联网 发布:java项目如何上线 编辑:程序博客网 时间:2024/06/06 03:59
using System;
class CRect
{
private int top,bottom,left,right;
public static int total_rects=0;
public static long total_rect_area=0;
public CRect()
{
left=top=right=bottom=0;
total_rects++;
total_rect_area+=getHeight()*getWidth();
Console.WriteLine("CRect() Constructing rectangle number {0} " ,total_rects );
Console.WriteLine("Total rectangle areas is: {0}",total_rect_area);
}
public CRect(int x1,int y1,int x2,int y2)
{
left=x1; top=y1;
right=x2; bottom=y2;
total_rects++;
total_rect_area+=getHeight()*getWidth();
Console.WriteLine("CRect(int,int,int,int) Constructing rectangle number {0} ",total_rects);
Console.WriteLine("Total rectangle areas is: {0}", total_rect_area );
}
public CRect(CRect r)
{
left=r.left; right=r.right;
top=r.top; bottom=r.bottom;
total_rects++;
total_rect_area+=getHeight()*getWidth();
Console.WriteLine("CRect(CRect&) Constructing rectangle number {0}", total_rects);
Console.WriteLine("Total rectangle areas is: {0}",total_rect_area );
}
public int getHeight()
{ return top>bottom ? top-bottom : bottom-top; }
public int getWidth()
{ return right>left ? right-left : left-right; }
public static int getTotalRects()
{ return total_rects; }
public static long getTotalRectArea()
{ return total_rect_area; }
}
public class Test31
{
public static void Main ( )
{
CRect rect1=new CRect(1,3,6,4),rect2=new CRect(rect1);
Console.Write("Rectangle 2: Height: {0}", rect2.getHeight() );
Console.WriteLine(", Width: {0}", rect2.getWidth());
{ //注释1
CRect rect3 = new CRect();
Console.Write("Rectangle 3: Height: {0}", rect3.getHeight()) ;
Console.WriteLine( ", Width: {0}", rect3.getWidth());
} //注释2
Console.Write("total_rects={0}", CRect.total_rects);
Console.WriteLine(" total_rect_area={0}", CRect.total_rect_area);
Console.Read();
}
2. 编辑、编译和运行程序,运行结果如图T3.1所示。
图T3.1 程序运行结果
- 面向对象/面向过程编程
- 什么是面向对象编程?
- JavaScript 面向对象编程
- JavaScript 面向对象编程
- PHP 面向对象编程
- PHP面向对象编程
- javascript面向对象编程
- 面向对象编程
- 面向对象编程
- 面向对象编程概要
- 面向对象编程,继承
- 面向对象编程,多态性
- 面向对象编程
- 面向对象编程
- javascript 面向对象编程
- JavaScript面向对象编程
- C#面向对象编程
- JavaScript 面向对象编程
- 变形动画之kinect模块
- 安装visual studio 2003重启问题
- 用指针访问类中private成员
- 改变文件所有者及所属群组的chown,chgrp
- Python网页抓取:获取页面中某段内容的xpath
- 面向对象编程
- 条件分支结构(switch-case)(if-else if-else) 2011.05.09
- ubuntu10.10 使用mrtg监控服务器的cpu、内存、网络等等情况
- Chord算法(原理)
- js中的prototype对象的理解
- apt-get 下载后,软件所在的路径
- Linux no acceptable C compiler found in $PATH错误
- oracle数据误删后恢复
- bus,device,driver三者关系