对继承的一些理解

From :《设计模式》第5章
学习设计模式以前必须了解继承的基本概念

主程序代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RectDraw
{
    
/// <summary>
    
/// Draws a rectangle and a square
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private CsharpPats.Rectangle rect;
        
private System.Windows.Forms.PictureBox pic;
        
private Square sq;
        
private void init() {
            rect 
= new CsharpPats.Rectangle (102070100);
            sq 
= new Square (150,100,70);
        }

        
public Form1()
        
{
        InitializeComponent();
        init();
        }

        
protected override void OnPaint(PaintEventArgs pa) {
        }

        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void pic_Paint(object sender, PaintEventArgs e) {
            Graphics g 
=  e.Graphics;
            rect.draw (g);
            sq.draw (g);
        }

    }

}

(1)构造函数的继承: 使用的类:
Square:
using System;
using CsharpPats;

namespace RectDraw
{
    
/// <summary>
    
/// Draws a square using the parent Rectangl class
    
/// </summary>

    public class Square : Rectangle
    
{

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

        }

    }

}

Rectangle:
using System;
using System.Drawing ;
namespace CsharpPats
{
    
/// <summary>
    
/// Draws a rectangle using a supplied Graphics object
    
/// </summary>

    public class Rectangle    {
        
private int x, y, w, h;
        
protected Pen rpen;
        
public Rectangle(int x_, int y_, int w_, int h_)         {
            x 
= x_;
            y 
= y_;
            w 
= w_;
            h 
= h_;
            rpen 
= new Pen(Color.Black);
        }

        
//-----------------
        public void draw(Graphics g) {
          g.DrawRectangle (rpen, x, y, w, h);
        }

    }

}

Square在构造函数的时候先执行的是他的基类Square的构造函数,然后才执行自己的构造函数
(2) 关键字override和virtual,New
DoubleRect:
using System;
using System.Drawing ;

namespace RectDraw
{
    
/// <summary>
    
/// Summary description for DoubleRect.
    
/// </summary>

    public class DoubleRect:Rectangle
    
{
        
private Pen rdPen;
        
public DoubleRect(int x, int y, int w, int h):base(x,y,w,h)
        
{
            rdPen  
= new Pen (Color.Red, 2);
        }

        
//-----------------
        public override void draw(Graphics g) {
            
base.draw (g); //g.DrawRectangle (rpen, x, y, w, h);
            g.DrawRectangle (rdPen, x +5, y+5, w, h);
        }

    }

}

Rectangle:
using System;
using System.Drawing ;
namespace RectDraw
{
    
/// <summary>
    
/// Draws a rectangle using a supplied Graphics object
    
/// </summary>

    public class Rectangle    {
        
protected int x, y, w, h;
        
protected Pen rpen;
        
public Rectangle(int x_, int y_, int w_, int h_)         {
            x 
= x_;
            y 
= y_;
            w 
= w_;
            h 
= h_;
            rpen 
= new Pen(Color.Black);
        }

        
//-----------------
        public virtual void draw(Graphics g) {
          g.DrawRectangle (rpen, x, y, w, h);
        }

    }

}

如果基类里面有一个方法,而想在派生类里面覆盖他,应该把基础类中的方法声明为Virtual,他的含义是,让派生类里具有相同名字和参数标识的方法被调用。然后,在派生类中必须用声明Override关键字声明该方法
使用New 替换方法:
    public new void draw(Graphics g) {
                  g.DrawRectangle(rpen,x,y,w,h);
          g.DrawRectangle(rpen,x
+5,y+5,w,h);
        }


(3)抽象类
Shape:
using System;
using System.Drawing ;
namespace CsharpPats
{
    
/// <summary>
    
/// Summary description for Shape.
    
/// </summary>

    public abstract class Shape     {
        
protected int height, width;
        
protected int xpos, ypos;
        
protected Pen bPen;
        
//-----
        public Shape(int x, int y, int h, int w)    {
            width 
= w;
            height 
= h;
            xpos 
= x;
            ypos 
= y;
            bPen 
= new Pen(Color.Black );
        }

        
//-----
        public abstract void draw(Graphics g);
        
//-----
        public virtual float getArea() {
            
return height * width;
        }

    }

}


其中  public abstract void draw(Graphics g);是抽象类,不提供实现
如果把一个方法声明为抽象类,也要把类声明为抽象的;

Rectangle :
using System;
using System.Drawing ;
namespace CsharpPats
{
    
/// <summary>
    
/// Summary description for Rectangle.
    
/// </summary>

    public class Rectangle:Shape     {
        
public Rectangle(int x, int y,int h, int w):base(x,y,h,w) {}
        
//-----
        public override void draw(Graphics g) {
            g.DrawRectangle (bPen, xpos, ypos, width, height);
        }

    }

}

public override void draw(Graphics g)是派生出来的抽象类
(4) 接口Interface
参考:http://www.cnblogs.com/jhtchina/articles/388696.html
http://www.cnblogs.com/jhtchina/articles/245512.html

posted @ 2006-11-28 16:43  jhtchina  阅读(330)  评论(0)    收藏  举报