关于Java“属性"之间的区别,和C#的属性 [英] In regards to the difference between Java "Properties" and C#' Properties

查看:89
本文介绍了关于Java“属性"之间的区别,和C#的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为实习的一部分,我在C#工作了六个月.我从实习中学到的东西是C#属性,AKA设置程序和获取程序的美.在实习开始之初,财产也是我困惑的根源,但是使用它一段时间后,我爱上了它.

I worked with C# for a good six months as part of my internship. Something I learned over that internship is the beauty of C# properties, AKA setters and getters. In the beginning of the internship, properties was also a source of my confusion, but using it for awhile, I fell in love with it.

回到Java上课,我不得不告别它.

Coming back to Java for classes, I had to bid goodbye to it.

直到..我开始了这个简单的任务:

Until.. I started this simple assignment:

我的方法和构造函数:

private double xCoor;
private double yCoor;

public Point(double xCoor, double yCoor)
{
    this.xCoor = xCoor;
    this.yCoor = yCoor;
}

public void setCoor(double xCoor, double yCoor)
{
    this.xCoor = xCoor;
    this.yCoor = yCoor;
}

public void printCoor()
{
    System.out.println("(" + xCoor + ", " + yCoor + ")");
}

为了演示,这是我的主要方法:

For demonstration, here is my main method:

    Point pointOne = new Point(6.0, 7.0);
    pointOne.printCoor();
    pointOne.setCoor(9.0, 3.0);

    pointOne.xCoor = 9.0;
    pointOne.yCoor = 7.0;

    System.out.println("Java Properties: " + pointOne.xCoor);
    System.out.println("Java Properties: " + pointOne.yCoor);

您可以说,以Point pointOne = ..;开头的前三行是"Java"处理方式.您可以看到以print语句形式的getter和以.setCoor(..)形式的setter.

As you can tell, the top three lines begining with Point pointOne = ..; is the "Java" way to do things. You can see the getter which is in the form of a print statement, and the setter which is in the form of .setCoor(..).

现在,我的问题是,在Java中(我刚刚学到了)-您还可以通过pointOne.xCoor = ..通过类似属性的声明来设置私有变量,当然,也可以通过与pointOne.xCoor相同的方式来获取它们.

Now, my question is, in Java (I JUST learned) - you can also set the private variables via a properties-like declaration through pointOne.xCoor = .., and of course, get them via the same way with pointOne.xCoor.

我知道与C#的这种差异,因为可以像这样(如果是C#)手动声明属性的名称.

I am aware of this discrepancy with C#'s way, in that the name of the property can be manually declared, like so (if it's C#).

public string XCoor
{
    get { return this.xCoor; }
    set { xCoor = value; }
}

有人可以帮助我理解Java进行属性的方式与C#方式之间的区别吗?

Can someone help me understand the difference between the way Java does properties and the C# way?

推荐答案

在Java中,如果没有公共的getter和setter方法,则不能从另一个类中设置私有字段.没有getter和setter方法的私有字段只能在同一类中设置和查看.

In java you can not set a private field from another class if there is no public getter and setter methods. Private fields without getter and setter methods can only be set and viewed within the same class.

例如

public class A {
    private int a;
    private int b;
    private int c;
    public int s;

    public A(){
        a=5; // In same class
        b=3; // In same class
        c=7;
        s=9;
    }

    public int sum(){
        return a+b; // I can access a and b directly, because we are in the same class with them.
    }

    public getC(){
        return c; // Other classes can read value of c with this public method
    }

    public setC(int value){
        c=value; // Other classes can set value of c with this public method
    }

}


public class B{
    private A obj = new A(); // 

    public void someMethod(){
        int t = obj.getC(); // this is ok
        int g = obj.s; // this is ok because s is public
        int f = obj.c // compiler error, c is private, i can't see it from another class directly
        int p = obj.a // compiler error, a is private, i can't see it from another class directly
    }
}

这篇关于关于Java“属性"之间的区别,和C#的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