开发者

Object Oriented question

I 开发者_高级运维recently gave in a piece of coursework and my teacher said that my approach is wrong and I got marks taken off. I would appreciate it if someone could tell me what I did wrong and how I can fix this.. (ignore the syntax but the idea is there, this was in objective c but I guess the same principle would apply)

main class()
{

int variable1; 
int variable2; 
int variable3; 

CalculationClass object1; 
variable3 = object1.calculate(variable1,variable2); 

}

CalculationClass()
{
           int calculate(int a, int b)
           {
              int c;
              c = a+b;
              return c;
           }
}

In the feedback the teacher said that my approach to object orientated is wrong. Because I don't have any variables in the calculation class, I could of just put everything inside the main class and call it from there.

Is this true? How can this be fixed? (do I just put variables inside the calculation class..? - I found it easier just to pass variables though the parameters).

thanks for any help


The problem is that the assignment that was given to you is of a functional nature.

Naturally, your solution steered towards a functional solution and away from artificial object-orientation that was required on top.

You could put the solution in a "true" object-oriented garment like your teacher requires (putting variables into the class) but is not natural in my opinion.


your teacher might not be totally right,

in OOP we have something called 'static class' which doesn't contain instance level properties (dynamic properties), the same way as you implement the code above,

this static class contains shared methods which are used by other classes

for example in pseudo code:


//class for translating phrases
class translator {

  static string translate(string phrase,string destination_language) {
    //return the 'phrase' in 'destination_language'
  }
}


I agree with Peter G. this is a functional nature problem, I believe. Nevertheless, another solution would be to create a class Number, an int wrapper with methods for calculations and other conversions, functionalities you might need.

Example:

Number num1 = Number(variable1);
Number num2 = Number(variable2);
Number num3;

num3 = num1.plus(num2); //Personally I believe this looks more OOP ;)

Which would be implemented somehow like this:

class Number(){
    int var;

    Number(int v){var = v;}//constructor

    Number plus(Number otherNum){//calculation
        return Number(this.var + otherNum.var);
    }
}


This is probably more like what your teacher could have wanted (in pseudo-OOP language):

class Computation
{
    private int a, b;

    public Computation(int a_, int b_)
    {
        a = a_; b = b_;
    }

    public int compute()
    {
        return a + b;
    }
};

int main()
{
    int v1, v2;
    ...
    Computation c(v1, v2);
    v3 = c.compute();
}

The point here is that the class Computation could be constructed from various different ways, but called always the same way (and eg. be replaced at run time by another object which computes a different way). This is the "functor" idiom.


Your instructor would probably like you to be able to write

object3.calculate(object1, object2);

not just put a function inside a class and call that object oriented.

class Calculations
{
     int calculate(int a, int b);
}

isn't any more "object oriented" than

namespace Calculations
{
    int calculate(int a, int b);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