Does having lots of methods on a class increase the overhead of that class's object?

后端 未结 2 459
轻奢々
轻奢々 2021-01-04 03:33

Imagine I am using a class to bring back items from a database, say

class BankRecord {

public int id;
public int balance;

public void GetOverdraft() {
...
         


        
2条回答
  •  情深已故
    2021-01-04 03:55

    The runtime does not duplicate the code when creating an instance of a object. Only the member fields are allocated space in memory when an instance is created. For this reason, you shouldn't be creating "data-only" classes unless you have some other design reason for doing so.

    That being said, there are other best-practices reasons why you might not want a class with a multitude of methods (e.g. the God object anti-pattern).

提交回复
热议问题