对继承的一些理解
From :《设计模式》第5章
学习设计模式以前必须了解继承的基本概念
主程序代码:
 using System;
using System;
 using System.Drawing;
using System.Drawing;
 using System.Collections;
using System.Collections;
 using System.ComponentModel;
using System.ComponentModel;
 using System.Windows.Forms;
using System.Windows.Forms;
 using System.Data;
using System.Data;

 namespace RectDraw
namespace RectDraw
 {
{
 /// <summary>
    /// <summary>
 /// Draws a rectangle and a square
    /// Draws a rectangle and a square
 /// </summary>
    /// </summary>
 public class Form1 : System.Windows.Forms.Form
    public class Form1 : System.Windows.Forms.Form
 {
    {
 /// <summary>
        /// <summary>
 /// Required designer variable.
        /// Required designer variable.
 /// </summary>
        /// </summary>
 private System.ComponentModel.Container components = null;
        private System.ComponentModel.Container components = null;
 private CsharpPats.Rectangle rect;
        private CsharpPats.Rectangle rect;
 private System.Windows.Forms.PictureBox pic;
        private System.Windows.Forms.PictureBox pic;
 private Square sq;
        private Square sq;
 private void init() {
        private void init() {
 rect = new CsharpPats.Rectangle (10, 20, 70, 100);
            rect = new CsharpPats.Rectangle (10, 20, 70, 100);
 sq = new Square (150,100,70);
            sq = new Square (150,100,70);
 }
        }
 public Form1()
        public Form1()
 {
        {
 InitializeComponent();
        InitializeComponent();
 init();
        init();
 }
        }
 protected override void OnPaint(PaintEventArgs pa) {
        protected override void OnPaint(PaintEventArgs pa) {
 }
        }
 /// <summary>
        /// <summary>
 /// Clean up any resources being used.
        /// Clean up any resources being used.
 /// </summary>
        /// </summary>
 protected override void Dispose( bool disposing )
        protected override void Dispose( bool disposing )
 {
        {
 if( disposing )
            if( disposing )
 {
            {
 if (components != null)
                if (components != null) 
 {
                {
 components.Dispose();
                    components.Dispose();
 }
                }
 }
            }
 base.Dispose( disposing );
            base.Dispose( disposing );
 }
        }

 Windows Form Designer generated code
        Windows Form Designer generated code

 /// <summary>
        /// <summary>
 /// The main entry point for the application.
        /// The main entry point for the application.
 /// </summary>
        /// </summary>
 [STAThread]
        [STAThread]
 static void Main()
        static void Main() 
 {
        {
 Application.Run(new Form1());
            Application.Run(new Form1());
 }
        }

 private void pic_Paint(object sender, PaintEventArgs e) {
        private void pic_Paint(object sender, PaintEventArgs e) {
 Graphics g =  e.Graphics;
            Graphics g =  e.Graphics;
 rect.draw (g);
            rect.draw (g);
 sq.draw (g);
            sq.draw (g);
 }
        }
 }
    }
 }
}
 (1)构造函数的继承: 使用的类:
(1)构造函数的继承: 使用的类:
Square:
 using System;
using System;
 using CsharpPats;
using CsharpPats;

 namespace RectDraw
namespace RectDraw
 {
{
 /// <summary>
    /// <summary>
 /// Draws a square using the parent Rectangl class
    /// Draws a square using the parent Rectangl class
 /// </summary>
    /// </summary>
 public class Square : Rectangle
    public class Square : Rectangle
 {
    {

 public Square(int x, int y, int w):    base(x, y, w, w) {
        public Square(int x, int y, int w):    base(x, y, w, w) {
 int i_x = x;
            int i_x = x;
 int i_y = y;
            int i_y = y;
 int i_w = w;
            int i_w = w;

 }
        }
 }
    }
 }
}
 Rectangle:
Rectangle:
 using System;
using System;
 using System.Drawing ;
using System.Drawing ;
 namespace CsharpPats
