开发者

Does Java have "properties" that work the same way properties work in C#?

In C#, you can use properties to make a data field publicly accessible (allowing the user to directly access it), and yet retain the ability to perform data validation on those directly-accessed fields. Does Java have something similar? For Instance, suppose there exists a C# class with the following implementation(see below):

public class newInt{

    public newInt(){...}

    public int x{
        get{ return this.x }
        set{ this.x = isValid(value) }
    }
}

private static int isValid(int value){...}

This definition in the class allows the user to "naturally" use the data field 'x' when retrieving values from it and assigning values to it. Below is how it would be used in main.

public class Test{

    public static void main(String[] args){

        newInt a = new newInt();
        a.x = 50;

        int b = a.x;
    }
}

The qu开发者_如何学JAVAestion is... can java do this as well? if so, what is it called?


No.

That's why Java has getters/setters.

In C# you typically have something like:

public class SomeObject
{
    private string _title = "";

    public string Title { get { return _title; } set { _title = value; } }
}

// Or with Auto-Properties
public class SomeObjectAutoProperties
{
    public string Title { get; set; }
}

The Java getter/setter equivalent would be:

public class SomeObject
{
    private String _title = "";

    public string getTitle() { return _title; }

    public void setTitle(String value) { _title = value; }
}


There's the Java platform, and there's the Java language.

The Java language does not support properties (and probably never will), but you are not forced to use the Java language to work with the Java platform (just as you don't need to stick to C# in order to work with .NET platform).

Check:

  • http://groovy.codehaus.org/
  • http://www.jython.org/

And many others.


Nope, you would use getter and setter methods instead. This is a Java convention.

public class newInt {

    public newInt() {...}

    private int _x = 0;

    public int getX() {
        return this._x;
    }

    public void setX(int x) {
        this._x = isValid(x);
    }
}


No. Java doesn't have properties. The Java idiom is to use mutator/accessor (getter/setter). Even though a lot of people are in favor of adding them, it is unlikely they will be included in the next version (Java 7).

Curiously, JavaFX has properties.

Keep in mind that when Java was born it borrowed a lot of ideas from C++. Thus some of the syntax and idioms are very similar to that language.


No, it hasn't.

I really have a bit of a problem to understand this C# properties, because, I think one of the rules is to perform as less code as possible in them and since they are already public, why don't use public attributes instead?

So, your Java equivalent ( and probably ugly ) would be:

public class NewInt { // In Java class names start with uppercase by convention 
     public int x; 
}

And you use it like:

 NewInt ni = new NewInt();
 ni.x = 50;
 int b = ni.x;

There is something I'm missing that's for sure, but, most of the times this would do ( BTW I never code like this :P )

BTW

I don't really like getters and setters, but I accept them as part of the Java conventions.

I just would like they have used this instead:

 public class NewInt {
     private int x;
     public int x(){ 
         return this.x;
     }
     public void x(int value ) {
         this.x=value;
     }
  }

So the usage would've been:

  NewInt a = new NewInt();
  a.x(10);
  int b = a.x();

Probably in the next Java life.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