开发者

How to Instantiate new structure object?

Is this correct?

//Structure
public struct Shape
{
    public string mShape;
    public int mSide;

    public Shape(/*initial sides parameter*/ int sSide, string sShape)
    {
        mShape = sShape;
        mSide = sSide;
    }

}

//more code here.... but no need to post.
//Instantiate new Shape structure object
myShape = new Shape(/*Sides integer value*/ ,sShape);

//How to Add the additional p开发者_运维问答arameter to construct the object?


Not sure I follow. If your question is, how do you send an integer in the constructor?

Here you go:

myShape = new Shape(4, sShape);

or:

int sides = 4;
myShape = new Shape(sides, sShape);


Agreed with CiC why are the fields public? Very dangerous violation of encapsulation http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 -- also see references on information hiding... Also, Shape is pretty vague, right? A square has one value, a rectangle 2 and a circle a circumference..and a polygon could have many. I thin the subclasses need to implement the sides by extending the superclass. And, in that case, why pass the name of the shape? What is the point of this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