C#-类的应用

/*
 * 类的综合应用。创建一个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();
        } 
    }
}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值