namespace CsharpPats
 {
{
 /// <summary>
    /// <summary>
 /// Draws a rectangle using a supplied Graphics object
    /// Draws a rectangle using a supplied Graphics object
 /// </summary>
    /// </summary>
 public class Rectangle    {
    public class Rectangle    {
 private int x, y, w, h;
        private int x, y, w, h;
 protected Pen rpen;
        protected Pen rpen;
 public Rectangle(int x_, int y_, int w_, int h_)         {
        public Rectangle(int x_, int y_, int w_, int h_)         {
 x = x_;
            x = x_;
 y = y_;
            y = y_;
 w = w_;
            w = w_;
 h = h_;
            h = h_;
 rpen = new Pen(Color.Black);
            rpen = new Pen(Color.Black);
 }
        }
 //-----------------
        //-----------------
 public void draw(Graphics g) {
        public void draw(Graphics g) {
 g.DrawRectangle (rpen, x, y, w, h);
          g.DrawRectangle (rpen, x, y, w, h);
 }
        }
 }
    }
 }
}
 Square在构造函数的时候先执行的是他的基类Square的构造函数,然后才执行自己的构造函数
Square在构造函数的时候先执行的是他的基类Square的构造函数,然后才执行自己的构造函数
(2) 关键字override和virtual,New
DoubleRect:
 using System;
using System;
 using System.Drawing ;
using System.Drawing ;

 namespace RectDraw
namespace RectDraw
 {
{
 /// <summary>
    /// <summary>
 /// Summary description for DoubleRect.
    /// Summary description for DoubleRect.
 /// </summary>
    /// </summary>
 public class DoubleRect:Rectangle
    public class DoubleRect:Rectangle
 {
    {
 private Pen rdPen;
        private Pen rdPen;
 public DoubleRect(int x, int y, int w, int h):base(x,y,w,h)
        public DoubleRect(int x, int y, int w, int h):base(x,y,w,h)
 {
        {
 rdPen  = new Pen (Color.Red, 2);
            rdPen  = new Pen (Color.Red, 2);
 }
        }
 //-----------------
        //-----------------
 public override void draw(Graphics g) {
        public override void draw(Graphics g) {
 base.draw (g); //g.DrawRectangle (rpen, x, y, w, h);
            base.draw (g); //g.DrawRectangle (rpen, x, y, w, h);
 g.DrawRectangle (rdPen, x +5, y+5, w, h);
            g.DrawRectangle (rdPen, x +5, y+5, w, h);
 }
        }
 }
    }
 }
}
 Rectangle:
Rectangle:
 using System;
using System;
 using System.Drawing ;
using System.Drawing ;
 namespace RectDraw
namespace RectDraw
 {
{
 /// <summary>
    /// <summary>
 /// Draws a rectangle using a supplied Graphics object
    /// Draws a rectangle using a supplied Graphics object
 /// </summary>
    /// </summary>
 public class Rectangle    {
    public class Rectangle    {
 protected int x, y, w, h;
        protected int x, y, w, h;
 protected Pen rpen;
        protected Pen rpen;
 public Rectangle(int x_, int y_, int w_, int h_)         {
        public Rectangle(int x_, int y_, int w_, int h_)         {
 x = x_;
            x = x_;
 y = y_;
            y = y_;
 w = w_;
            w = w_;
 h = h_;
            h = h_;
 rpen = new Pen(Color.Black);
            rpen = new Pen(Color.Black);
 }
        }
 //-----------------
        //-----------------
 public virtual void draw(Graphics g) {
        public virtual void draw(Graphics g) {
 g.DrawRectangle (rpen, x, y, w, h);
          g.DrawRectangle (rpen, x, y, w, h);
 }
        }
 }
    }
 }
}
 如果基类里面有一个方法,而想在派生类里面覆盖他,应该把基础类中的方法声明为Virtual,他的含义是,让派生类里具有相同名字和参数标识的方法被调用。然后,在派生类中必须用声明Override关键字声明该方法
