Three Different Styles of Class Implement

1. Cpp

Cpp was famous as "C with class" before, which is not so precise today but still a good slogan.

Cpp uses the most easy understanding way to implement a class like we are not knowing class at all. 

If we need any containers, we can define them directly on beginning of a class. All functions below are shadowed by the variables so they can be used directly.

Initialization will be implemented by a function with the same name as our class itself. This function has no return or type so it can easy to be noticed. 

Cpp treats class more like "nothing". Methods inside the class communicates via class-wide-global variables. Which is also the general rule about functions without class at all.

Class communicate with outside world via initialization inputs and values pass into methods.

class ParkingSystem {
public:
    int count[3] = {0};
    
    ParkingSystem(int big, int medium, int small) {
        count[0] = big;
        count[1] = medium;
        count[2] = small;
    }
    
    bool addCar(int carType) {
        if(count[carType-1]>0) {
            count[carType-1]--;
            return true;
        }
        return false;
    }
};

/**
 * Your ParkingSystem object will be instantiated and called as such:
 * ParkingSystem* obj = new ParkingSystem(big, medium, small);
 * bool param_1 = obj->addCar(carType);
 */

 

2. Python

Python is completely outside C family so it has special idea about class.

Python has a "wired" function __init__()  and "wired" variable called self everywhere. 

Double underscores means this function shall not be used directly, and actually it will run automatically when the class is initialized. Name of the class is more important than Cpp or Java becuase it will be used at initialization.

If we need any containers, they can be defined and binded onto self variable. Though self, they are going to every methods inside the class.

Python treats class more like a big "function". Methods belongs to that class are sub-functions inside it. All those sub-functions and the big funcion(class) communicate by variable "self".

Class communicate with outside world via initialization inputs and values pass into methods.

class ParkingSystem:

    def __init__(self, big: int, medium: int, small: int):
        self.count = [big, medium, small]

    def addCar(self, carType: int) -> bool:
        if self.count[carType-1]>0:
            self.count[carType-1] -= 1
            return True
        return False

# Your ParkingSystem object will be instantiated and called as such:
# obj = ParkingSystem(big, medium, small)
# param_1 = obj.addCar(carType)

 

3. Go

As relatively a "new" language than two examples before, Go also has some special idea of class implement.

Go has no class, but it can use method and attribute as if it actually has a class. This idea may norrow the gap between object-oriented and function-oriented idea.

If we need any containers, we first define our struct with type name. In this struct we will decide how our containers are implemented. 

After that, we will write a constructor function requres result type is the type we defined. This is how we initialize the class in Go.

Class normally do two things, first contain some data, second prepare some method waitting to be called.

Go actually disassemble these two things back to two part. The data part are achieved by type-struct. The method part are just let function as it already is. 

To distinguwish with normal function, methods will have brackets and type name after keyword func. The start hints that pointer is used to communicate between methods.

type ParkingSystem struct {
    count [3]int
}


func Constructor(big int, medium int, small int) ParkingSystem {
    var obj ParkingSystem
    obj.count[0] = big
    obj.count[1] = medium
    obj.count[2] = small
    return obj
}


func (this *ParkingSystem) AddCar(carType int) bool {
    if this.count[carType-1]>0 {
        this.count[carType-1]--
        return true
    }
    return false
}


/**
 * Your ParkingSystem object will be instantiated and called as such:
 * obj := Constructor(big, medium, small);
 * param_1 := obj.AddCar(carType);
 */

  

  

  

posted @ 2020-10-07 17:03  DrVonGoosewing  阅读(154)  评论(0)    收藏  举报