How do getters and setters work?

I am from the php world. Could you explain what getters and setters are there and can give you some examples?

+80
java setter getter
Jan 10 '10 at 12:20
source share
6 answers

This does not require a textbook. Read on encapsulation

private String myField; //"private" means access to this is restricted public String getMyField() { //include validation, logic, logging or whatever you like here return this.myField; } public void setMyField(String value) { //include more logic this.myField = value; } 
+101
Jan 10
source share

In Java, getters and seters are completely common functions. This is the only thing that makes them getters or setters. The getter for foo is called getFoo, and the setter is called setFoo. In the case of boolean, the getter is called isFoo. They must also have a specific declaration, as shown in this example getter and installer for "name":

 class Dummy { private String name; public Dummy() {} public Dummy(String name) { this.name = name; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } } 

The reason for using getters and setters instead of publishing your members is because it allows you to change the implementation without changing the interface. In addition, many tools and tools that use reflection to study objects accept only objects that have getters and setters. JavaBeans , for example, must have getters and setters, as well as some other requirements.

+28
Jan 10
source share
 class Clock { String time; void setTime (String t) { time = t; } String getTime() { return time; } } class ClockTestDrive { public static void main (String [] args) { Clock c = new Clock; c.setTime("12345") String tod = c.getTime(); System.out.println(time: " + tod); } } 

When you run the program, the program runs on the network,

  • object c is created
  • The setTime() function is called by c
  • the time variable is set to the value passed
  • The getTime() function is called by c
  • time is back
  • Opens tod and tod print
+10
Jan 25 '14 at 4:51
source share

You can also read " Why getter and setter methods are evil :

Although getter / setter methods are common in Java, they are not particularly object oriented (OO). In fact, they can damage your code performance. Moreover, the presence of numerous getter and setter methods is a red flag that the program is not necessarily well designed in terms of OO.

This article explains why you shouldn't use getters and setters (and when you can use them) and offers a design methodology that helps you get out of the getter / setter mentality.

+6
Jan 10
source share

1. The best getters / setters are smart.

Here is a javascript example from mozilla:

 var o = { a:0 } // `o` is now a basic object Object.defineProperty(o, "b", { get: function () { return this.a + 1; } }); console.log(ob) // Runs the getter, which yields a + 1 (which is 1) 

I used these LOTs because they are awesome . I would use it when I liked coding + animation. For example, create a setter that deals with Number , which displays this number on your web page. When the setter is used, it animates the old number with the new number using tweener . If the starting number is 0, and you set it to 10, you will see that the numbers are quickly flipped from 0 to 10, say, in half a second. Users love this stuff, and it's fun to create.

2. Getters / seters in php

Example from sof

 <?php class MyClass { private $firstField; private $secondField; public function __get($property) { if (property_exists($this, $property)) { return $this->$property; } } public function __set($property, $value) { if (property_exists($this, $property)) { $this->$property = $value; } return $this; } } ?> 

citings:

+2
Jan 06 '16 at 16:56
source share

an example is here to learn this simplest way to use getter and setter in "java". it can be done in a more direct way, but the receiver and setter have something special that when using the private member of the parent class in the child class in inheritance. you can do this using getter and setter.

 package stackoverflow; public class StackoverFlow { private int x; public int getX() { return x; } public int setX(int x) { return this.x = x; } public void showX() { System.out.println("value of x "+x); } public static void main(String[] args) { StackoverFlow sto = new StackoverFlow(); sto.setX(10); sto.getX(); sto.showX(); } } 
0
May 18 '17 at 8:22
source share



All Articles