In C# you can do this
is there a way to do something like this in javaCode:private int iCoins;
public int Coins
{
Get
{
return this.iCoins;
}
Set
{
this.iCoins = value;
}
}
Printable View
In C# you can do this
is there a way to do something like this in javaCode:private int iCoins;
public int Coins
{
Get
{
return this.iCoins;
}
Set
{
this.iCoins = value;
}
}
I dont have a java compiler on the computer i am using, so i dont know if this syntax is allowed. Try this.I dont think you need to use this when returning iCoins.Code:private int iCoins;
public int getCoins(int value)
{
//Set 'iCoins'
this.iCoins = value;
//Return the value of 'iCoins'
return iCoins;
}
In java you have to write the accessors yourself:
SurName would be read-only as you have no set method for it. To get and set the properties:Code:class Person
{
private String firstName;
private String surName;
void setFirstName(String s)
{
firstName = s;
}
String getFirstName()
{
return firstName;
}
String getSurName()
{
return SurName;
}
}
Code:p = new Person();
p.setFirstName('Santa');
damn i was hoping that i would be able to do
or something alone those line.Code:class yyy
{
private String sMyString;
public String MyString
{
get
{
return this.sMyString;
}
set
{
this.sMyString = value;
System.out.writeln("MyString has been changed to: " + value);
}
}
}
class xxx
{
public xxx()
{
yyy y = new yyy();
y.MyString = "Hello World!";
}
}
Nope. Properties don't exist in Java proper. (No loss IMHO.) Bean scripting environments, however, will use the get/set pattern to create properties.