简体   繁体   English

我们应该使用什么原始或什么类型来表示java中的电话号码?

[英]what primitive or what type should we use to represent a telephone number in java?

what type could contain a number like 2023209999 in java? 什么类型可以在java中包含像2023209999这样的数字? do you think that using a string type to represent a telephone number is a good idea? 你认为使用字符串类型来表示电话号码是个好主意吗?

Using a string is a very good idea. 使用字符串是一个非常好的主意。 Remember that the point of OOP is that different types of data have different usage patterns. 请记住,OOP的重点是不同类型的数据具有不同的使用模式。 So let's look at a phone number patterns. 那么让我们看一下电话号码模式。

  • Do we add phone numbers or perform other arithmetic on them? 我们是否添加电话号码或对其进行其他算术运算?
  • Do we split it based on length, match against parts of it, replace parts of it? 我们是根据长度分割它,与它的部分匹配,更换它的一部分吗?

The answer to the first question is no. 第一个问题的答案是否定的。 So we don't manipulate them like numbers. 所以我们不像数字那样操纵它们。 The answer to the second question is yes, so we manipulate them like strings. 第二个问题的答案是肯定的,所以我们像字符串一样操纵它们。

Now, many here are advocating making phones a class by itself. 现在,许多人都在倡导自己制作手机。 There is merit in this regard, but I'm addressing the more pressing concern of how do you store the phone number, which is something you need to do no matter if a phone is a class or not. 在这方面有一些优点,但我正在解决你如何存储电话号码这一更紧迫的问题,无论电话是否是班级,这都是你需要做的事情。 A phone class which stored its data as a number would not be a good fit. 将数据存储为数字的电话类不太适合。

我会写一个PhoneNumber类,它使用String作为底层存储,并添加验证/漂亮的格式化功能。

I'd say a String at the least, but personally, I'd make a PhoneNumber object. 我至少会说一个String,但就个人而言,我会创建一个PhoneNumber对象。 It's the sort of thing that affords itself to extra methods such as: 这种方式可以提供额外的方法,例如:

 boolean isValid();

 PhoneNumberUtils.getCountry(PhoneNumber number);
 PhoneNumberUtils.getState(PhoneNumber number);

...or whatever. ...管他呢。 One thing I'd be thinking out for is just letting people put in phone numbers and getting the system to learn the rest. 我要考虑的一件事就是让人们输入电话号码并让系统学习其余部分。 I despise entering data that could be determined by the system. 我鄙视输入可由系统确定的数据。 This is just my preference. 这只是我的偏好。

On a simpler level, just encapsulating the String in an PhoneNumber object gives your brain a handle ... in a week or so when your brain wonders "Where should this phone number method go?", you may find yourself with a quick answer. 在一个更简单的层面上,只需将字符串封装在一个PhoneNumber对象中就可以让你的大脑处理......在你的大脑想知道“这个电话号码方法应该去哪里?”的一周左右的时间里,你可能会发现自己的答案很快。

it's rather late, but I may add my 2 cents .... 现在已经很晚了,但我可能会增加2美分......

I am in telecom and have made best experience to store phone numbers in structures (or objects) with character members of variable length, ie 我在电信行业,并且已经将电话号码存储在具有可变长度字符成员的结构(或对象)中的最佳体验,即

struct TelephoneNumber (
    InternationalPrefix VARCHAR;
    AreaCode VARCHAR;
    Subscriber VARCHAR;
    Extension VARCHAR;)