如果基类里面有一个方法,而想在派生类里面覆盖他,应该把基础类中的方法声明为Virtual,他的含义是,让派生类里具有相同名字和参数标识的方法被调用。然后,在派生类中必须用声明Override关键字声明该方法
使用New 替换方法:
 public new void draw(Graphics g) {
    public new void draw(Graphics g) {
 g.DrawRectangle(rpen,x,y,w,h);
                  g.DrawRectangle(rpen,x,y,w,h);
 g.DrawRectangle(rpen,x+5,y+5,w,h);
          g.DrawRectangle(rpen,x+5,y+5,w,h);
 }
        }
 
(3)抽象类
Shape:
 using System;
using System;
 using System.Drawing ;
using System.Drawing ;
 namespace CsharpPats
namespace CsharpPats
 {
{
 /// <summary>
    /// <summary>
 /// Summary description for Shape.
    /// Summary description for Shape.
 /// </summary>
    /// </summary>
 public abstract class Shape     {
    public abstract class Shape     {
 protected int height, width;
        protected int height, width;
 protected int xpos, ypos;
        protected int xpos, ypos;
 protected Pen bPen;
        protected Pen bPen;
 //-----
        //-----
 public Shape(int x, int y, int h, int w)    {
        public Shape(int x, int y, int h, int w)    {
 width = w;
            width = w;
 height = h;
            height = h;
 xpos = x;
            xpos = x;
 ypos = y;
            ypos = y;
 bPen = new Pen(Color.Black );
            bPen = new Pen(Color.Black );
 }
        }
 //-----
        //-----
 public abstract void draw(Graphics g);
        public abstract void draw(Graphics g);
 //-----
        //-----
 public virtual float getArea() {
        public virtual float getArea() {
 return height * width;
            return height * width;
 }
        }
 }
    }
 }
}
 
其中 public abstract void draw(Graphics g);是抽象类,不提供实现
如果把一个方法声明为抽象类,也要把类声明为抽象的;
Rectangle :
 using System;
using System;
 using System.Drawing ;
using System.Drawing ;
 namespace CsharpPats
namespace CsharpPats
 {
{
 /// <summary>
    /// <summary>
 /// Summary description for Rectangle.
    /// Summary description for Rectangle.
 /// </summary>
    /// </summary>
 public class Rectangle:Shape     {
    public class Rectangle:Shape     {
 public Rectangle(int x, int y,int h, int w):base(x,y,h,w) {}
        public Rectangle(int x, int y,int h, int w):base(x,y,h,w) {}
 //-----
        //-----
 public override void draw(Graphics g) {
        public override void draw(Graphics g) {
 g.DrawRectangle (bPen, xpos, ypos, width, height);
            g.DrawRectangle (bPen, xpos, ypos, width, height);
 }
        }
 }
    }
 }
}
 public override void draw(Graphics g)是派生出来的抽象类
public override void draw(Graphics g)是派生出来的抽象类
(4) 接口Interface
参考:http://www.cnblogs.com/jhtchina/articles/388696.html
http://www.cnblogs.com/jhtchina/articles/245512.html
学习设计模式以前必须了解继承的基本概念
主程序代码:
 using System;
using System; using System.Drawing;
using System.Drawing; using System.Collections;
using System.Collections; using System.ComponentModel;
using System.ComponentModel; using System.Windows.Forms;
using System.Windows.Forms; using System.Data;
using System.Data;
 namespace RectDraw
