Set and Get Methods in java?

2019-01-04 23:54发布

How can I use the set and get methods, and why should I use them? Are they really helpful? And also can you give me examples of set and get methods?

15条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-05 00:39

Setters and getters are used to replace directly accessing member variables from external classes. if you use a setter and getter in accessing a property, you can include initialization, error checking, complex transformations, etc. Some examples:

private String x;

public void setX(String newX) {
    if (newX == null) {
        x = "";
    } else {
        x = newX;
    }
}

public String getX() {
    if (x == null) {
        return "";
    } else {
       return x;
    }
}
查看更多
看我几分像从前
3楼-- · 2019-01-05 00:40

It looks like you trying to do something similar to C# if you want setAge create method
setAge(int age){ this.age = age;}

查看更多
欢心
4楼-- · 2019-01-05 00:41

just because the OOP rule: Data Hiding and Encapsulation. It is a very bad practice to declare a object's as public and change it on the fly in most situations. Also there are many other reasons , but the root is Encapsulation in OOP. and "buy a book or go read on Object Oriented Programming ", you will understand everything on this after you read any book on OOP.

查看更多
Fickle 薄情
5楼-- · 2019-01-05 00:42

The above answers summarize the role of getters and setters better than I could, however I did want to add that your code should ideally be structured to reduce the use of pure getters and setters, i.e. those without complex constructions, validation, and so forth, as they break encapsulation. This doesn't mean you can't ever use them (stivlo's answer shows an example of a good use of getters and setters), just try to minimize how often you use them.

The problem is that getters and setters can act as a workaround for direct access of private data. Private data is called private because it's not meant to be shared with other objects; it's meant as a representation of the object's state. Allowing other objects to access an object's private fields defeats the entire purpose of setting it private in the first place. Moreover, you introduce coupling for every getter or setter you write. Consider this, for example:

private String foo;

public void setFoo(String bar) {
    this.foo = bar;
}

What happens if, somewhere down the road, you decide you don't need foo anymore, or you want to make it an integer? Every object that uses the setFoo method now needs to be changed along with foo.

查看更多
淡お忘
6楼-- · 2019-01-05 00:46

This answer is merged from another question.

Your getAge() method is called instance method in Java.

To invoke an instance method, you should have a object of the Class in which this method is defined.

For Example, If this method in a Class called Person, then

  1. Create a Person object using new operator

     Person p = new Person();
    
  2. To get the age of a Person object, use this method

    p.getAge()
    
查看更多
贪生不怕死
7楼-- · 2019-01-05 00:47

this is the code for set method

public void setAge(int age){
  this.age = age;
}
查看更多
登录 后发表回答