java get set
class Employ
{
public String name; //name of the employ
public String getName() //Get the name
{
return name;
}
public String setName(String newName) //Set the name
{
this.name = newName;
}
}
java get set
class Employ
{
public String name; //name of the employ
public String getName() //Get the name
{
return name;
}
public String setName(String newName) //Set the name
{
this.name = newName;
}
}
getters and setters javascript
let obj = {
log: ['a', 'b', 'c'],
get latest() {
if (this.log.length === 0) {
return undefined;
}
return this.log[this.log.length - 1];
}
};
obj.log.push('d');
console.log(obj.latest); //output: 'd'
setter&getter java
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
v1.setColor("Red");
System.out.println(v1.getColor());
}
// Outputs "Red"
js why to use getters and setters
1) Syntax reasons. It’s easier and faster to read code
created with accessor functions
2) Encapsulation. I can create safer code with accessor functions.
use of getter setter in java
package gettersetterProgram;
public class Mensuration
{
public double x, y, r;
// Create getter method for each variable.
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getR() {
return r;
}
// Create setter method for each variable in the class.
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setR(double r) {
this.r = r;
}
// Now write the logic of Rectangle and Circle.
public double areaRec() {
double area = x*y;
return area;
}
public double perRec() {
double per = 2*(x+y);
return per;
}
public double areaCircle() {
double area = 3.14*r*r;
return area;
}
public double circumCircle() {
double circumference = 2*3.14*r;
return circumference;
}
}
public class MyTest {
public static void main(String[] args)
{
Mensuration mens; // Creating reference.
men's = new Mensuration(); // Creating object.
// Set the values of the variables.
mens.setX(20.5);
mens.setY(30.5);
mens.setR(12.5);
double areaRec = mens.areaRec();
double perRec = mens.perRec();
double areaCircle = mens.areaCircle();
double circumCircle = mens.circumCircle();
System.out.println("Area of rectangle: " +areaRec);
System.out.println("Perimeter of rectangle: " +perRec);
System.out.println("Area of circle: " +areaCircle);
System.out.println("Circumference of circle: " +circumCircle);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us