C#-类的应用
2016-04-08 17:09
459 查看
/* * 类的综合应用。创建一个WindowsForm应用程序,在此实例中自定义了一个按钮,运用了类的继承、构造函数、this、base、override等知识点。 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CryButton cb = new CryButton(); //声明类的实例 cb.Location=new Point(50,50); this.Controls.Add(cb); //将实例(按钮)添加到当前窗体 cb.Click+=new EventHandler(cb_Click); //委托 } void cb_Click(object sender,EventArgs e){ //自定义方法 this.Text="自定义按钮"; } } public class CryButton : Button { public CryButton() { this.Width = 100; this.Height = 100; } protected override void OnPaint(PaintEventArgs pevent) //重写父类的虚方法 { //base.OnPaint(pevent); //直接调用父类的虚方法 Graphics g = pevent.Graphics; g.Clear(SystemColors.Control); SolidBrush brush = new SolidBrush(Color.Red); g.FillEllipse(brush, new Rectangle(0, 0, 100, 100)); } } }
运行结果:
当按钮按下时
注意:
方法的重载、覆盖(重写)与隐藏
重载:与父类同名,但签名不同的方法;
隐藏:定义与父类同名且签名也相同的方法。需要在子类的方法前加new;
重写:定义与父类同名且签名也相同的方法,但父类的方法前用virtual或abstract进行了修饰,子类的同名方法前同样用overrride进行了修饰。父类中称为虚方法、抽象方法。
抽象方法是不完整的,没有具体实现,不能被执行;而虚方法是完整的方法,是可以执行的。
密封类(sealed class)是不允许其他类继承的类。
密封方法不允许被重写。
1、子类隐藏了父类方法,如果要想在子类中调用父类方法,需要在子类方法前加base。如:base.MyMethod()。
2、子类在自己的构造函数中,可以使用base()来调用父类的构造函数,必要时还可带上相应的参数。
如:public Student:Person{ private int classID; public Student(string name,int ClassID)
:base (Name) {this.classID=ClassID;} }
/* 把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。 要求成员函数能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造完整程序。 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { CPoint p1 = new CPoint(3.0, 2.5), p2 = new CPoint(3.0, 4.1),p3=new CPoint(5.0,2.5); Cline cl1 = new Cline(); Cline cl2 = new Cline(); Console.WriteLine("两点间的距离为:{0}", cl1.distance(p1, p2)); cl1.set_length(p1, p2); cl2.set_length(p1, p3); CRect cr = new CRect(); Console.WriteLine("矩形的周长为:{0}", cr.perimeter(cl1, cl2)); Console.WriteLine("矩形的面积为:{0}", cr.area(cl1, cl2)); Console.ReadKey(); } } class CPoint { private double x; private double y; public CPoint() { } public CPoint(double x, double y) { this.x = x; this.y = y; } public double get_x() { return x; } public double get_y() { return y; } } class Cline : CPoint { public double d; public Cline() { d = 0; } public Cline(double d) { this.d= d; } public double distance(CPoint a, CPoint b) { d = Math.Sqrt((a.get_x() - b.get_x()) * (a.get_x() - b.get_x()) + (a.get_y() - b.get_y()) * (a.get_y() - b.get_y())); return d; } public double get_length() { return d; } public void set_length(CPoint a, CPoint b) { d = distance(a , b ); } } class CRect : Cline { double z; double m; public CRect() { } public CRect(double a, double b) { z = a; m = b; } public double perimeter(Cline c1, Cline c2) { return z = 2 * (c1.get_length() + c2.get_length()); } public double area(Cline c1, Cline c2) { return m = c1.get_length() * c2.get_length(); } } }
运行结果:
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法