I never store access digits (the zero's, double zeros, pluses etc.), they don't belong to to the telephone number per se but are part of what I may call "dialing rules" 我从不存储访问数字(零,双零,加号等),它们不属于电话号码本身,但是我称之为“拨号规则”的一部分

struct DialingRule (
    International VARCHAR;
    National VARCHAR;
    Local VARCHAR;)

typical values for DialingRule are "00", "0", NULL for a direct line, and "000", "00", "0" for a PBX requiring a "0 to get the line" DialingRule的典型值为“00”,“0”,直接行为NULL,“000”,“00”,“0”表示PBX需要“0来获取行”

to "display" a number you can freely format the objects, insert hyphens, brackets or whatever you fancy “显示”一个数字,你可以自由地格式化对象,插入连字符,括号或任何你想要的

to create a dialable sequence I determine the type (international, national or local) by comparing the corresponding elements of the FROM and TO number and add the respective string from the dialing rule set as a prefix. 创建可拨号序列我通过比较FROM和TO编号的相应元素来确定类型(国际,国家或本地),并将拨号规则集中的相应字符串添加为前缀。

This all may sound like an overkill, but for international applications with strong requirements on data integrity and with strong links to hardware I didn't come up with any better. 这听起来像是一种矫枉过正,但对于对数据完整性有强烈要求且与硬件有强大联系的国际应用,我没有提出更好的建议。 It removes ambiguities and the need for hardcoding lenghts etc. when you want to manipulate numbers. 当你想操纵数字时,它消除了模糊性和硬编码长度等的需要。 It's also easy to prefill parts of the structure from country / city lookup tables containing ISO country codes, IATA city codes and their corresponding prefixes. 从包含ISO国家/地区代码,IATA城市代码及其相应前缀的国家/城市查找表中预填充部分结构也很容易。

Good luck MikeD 祝你好运MikeD

I think that a dedicated PhoneNumber class is the way to go about it. 我认为专用的PhoneNumber课程是实现它的方法。 Phone number are not just strings. 电话号码不仅仅是字符串。 First and foremost, phone numbers obey to rules, such as: they only contain digits, in the US they can contain either 7 or 10 digits. 首先,电话号码遵守规则,例如:它们只包含数字,在美国,它们可以包含7或10位数字。 You'd need a constructor to make sure that your phone numbers are correct. 您需要一个构造函数来确保您的电话号码正确无误。

Second, a class will make it easy for you to steamline the differences between various formats. 其次,课程将使您轻松地简化各种格式之间的差异。 For instance, 555-4834 and 5554834 are different strings but are the same phone number. 例如,555-4834和5554834是不同的字符串,但是电话号码相同。

Finally, you'd probably want methods such as: getAreaCode() or getLocalNumber() Calling such a method is much more concise and much less error prone than manipulating a String directly: 最后,您可能需要以下方法:getAreaCode()或getLocalNumber()调用这样的方法比直接操作String更简洁,更不容易出错:

String phoneNumber pn = ....;
String localNumber = pn.length() == 7 ? pn : pn.substring(4) : 

In Germany area codes start with a 0 so a integer representation would lose that information. 在德国,区域代码以0开头,因此整数表示将丢失该信息。

Still I wouldn't recommend just using a String. 我仍然不建议只使用字符串。
Instead use a Phonenumber class (or interface and implementations). 而是使用Phonenumber类(或接口和实现)。 This approach has some advantages. 这种方法有一些优点。
If at some point you find that a String is insufficient you just have to change the Phonenumber class not every class that uses Phonenumbers. 如果在某些时候你发现String不够,你只需要更改Phonenumber类而不是每个使用Phonenumbers的类。
Additionally it would allow you to seperate area code and number internally. 此外,它允许您在内部分隔区号和数字。

If you need to record leading 0 or + for international then you should use a String. 如果你需要记录前导0或+为国际,那么你应该使用一个字符串。 If you ain't worried about these you can just use a long. 如果你不担心这些,你可以使用很长时间。 eg 例如

long phoneNumber = 2023209999L; // the L is for a long constant.

It would honestly come down to what you plan to do with it. 老实说,你可以归结为你打算用它做什么。 If you just want to store it to reprint it a string or maybe even a long would be fine, assuming we are dealing with US numbers. 假如我们正在处理美国数字,如果你只想存储它来重新打印一个字符串,甚至可能很长一段时间。

If you want to do something more sophisticated making a Class with containing several strings, one for each component. 如果你想做一些更复杂的事情,使一个包含几个字符串的Class,每个组件一个。

Basically just not enough info here to make a real decision. 基本上这里没有足够的信息做出真正的决定。

Using a String Type allows you to break apart the phone number without any voodoo or casting. 使用String类型允许您拆分电话号码而无需任何伏都教或强制转换。

For instance, if you change the format of how you accept phone numbers, you'll have to do extra work to get the phone number contatenated if it is a long instead of a string . 举例来说,如果你改变你如何接受电话号码的格式,你就必须做一些额外的工作来获取contatenated的电话号码,如果它是一个long而不是一个的string

Another thing to consider: an int would be too small. 另一件需要考虑的事情是: int太小了。 the maximum value an int can hold is 2,147,483,647. int可以容纳的最大值是2,147,483,647。 as far as primitives are concerned, long is your best bet. 就原始人而言, long是最好的选择。

It depends on what you're doing. 这取决于你在做什么。 Let's say you want to be able to represent international numbers, local numbers, branch exchange numbers, etc. In this case, a String is a bad choice. 假设您希望能够表示国际号码,本地号码,分支交换号码等。在这种情况下,String是一个糟糕的选择。 It doesn't have any meta-information. 它没有任何元信息。 You probably want a class to represent a phone number. 您可能希望课程代表电话号码。

As far as what you use to represent the phone number in this class, you could use a BigInteger (to allow you to have international numbers), or more simply, you could store each portion of a phone number as a long. 至于您用来表示此课程中的电话号码,您可以使用BigInteger(允许您拥有国际号码),或者更简单地说,您可以将电话号码的每个部分存储为长号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM