如何实现多态 [英] How to implement Polymorphism

查看:74
本文介绍了如何实现多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些图形类,它们的公共成员在运行时由用户操纵.

我想通过多态性访问这些类.类的公共参数将有所不同(即,某些属性匹配,例如width,height等,而其他属性则不匹配)

以下是我到目前为止的代码.请彻底帮助我.

I have some Graphical Classes whose public member are manipulated by user at runtime.

I want to access these class through polymorphism. The classes public parameters will differ (i.e; some properties match such as width, height , etc and others donot match)

The following is my code so far. Please help me throughly.

public interface GraphicsObject
    {
        int Width { get; set; }
        int Height { get; set; }

        int Repetation { get; set; }
        int RepetationMin { get; set; }
        int RepetationMax { get; set; }
        bool IsRepetationRandom { get; set; }

        bool IsTop { get; set; }
        bool IsBtm { get; set; }
        bool IsOffset { get; set; }

        LineType LineType { get; set; }
        bool IsLineTypeRandom { get; set; }

        Shapes Shapes { get; set; }
        bool IsShapesRandom { get; set; }

        GraphicsPath[] Draw();
    }



我的图形班:



My Graphical Classes:

public class FallingLines : GraphicsObject
    {
        #region Properties
        public int Width { get; set; }
        public int Height { get; set; }

        public int Repetation { get; set; }
        public int RepetationMax { get; set; }
        public int RepetationMin { get; set; }
        public bool IsRepetationRandom { get; set; }

        public bool IsTop { get; set; }
        public bool IsBtm { get; set; }
        public bool IsOffset { get; set; }

        public LineType LineType { get; set; }
        public bool IsLineTypeRandom { get; set; }
        #endregion

        #region Constructor
        public FallingLines(int width, int height)
        {
            this.Width = width;
            this.Height = height;

            this.Repetation = 10;
            this.RepetationMin = 8;
            this.RepetationMax = 10;
            this.IsRepetationRandom = true;

            this.IsTop = true;
            this.IsBtm = false;
            this.IsOffset = true;

            this.LineType = GraphicalDesigns.LineType.Irregular;
            this.IsLineTypeRandom = false;
        }
        #endregion

        #region Function
        public GraphicsPath[] Draw()
        {
            // does some work
        }

        private Rectangle[] GetRectangle(int randomOffset)
        {
            // does some work
        }

        private GraphicsPath GetPath(int x, int y, int width, int height)
        {
            // does some work
        }

        private Point[] GetPoints(int count)
        {
            // does some work
        }

        private Point[] DrawWave(Point top, Point btm, int cntWave)
        {
            // does some work
        }

        private GraphicsPath DrawIrregularity(Point[] points, int width)
        {
            // does some work
        }

        private Point[] GetRandomPointInLine(Point[] points, int curveCnt)
        {
            // does some work
        }

        private Point[] DrawIrregularWave(Point top, Point btm, int width)
        {
            // does some work
		}
    }

    public enum LineType
    {
        Straight,
        SineCurve,
        Irregular
    }





public class GeometricalShapes : GraphicsObject
    {
        #region Properties
        public int Width { get; set; }
        public int Height { get; set; }

        public int Repetation { get; set; }
        public int RepetationMax { get; set; }
        public int RepetationMin { get; set; }
        public bool IsRepetationRandom { get; set; }

        public bool IsOffset { get; set; }
        
        public Shapes Shapes { get; set; }
        public bool IsShapesRandom { get; set; }
        #endregion

        public GeometricalShapes(int width, int height) 
        {
            this.Width = width;
            this.Height = height;

            this.Repetation = 6;
            this.RepetationMax = 10;
            this.RepetationMin = 4;
            this.IsRepetationRandom = true;

            this.IsOffset = true;

            this.Shapes = GraphicalDesigns.Shapes.Circle;
            this.IsShapesRandom = true;
        }

        public GraphicsPath[] Draw()
        {
            // some work is done
        }
        
        private int[] GetIncBoxSize(int repetation)
        {
            // some work is done
        }

        private GraphicsPath GetPath(int x, int y, int wt, int ht)
        {
            // some work is done
        }
    }

    public enum Shapes
    {
        Triangle,
        Square,
        Rectangle,
        Circle,
        Oval,
        Pentagon,
        Hexagon,
        Octagon
    }



用于通过主窗体进行控制:



For Controlling through the main Form:

