数字类中的抽象方法 [英] Abstract Methods in Number Class

查看:187
本文介绍了数字类中的抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Number类为Double,Int,Long和Float的转换方法提供了抽象方法,但不提供byte和short的抽象方法?



总体而言,我对何时使用抽象方法感到困惑,因为我刚开始学习Java。



谢谢任何人都可以提供任何洞察。

解决方案

看看他们的来源为什么:

  public byte byteValue(){
return(byte)intValue();
}

public short shortValue(){
return(short)intValue();它们都依赖于 intValue()的事实,因此,



这让我想知道为什么他们不只是使

  public int intValue(){
return(int)longValue();
}

由于同样的规则适用。



注意,没有什么说你不能重写这些方法。



我的机器上的结果:

  C:\Documents and Settings\glow\My Documents> java SizeTest 
int:45069467
short:45069467
byte:90443706
long:11303499

C:\Documents和Settings \glow \ My Documents>

类:

 code> class SizeTest {

/ **
*对于每个基本类型int,short,byte和long,
*试图创建一个和你一样大的数组可以直到
*内存不足。从数组10000,
*开始,并增加容量1%,直到它引发错误。
*捕获错误并打印尺寸。
* /
public static void main(String [] args){
int len = 10000;
final double inc = 1.01;
try {
while(true){
len =(int)(len * inc);
int [] arr = new int [len];
}
} catch(Throwable t){
System.out.println(int:+ len);
}

len = 10000;
try {
while(true){
len =(int)(len * inc);
short [] arr = new short [len];
}
} catch(Throwable t){
System.out.println(short:+ len);
}


len = 10000;
try {
while(true){
len =(int)(len * inc);
byte [] arr =新字节[len];
}
} catch(Throwable t){
System.out.println(byte:+ len);
}

len = 10000;
try {
while(true){
len =(int)(len * inc);
long [] arr = new long [len];
}
} catch(Throwable t){
System.out.println(long:+ len);
}
}
}


Why is it that the Number Class provides abstract methods for conversion methods for Double, Int, Long, and Float but not abstract methods for byte and short?

Overall I am slightly confused on when to use Abstract methods, as I just began learning Java.

Thanks for any insight anyone can offer.

解决方案

One look at the source for them says why:

public byte byteValue() {
    return (byte)intValue();
}

public short shortValue() {
    return (short)intValue();
}

They both rely on the fact that intValue() will be defined, and just use whatever they provide for that.

This makes me wonder why they don't just make

public int intValue() {
    return (int)longValue();
}

Since the same rule applies.

Note that there's nothing that says you can't override these methods anyway. They don't have to be abstract for you to override them.

Results on my machine:

C:\Documents and Settings\glow\My Documents>java SizeTest
int: 45069467
short: 45069467
byte: 90443706
long: 11303499

C:\Documents and Settings\glow\My Documents>

Class:

class SizeTest {

    /**
     * For each primitive type int, short, byte and long,
     * attempt to make an array as large as you can until
     * running out of memory. Start with an array of 10000,
     * and increase capacity by 1% until it throws an error.
     * Catch the error and print the size.
     */    
    public static void main(String[] args) {
        int len = 10000;
        final double inc = 1.01;
        try {
            while(true) {
                len = (int)(len * inc);
                int[] arr = new int[len];
            }
        } catch(Throwable t) {
            System.out.println("int: " + len);
        }

        len = 10000;
        try {
            while(true) {
                len = (int)(len * inc);
                short[] arr = new short[len];
            }
        } catch(Throwable t) {
            System.out.println("short: " + len);
        }


        len = 10000;
        try {
            while(true) {
                len = (int)(len * inc);
                byte[] arr = new byte[len];
            }
        } catch(Throwable t) {
            System.out.println("byte: " + len);
        }

        len = 10000;
        try {
            while(true) {
                len = (int)(len * inc);
                long[] arr = new long[len];
            }
        } catch(Throwable t) {
            System.out.println("long: " + len);
        }
    }
}

这篇关于数字类中的抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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