首页 > 技术文章 > 私有域

leftfist 2014-09-04 23:05 原文

按道理,类中的私有域,外界不能访问,但是,对于同属一个类的对象,却可以访问,这一点,java和C#都是一样的。

java:

public class Employee {
	private String name;
	public Employee(String name){
		this.name = name;
	}
	public boolean equals(Employee other){
		return name.equalsIgnoreCase(other.name);
	}
}

name是私有域,但在这里,访问并无问题。

c#

    class Employee
    {
        private string name;
        public Employee(string name)
        {
            this.name = name;
        }
        public bool Equals(Employee other)
        {
            return (this.name.CompareTo(other.name) == 0);
        }
    }


推荐阅读