从另一个类获取变量的正确方法 [英] Proper way of getting variable from another class

查看:115
本文介绍了从另一个类获取变量的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过两种方式调用变量。



一个就是这样做:

  MyClass myClass = new MyClass(); 

myLocalVar = myClass.myVarVal;

另一种方法是使用这样的getter:

  myLocalVar = myClass.getMyVarVal(); 

两种方式都运行良好,但我想知道什么是最有效/最合适的做法这个?



谢谢

解决方案

这两种技术都很糟糕,但是使用getter是常见的(也是更安全的)练习。



为了访问班级的公共数据成员(也就是公共领域或公共财产),你必须知道类的实现细节(数据成员名称和数据成员类型)。这是件坏事;它破坏了OOP概念信息隐藏并增加了耦合。



使用getter也很糟糕(如在糟糕的OOP实践中)因为对象不仅仅是包装器围绕数据;对象应该封装功能和数据。 将此存储在此值,以便我以后可以获得它不是功能;它是hoot的功能(就像在笼子里的猴子一样)。吸气剂是;然而,java中的公认实践(以及其他OOP-lite语言,如c ++和c#)。



以免你认为我是象牙塔最纯净,当然我使用getters ;我使用java,所以我使用getters。



Getters可以完成工作(没有双关语),只是不要相信IR gud OOP Prgmr ,因为如果你使用getter,你不是一个好的oop程序员,你只是一个完成工作的程序员。



编辑:也许是更好的方法。 / p>

更好的方法是不使用getter,而是设计你的类,使它们暴露功能而不是数据。在实践中,有一个地方会崩溃;例如,如果需要在JSP页面上显示地址,则将bean放在请求(或会话或blah)中,并使用地址并使用getter公开值。 更纯粹的纯粹方式是将暴露出来的bean在jsp上显示地址功能。



Edit2:也许是一个更好的例子。



说我在美国的一家电话公司工作,我有一个代表客户电话号码的对象。这可能如下所示:

  public class CustomerPhoneNumber 
{
private String npa; //编号计划区域(谷歌搜索nanp了解更多详情)
private String nxx; //交换
private String serviceNumber;

public String toString()
{
return(+ npa +)+ nxx + - + serviceNumber;
}

public boolean equals(Object object)
{
... standard equals implementation(假设有效)
}
}

现在说我从一个网页输入一个电话号码 String inputPhoneNumber 。出于讨论的目的,接收此输入的类称为servlet。



如何回答此问题:我的列表中是否输入了电话号码CustomerPhoneNumber对象?



选项1是将npa,nxx和serviceNumber数据成员公开并访问它们。这很糟糕。



选项2提供npa,nxx和服务号的getter,并将它们与输入进行比较。同样可怕的是,暴露了太多内部细节。



选项3提供了一个返回格式化电话号码的getter(我在上面称之为toString())。这更聪明但仍然很糟糕,因为servlet必须知道getter将使用的格式并确保输入的格式相同。



选项4(我称之为欢迎使用OOP)提供一个接受String的方法,如果匹配客户服务号,则返回true。这样更好,可能看起来像这样(名称很长,但对于这个例子来说足够了):

  public boolean doesPhoneNumberMatchThisInput(final字符串输入)
{
String formattedInput;
String formattedCustomerPhoneNumber = npa + nxx + serviceNumber;

formattedInput = ...从输入中删除所有非数字。

返回StringUtils.equals(formattedCustomerPhoneNumber,formattedInput);
}

这是赢家,因为没有公开任何实施细节。此外,toString可用于在JSP页面上输出电话号码。



StringUtils是 Apache Commons Lang


I can call variables 2 ways.

One is just to do it like this:

MyClass myClass = new MyClass();    

myLocalVar = myClass.myVarVal;

And the other way is to use a getter like this:

myLocalVar = myClass.getMyVarVal();

Both ways are working fine, but I was wondering what would be the most efficient/proper way of doing this?

Thanks

解决方案

Both techniques are terrible, but using the getter is the common (and safer) practice.

In order to access a public data member (a.k.a. public field or public property) of a class, you must know the implementation details of the class (the data member name and the data member type). This is a bad thing; it breaks the OOP concept "information hiding" and increases "coupling".

Using a getter is also bad (as in a bad OOP practice) because objects are not just wrappers around data; objects are supposed to encapsulate functionality and data. "store this here value so I can get it later" is not functionality; it is hoot functionality (as in a monkey in a cage hooting). Getters are; however, an accepted practice in java (and other OOP-lite languages like c++ and c#).

Lest you think I am some ivory tower purest, of course I use getters; I use java, so I use getters.

Getters are fine for getting the work done (no pun), just don't walk around believing that "I R gud OOP Prgmr", because if you use getters you are not a "good oop programmer", you are just a programmer who gets work done.

Edit: Perhaps a better way.

The better way is to not use getters, but to instead design your classes so they expose functionality not data. In practice, there is a point where this breaks down; for example, if you need to display an address on a JSP page, you put a bean in the request (or session or blah) with the address and expose the values using getters. A "more oop pure" way would be to put a bean that exposed "display the address on a jsp" functionality.

Edit2: Perhaps a better example.

Say I work for a phone company, in the USA, and I have an object that represents a customers phone number. This might look like the following:

public class CustomerPhoneNumber
{
  private String npa; // numbering plan area (google search nanp for more details)
  private String nxx; // exchange.
  private String serviceNumber;

  public String toString()
  {
    return "(" + npa + ") " + nxx + "-" + serviceNumber;
  }

  public boolean equals(Object object)
  {
    ... standard equals implementation (assume this works)
  }
}

Now say I get a phone number as an input from a web page in the form String inputPhoneNumber. For the purposes of discussion, the class that receives this input is called "the servlet".

How can I answer this question: "Is the input phone number in my list of CustomerPhoneNumber objects?"

Option 1 is make the npa, nxx, and serviceNumber data members public and access them. This is terrible.

Option 2 is provide getters for npa, nxx, and service number and compare them with the input. Also terrible, too many internal details exposed.

Option 3 is provide a getter that returns the formatted phone number (I called this toString() above). This is smarter but still terrible because the servlet has to know the format that will be used by the getter and ensure that the input is formatted the same way.

Option 4 (I call this "Welcome to OOP") provide a method that takes a String and returns true if that matches the customer service number. This is better and might look like this (the name is long, but sufficient for this example):

public boolean doesPhoneNumberMatchThisInput(final String input)
{
   String formattedInput;
   String formattedCustomerPhoneNumber = npa + nxx + serviceNumber;

   formattedInput = ... strip all non-digits from input.

   return StringUtils.equals(formattedCustomerPhoneNumber, formattedInput);
}

This is the winner because no implementation details are exposed. Also the toString can be used to output the phone number on a JSP page.

StringUtils is part of Apache Commons Lang.

这篇关于从另一个类获取变量的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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