更有效地利用OOP

时间:2015-02-22 14:04:19

标签: c# oop

我正在寻找以更好的方式塑造我的程序的可能性,并希望更有效地利用面向对象的方法。

public ScrollingBackground(int width, int height, int speed, string title, Bitmap path) 
    {

        intBreite = width;
        intHoehe = height;
        intFeinheitDerBewegungen = speed;
        stringTitel = title;
        bitmapBildpfad = path;


        this.Text = title;
        this.Size = new Size(this.intBreite, this.intHoehe);
        this.StartPosition = FormStartPosition.CenterScreen;
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;


        timerBewegungImage = new Timer();
        timerBewegungImage.Tick += new EventHandler(bewegungImage_XRichtung_Tick);
        timerBewegungImage.Interval = constIntInterval;
        timerBewegungImage.Start();

        ////
        picBoxImage = new PictureBox();
        picBoxImage.Image = global::Flugzeugspiel.Properties.Resources.MyFlugzeug;
        picBoxImage.BackColor = Color.Transparent;
        picBoxImage.SetBounds(100, 100, 125, 50);
        this.Controls.Add(picBoxImage);
        ////

        listPicBoxAufeinanderfolgendeImages = new PictureBox[2];

        imageInWinFormLadenBeginn();

        this.ShowDialog();

    }

正如你可以看到上面的构造函数,斜杠包含的代码位于构造函数ScrollingBackground中。

对我来说,这个构造函数应该只包含与滚动背景相关的代码而不是图像MyFlugzeug.png。 这个图像(当然是斜线包含的整个代码)应该换成我的主要类Flugzeug。

using System;
using System.Drawing;
using System.Windows.Forms;


    public abstract class Flugzeug : Form
    {

        private PictureBox picBoxImage;     
        protected int lebensanzeige = 10; 




        public virtual void nachObenUntenFliegen(string pathOfMovingObject, int xPositionObject, int yPositionObject, int widthOfObject, int heightOfObject)
        {

                 // The code in the upper constructor enclosed by the slashes should stand here. Here I'd like to ensure the control of the airplane which has to be still programmed. 

        }


        public int getLebensanzeige()
        {

            return lebensanzeige;

        }


        public int getTreffer(int schussstaerke)
        {

            return lebensanzeige = lebensanzeige - schussstaerke;

        }

    }

如何将代码从构造函数传递到Flugzeug类?

我尝试了一些东西,但它没有用。

我试过了......像这样:

    public class MyFlugzeugspiel
        {


            public static void Main()
            {

                MyFlugzeug myPlane = new MyFlugzeug(10);


                ScrollingBackground hintergrund = new ScrollingBackground(1000, 650, 5, "THE FLIGHTER", global::Flugzeugspiel.Properties.Resources.Himmel, myPlane);

...

    public abstract class Flugzeug : Form
        {

            private PictureBox picBoxImage;     
            protected int lebensanzeige = 10; 



            public virtual void nachObenUntenFliegen(string pathOfMovingObject, int xPositionObject, int yPositionObject, int widthOfObject, int heightOfObject)
            {

                picBoxImage = new PictureBox();
                picBoxImage.Image = global::Flugzeugspiel.Properties.Resources.MyFlugzeug;
                picBoxImage.BackColor = Color.Transparent;
                picBoxImage.SetBounds(100, 100, 125, 50);
                this.Controls.Add(picBoxImage);


            }

...




 public ScrollingBackground(int width, int height, int speed, string title, Bitmap path, Flugzeug plane) 
    {


        intBreite = width;
        intHoehe = height;
        intFeinheitDerBewegungen = speed;
        stringTitel = title;
        bitmapBildpfad = path;
        this.plane = plane;
        plane.nachObenUntenFliegen("Path", 0, 0, 100, 100);



        this.Text = title;
        this.Size = new Size(this.intBreite, this.intHoehe);
        this.StartPosition = FormStartPosition.CenterScreen;
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;

...

但这不对,是吗?什么都没发生。飞机不会出现在滚动背景上。

谁能帮我解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以从Control课程中制作FormScrollingBackground,并以Flugzeug形式从中派生(或将其用作对照)。

如果您认为派生是最佳选择,您的班级签名应如下所示:

public class ScrollingBackground : Form { }

public class Flugzeug : ScrollingBackground { }

如果您更喜欢控件,则应该从ScrollingBackground课程中公开所需的属性,以便在Flugzeug课程中访问它们。