Java class with no constructor?

前端 未结 5 1273
心在旅途
心在旅途 2021-01-27 16:46

I have this class

class Customer{
    int ID;
    Time arriveTime;
    Time serviceTime;
    Time completeTime;
    int transaction;
}

Don\'t I

5条回答
  •  别那么骄傲
    2021-01-27 17:33

    The method that you are suggesting does not do a very good job of abstracting and separating variables. It allows them to be messed with by any code at will.

    Part of Java is OOP, or Object Oriented Programming. If you make certain parts of the class private and separated from the rest of the code, then it will limit access from other code, which helps your class follow the OO principle of encapsulation.

    Instead, I would make a constructor that fills in the fields when appropriate, such as arrive and id. I would make the variables private. Also, I would make id a static member, because it would be incremented for every new customer.

提交回复
热议问题