|
-
Nov 1st, 2005, 11:48 AM
#1
Thread Starter
<?="Moderator"?>
Get {} Set {}?
In C# you can do this
Code:
private int iCoins;
public int Coins
{
Get
{
return this.iCoins;
}
Set
{
this.iCoins = value;
}
}
is there a way to do something like this in java
-
Nov 1st, 2005, 01:35 PM
#2
Fanatic Member
Re: Get {} Set {}?
I dont have a java compiler on the computer i am using, so i dont know if this syntax is allowed. Try this.
Code:
private int iCoins;
public int getCoins(int value)
{
//Set 'iCoins'
this.iCoins = value;
//Return the value of 'iCoins'
return iCoins;
}
I dont think you need to use this when returning iCoins.
Last edited by x-ice; Nov 1st, 2005 at 01:38 PM.
-
Nov 1st, 2005, 02:34 PM
#3
New Member
Re: Get {} Set {}?
In java you have to write the accessors yourself:
Code:
class Person
{
private String firstName;
private String surName;
void setFirstName(String s)
{
firstName = s;
}
String getFirstName()
{
return firstName;
}
String getSurName()
{
return SurName;
}
}
SurName would be read-only as you have no set method for it. To get and set the properties:
Code:
p = new Person();
p.setFirstName('Santa');
I am the real Santa Clause 
-
Nov 1st, 2005, 02:55 PM
#4
Thread Starter
<?="Moderator"?>
Re: Get {} Set {}?
damn i was hoping that i would be able to do
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!";
}
}
or something alone those line.
-
Nov 10th, 2005, 06:36 AM
#5
Re: Get {} Set {}?
Nope. Properties don't exist in Java proper. (No loss IMHO.) Bean scripting environments, however, will use the get/set pattern to create properties.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|