namespace RectDraw {
{ /// <summary>
    /// <summary> /// Draws a rectangle and a square
    /// Draws a rectangle and a square /// </summary>
    /// </summary> public class Form1 : System.Windows.Forms.Form
    public class Form1 : System.Windows.Forms.Form {
    { /// <summary>
        /// <summary> /// Required designer variable.
        /// Required designer variable. /// </summary>
        /// </summary> private System.ComponentModel.Container components = null;
        private System.ComponentModel.Container components = null; private CsharpPats.Rectangle rect;
        private CsharpPats.Rectangle rect; private System.Windows.Forms.PictureBox pic;
        private System.Windows.Forms.PictureBox pic; private Square sq;
        private Square sq; private void init() {
        private void init() { rect = new CsharpPats.Rectangle (10, 20, 70, 100);
            rect = new CsharpPats.Rectangle (10, 20, 70, 100); sq = new Square (150,100,70);
            sq = new Square (150,100,70); }
        } public Form1()
        public Form1() {
        { InitializeComponent();
        InitializeComponent(); init();
        init(); }
        } protected override void OnPaint(PaintEventArgs pa) {
        protected override void OnPaint(PaintEventArgs pa) { }
        } /// <summary>
        /// <summary> /// Clean up any resources being used.
        /// Clean up any resources being used. /// </summary>
        /// </summary> protected override void Dispose( bool disposing )
        protected override void Dispose( bool disposing ) {
        { if( disposing )
            if( disposing ) {
            { if (components != null)
                if (components != null)  {
                { components.Dispose();
                    components.Dispose(); }
                } }
            } base.Dispose( disposing );
            base.Dispose( disposing ); }
        }
 Windows Form Designer generated code
        Windows Form Designer generated code
 /// <summary>
        /// <summary> /// The main entry point for the application.
        /// The main entry point for the application. /// </summary>
        /// </summary> [STAThread]
        [STAThread] static void Main()
        static void Main()  {
        { Application.Run(new Form1());
            Application.Run(new Form1()); }
        }
 private void pic_Paint(object sender, PaintEventArgs e) {
        private void pic_Paint(object sender, PaintEventArgs e) { Graphics g =  e.Graphics;
            Graphics g =  e.Graphics; rect.draw (g);
            rect.draw (g); sq.draw (g);
            sq.draw (g); }
        } }
    } }
}
Square:
 using System;
using System; using CsharpPats;
using CsharpPats;
 namespace RectDraw
namespace RectDraw {
{ /// <summary>
    /// <summary> /// Draws a square using the parent Rectangl class
    /// Draws a square using the parent Rectangl class /// </summary>
    /// </summary> public class Square : Rectangle
    public class Square : Rectangle {
    {
 public Square(int x, int y, int w):    base(x, y, w, w) {
        public Square(int x, int y, int w):    base(x, y, w, w) { int i_x = x;
            int i_x = x; int i_y = y;
            int i_y = y; int i_w = w;
            int i_w = w;
 }
        } }
    } }
}
 using System;
using System; using System.Drawing ;
using System.Drawing ; namespace CsharpPats
namespace CsharpPats {
{ /// <summary>
    /// <summary> /// Draws a rectangle using a supplied Graphics object
    /// Draws a rectangle using a supplied Graphics object /// </summary>
    /// </summary> public class Rectangle    {
    public class Rectangle    { private int x, y, w, h;
        private int x, y, w, h; protected Pen rpen;
        protected Pen rpen; public Rectangle(int x_, int y_, int w_, int h_)         {
        public Rectangle(int x_, int y_, int w_, int h_)         { x = x_;
            x = x_; y = y_;
            y = y_; w = w_;
            w = w_; h = h_;
            h = h_; rpen = new Pen(Color.Black);
            rpen = new Pen(Color.Black); }
        } //-----------------
        //----------------- public void draw(Graphics g) {
        public void draw(Graphics g) { g.DrawRectangle (rpen, x, y, w, h);
          g.DrawRectangle (rpen, x, y, w, h); }
        } }
    } }
}
(2) 关键字override和virtual,New
DoubleRect:
 using System;
using System; using System.Drawing ;
using System.Drawing ;
 namespace RectDraw
namespace RectDraw {
{ /// <summary>
    /// <summary> /// Summary description for DoubleRect.
    /// Summary description for DoubleRect. /// </summary>
    /// </summary> public class DoubleRect:Rectangle
    public class DoubleRect:Rectangle {
    { private Pen rdPen;
        private Pen rdPen; public DoubleRect(int x, int y, int w, int h):base(x,y,w,h)
        public DoubleRect(int x, int y, int w, int h):base(x,y,w,h) {
        { rdPen  = new Pen (Color.Red, 2);
            rdPen  = new Pen (Color.Red, 2); }
        } //-----------------
        //----------------- public override void draw(Graphics g) {
        public override void draw(Graphics g) { base.draw (g); //g.DrawRectangle (rpen, x, y, w, h);
            base.draw (g); //g.DrawRectangle (rpen, x, y, w, h); g.DrawRectangle (rdPen, x +5, y+5, w, h);
            g.DrawRectangle (rdPen, x +5, y+5, w, h); }
        } }
    } }
}
 using System;
