电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

主类型的过载


发布日期:2018/4/4
 

主(数据)类型能从一个较小的类型自动转变成一个较大的类型涉及过载问题时这会稍微造成一些混乱下面这个例子揭示了将主类型传递给过载的方法时发生的情况

//: PrimitiveOverloadingjava

// Promotion of primitives and overloading

public class PrimitiveOverloading {

// boolean cant be automatically converted

static void prt(String s) {

Systemoutprintln(s);

}

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(double x) { prt(f(double)); }

void testConstVal() {

prt(Testing with );

f();f();f();f();f();f();f();

}

void testChar() {

char x = x;

prt(char argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

void testByte() {

byte x = ;

prt(byte argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

void testShort() {

short x = ;

prt(short argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

void testInt() {

int x = ;

prt(int argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

void testLong() {

long x = ;

prt(long argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

void testFloat() {

float x = ;

prt(float argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

void testDouble() {

double x = ;

prt(double argument:);

f(x);f(x);f(x);f(x);f(x);f(x);f(x);

}

public static void main(String[] args) {

PrimitiveOverloading p =

new PrimitiveOverloading();

ptestConstVal();

ptestChar();

ptestByte();

ptestShort();

ptestInt();

ptestLong();

ptestFloat();

ptestDouble();

}

} ///:~

若观察这个程序的输出就会发现常数值被当作一个int值处理所以假若可以使用一个过载的方法就能获取它使用的int值在其他所有情况下若我们的数据类型小于方法中使用的自变量就会对那种数据类型进行转型处理char获得的效果稍有些不同这是由于假期它没有发现一个准确的char匹配就会转型为int

若我们的自变量大于过载方法期望的自变量这时又会出现什么情况呢?对前述程序的一个修改揭示出了答案

//: Demotionjava

// Demotion of primitives and overloading

public class Demotion {

static void prt(String s) {

Systemoutprintln(s);

}

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(double x) { prt(f(double)); }

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(float x) { prt(f(float)); }

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(long x) { prt(f(long)); }

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(int x) { prt(f(int)); }

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(short x) { prt(f(short)); }

void f(char x) { prt(f(char)); }

void f(byte x) { prt(f(byte)); }

void f(char x) { prt(f(char)); }

void testDouble() {

double x = ;

prt(double argument:);

f(x);f((float)x);f((long)x);f((int)x);

f((short)x);f((byte)x);f((char)x);

}

public static void main(String[] args) {

Demotion p = new Demotion();

ptestDouble();

}

} ///:~

在这里方法采用了容量更小范围更窄的主类型值若我们的自变量范围比它宽就必须用括号中的类型名将其转为适当的类型如果不这样做编译器会报告出错

大家可注意到这是一种缩小转换也就是说在造型或转型过程中可能丢失一些信息这正是编译器强迫我们明确定义的原因——我们需明确表达想要转型的愿望

上一篇:用fasterCSV快速处理导入CSV文件

下一篇:实例编程:JFreechart 的一些用法