当前位置: 技术问答>java相关
关于JAVA的简单问题
来源: 互联网 发布时间:2015-02-22
本文导语: 有个例子: import java.io.*; class FatherClass { public int DM = 2000; } class SonClass extends FatherClass { public float DM = (float)1999.999; public void showDM() { System.out.println("Old DM: "+super.DM); System.out.println("Ne...
有个例子:
import java.io.*;
class FatherClass
{
public int DM = 2000;
}
class SonClass extends FatherClass
{
public float DM = (float)1999.999;
public void showDM()
{
System.out.println("Old DM: "+super.DM);
System.out.println("New DM: "+DM);
}
}
public class SuperSample
{
public static void main (String args[])
{
SonClass s=new SonClass();
s.showDM();
}
}
在类SonClass中定义的变量:DM在赋初值时为何要用类型转换?我想通过实例s访问FatherClass中的DM行吗,怎么实现?
import java.io.*;
class FatherClass
{
public int DM = 2000;
}
class SonClass extends FatherClass
{
public float DM = (float)1999.999;
public void showDM()
{
System.out.println("Old DM: "+super.DM);
System.out.println("New DM: "+DM);
}
}
public class SuperSample
{
public static void main (String args[])
{
SonClass s=new SonClass();
s.showDM();
}
}
在类SonClass中定义的变量:DM在赋初值时为何要用类型转换?我想通过实例s访问FatherClass中的DM行吗,怎么实现?
|
1.1999.999 has a type of "double",
use
public float DM = 1999.999F;
2.实例s访问FatherClass中的DM:
use upcasting:
System.out.println("Father's DM: "+((FatherClass)s).DM);
use
public float DM = 1999.999F;
2.实例s访问FatherClass中的DM:
use upcasting:
System.out.println("Father's DM: "+((FatherClass)s).DM);