using System; using System.Drawing ;
using System.Drawing ; namespace RectDraw
namespace RectDraw {
{ /// <summary>
    /// <summary> /// Draws a rectangle using a supplied Graphics object
    /// Draws a rectangle using a supplied Graphics object /// </summary>
    /// </summary> public class Rectangle    {
    public class Rectangle    { protected int x, y, w, h;
        protected int x, y, w, h; protected Pen rpen;
        protected Pen rpen; public Rectangle(int x_, int y_, int w_, int h_)         {
        public Rectangle(int x_, int y_, int w_, int h_)         { x = x_;
            x = x_; y = y_;
            y = y_; w = w_;
            w = w_; h = h_;
            h = h_; rpen = new Pen(Color.Black);
            rpen = new Pen(Color.Black); }
        } //-----------------
        //----------------- public virtual void draw(Graphics g) {
        public virtual void draw(Graphics g) { g.DrawRectangle (rpen, x, y, w, h);
          g.DrawRectangle (rpen, x, y, w, h); }
        } }
    } }
}
使用New 替换方法:
 public new void draw(Graphics g) {
    public new void draw(Graphics g) { g.DrawRectangle(rpen,x,y,w,h);
                  g.DrawRectangle(rpen,x,y,w,h); g.DrawRectangle(rpen,x+5,y+5,w,h);
          g.DrawRectangle(rpen,x+5,y+5,w,h); }
        }
(3)抽象类
Shape:
 using System;
using System; using System.Drawing ;
using System.Drawing ; namespace CsharpPats
namespace CsharpPats {
{ /// <summary>
    /// <summary> /// Summary description for Shape.
    /// Summary description for Shape. /// </summary>
    /// </summary> public abstract class Shape     {
    public abstract class Shape     { protected int height, width;
        protected int height, width; protected int xpos, ypos;
        protected int xpos, ypos; protected Pen bPen;
        protected Pen bPen; //-----
        //----- public Shape(int x, int y, int h, int w)    {
        public Shape(int x, int y, int h, int w)    { width = w;
            width = w; height = h;
            height = h; xpos = x;
            xpos = x; ypos = y;
            ypos = y; bPen = new Pen(Color.Black );
            bPen = new Pen(Color.Black ); }
        } //-----
        //----- public abstract void draw(Graphics g);
        public abstract void draw(Graphics g); //-----
        //----- public virtual float getArea() {
        public virtual float getArea() { return height * width;
            return height * width; }
        } }
    } }
}
其中 public abstract void draw(Graphics g);是抽象类,不提供实现
如果把一个方法声明为抽象类,也要把类声明为抽象的;
Rectangle :
 using System;
using System; using System.Drawing ;
using System.Drawing ; namespace CsharpPats
namespace CsharpPats {
{ /// <summary>
    /// <summary> /// Summary description for Rectangle.
    /// Summary description for Rectangle. /// </summary>
    /// </summary> public class Rectangle:Shape     {
    public class Rectangle:Shape     { public Rectangle(int x, int y,int h, int w):base(x,y,h,w) {}
        public Rectangle(int x, int y,int h, int w):base(x,y,h,w) {} //-----
        //----- public override void draw(Graphics g) {
        public override void draw(Graphics g) { g.DrawRectangle (bPen, xpos, ypos, width, height);
            g.DrawRectangle (bPen, xpos, ypos, width, height); }
        } }
    } }
}
(4) 接口Interface
参考:http://www.cnblogs.com/jhtchina/articles/388696.html
http://www.cnblogs.com/jhtchina/articles/245512.html
 
                    
                

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号