private GraphicsPath[] gPath = null;
        public GraphicsPath[] GPath 
        {
            get { return this.gPath; }
        }

        private PointF[] points = null;
        public PointF[] Points 
        {
            get { return points; }
        }

        public GraphicsMode mode; 

        public int Width { get; set; }
        public int Height { get; set; }

        public int Repetation { get; set; }
        public int RepetationMin { get; set; }
        public int RepetationMax { get; set; }
        public bool IsRepetationRandom { get; set; }

        public bool IsTop { get; set; }
        public bool IsBtm { get; set; }
        public bool IsOffset { get; set; }

        public LineType LineType { get; set; }
        public bool IsLineTypeRandom { get; set; }

        public Shapes Shapes { get; set; }
        public bool IsShapesRandom { get; set; }

        public GraphicsObject gObject;

        public ModelGraphics(int width, int height) 
        {
            this.Width = width;
            this.Height = height;
        }

        public void SetGraphicalObject(GraphicsMode mode)
        {
            this.mode = mode;

            switch (mode)
            {
                case GraphicsMode.GeometricalShape:
                    gObject = new GeometricalShapes(this.Width,this.Height);
                    this.Repetation = gObject.Repetation;
                    this.RepetationMax = gObject.RepetationMax;
                    this.RepetationMin = gObject.RepetationMin;
                    this.IsRepetationRandom = gObject.IsRepetationRandom;
                    this.IsOffset = gObject.IsOffset;
                    this.Shapes = gObject.Shapes;
                    this.IsShapesRandom = gObject.IsShapesRandom;
                    break;

                case GraphicsMode.FallingLine:
                    gObject = new FallingLines(this.Width,this.Height);
                    this.Repetation = gObject.Repetation;
                    this.RepetationMax = gObject.RepetationMax;
                    this.RepetationMin = gObject.RepetationMin;
                    this.IsRepetationRandom = gObject.IsRepetationRandom;
                    this.IsTop = gObject.IsTop;
                    this.IsBtm = gObject.IsBtm;
                    this.IsOffset = gObject.IsOffset;
                    this.LineType = gObject.LineType;
                    this.IsLineTypeRandom = gObject.IsLineTypeRandom;
                    break;
            }
        }

        public void GetGraphicalObject()
        {
            gPath = (GraphicsPath[])gObject.Draw().Clone();
        }



如何将mainForm中的值设置为图形类,反之亦然.

我知道OOP的概念不匹配.当我有更多的图形类(例如FallingLines和GeometricalShapes)时,请帮助我理解并帮助我轻松弄清楚我的结构
Thankyou



How do i set the values from mainForm to graphical classes and vice-versa.

I know the concept of OOP doesnot match. Please help me understand and help me figure out my structure for ease when i have more graphical classes such as FallingLines and GeometricalShapes
Thankyou

推荐答案

您不应该问如何实现多态.目前,这是一个完全错误的问题.此代码表明,您首先需要回答完全不同的问题:什么是多态性"?因为现在您甚至没有得到任何多态设计的要求.

我可以告诉你为什么.这是声明enum Shapes.基本上,不仅是多态性,而且整个OOP的主要思想是:您永远不需要这样的声明. OOP确实比您可能拥有的功能强大得多.

那么,如何在这里提供帮助?越早忘记所做的事情越好.您需要从头开始,只需要了解OOP的全部含义.以及为什么.

也许,从这里开始:
http://en.wikipedia.org/wiki/面向对象的程序 [ http://en.wikipedia.org/wiki/Virtual_function [ http://en.wikipedia.org/wiki/Abstract_class [ http://en.wikipedia.org/wiki/Interface_%28computing%29#Software_interfaces_in_object_directional_languages [ ^ ],
http://en.wikipedia.org/wiki/Dynamic_dispatch [ http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ].

这里的主要内容是:

You should not ask how to implement polymorphism. At this moment, this is a totally wrong question. This code shows, that you first need to answer totally different question: "what is polymorphism"? Because right now you didn''t even get the requirements for the design of anything polymorphic.

I can tell you why. This is the declaration enum Shapes. Basically, the main idea not only of the polymorphism, but the main idea of whole OOP is this: you never need such declarations. OOP is really much more powerful that you might thing.

So, how to help here? The sooner you forget what you are doing, the better. You need to start from the very beginning and just understand what OOP is all about. And why.

Perhaps, start from here:
http://en.wikipedia.org/wiki/Object-oriented_programming[^],
http://en.wikipedia.org/wiki/Virtual_function[^],
http://en.wikipedia.org/wiki/Abstract_class[^],
http://en.wikipedia.org/wiki/Interface_%28computing%29#Software_interfaces_in_object_oriented_languages[^],
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

Main thing here is this:

Yoda 所写:

取消了解您所学.

—SA


这篇关于如何实现多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