java的一个小程序,帮我看看哪不对?
packagebao1;publicclassHuadan{floatct=500f;floatdx=200f;floatsh=300f;floathf;Stringtc...
package bao1;
public class Huadan {
float ct=500f;
float dx=200f;
float sh=300f;
float hf;
String tc;
public void fangfa(){
System.out.println(tc+hf);
}
}
然后分了几个子类:
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Donggandidai extends Huadan {
public float fangfa1(){
float ctf=0.6f;
float dxf=0.1f;
float shf=0.25f;
tc="动感地带的资费是";
hf=ctf*ct+dxf*dx+shf*sh;
return hf;
}
}
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Quanqiutong extends Huadan{
public float fangfa3(){
float ctf=0.5f;
float dxf=0.1f;
float shf=0.3f;
tc="全球通的资费是";
hf=ctf*ct+dxf*dx+shf*sh;
return hf;
}
}
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Shenzhouxing extends Huadan {
public float fangfa2()
{
float ctf=0.4f;
float dxf=0.1f;
float shf=0.1f;
tc="神州行的资费是:";
hf=ctf*ct+dxf*dx+shf*sh;
return hf;
}
}
建了一个工厂:
public class Factory {
public Huadan fangfa4(int i){
Huadan a;
if(i==1)
{
a=new Donggandidai();
}
if(i==2){
a=new Quanqiutong();
}
else{
a=new Shenzhouxing();
}
return a;
}
}
最后用这个类来调用:
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test extends Factory {
public static void main(String[] args) {
Factory f=new Factory();
Huadan a=f.fangfa4(1);
a.fangfa();
}
} 展开
public class Huadan {
float ct=500f;
float dx=200f;
float sh=300f;
float hf;
String tc;
public void fangfa(){
System.out.println(tc+hf);
}
}
然后分了几个子类:
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Donggandidai extends Huadan {
public float fangfa1(){
float ctf=0.6f;
float dxf=0.1f;
float shf=0.25f;
tc="动感地带的资费是";
hf=ctf*ct+dxf*dx+shf*sh;
return hf;
}
}
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Quanqiutong extends Huadan{
public float fangfa3(){
float ctf=0.5f;
float dxf=0.1f;
float shf=0.3f;
tc="全球通的资费是";
hf=ctf*ct+dxf*dx+shf*sh;
return hf;
}
}
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Shenzhouxing extends Huadan {
public float fangfa2()
{
float ctf=0.4f;
float dxf=0.1f;
float shf=0.1f;
tc="神州行的资费是:";
hf=ctf*ct+dxf*dx+shf*sh;
return hf;
}
}
建了一个工厂:
public class Factory {
public Huadan fangfa4(int i){
Huadan a;
if(i==1)
{
a=new Donggandidai();
}
if(i==2){
a=new Quanqiutong();
}
else{
a=new Shenzhouxing();
}
return a;
}
}
最后用这个类来调用:
package bao1;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Test extends Factory {
public static void main(String[] args) {
Factory f=new Factory();
Huadan a=f.fangfa4(1);
a.fangfa();
}
} 展开
2个回答
展开全部
看了下拉
大致知道你要干什么拉
有几个地方要改
1 public class Donggandidai extends Huadan {
public Donggandidai(){
float ctf=0.6f;
float dxf=0.1f;
float shf=0.25f;
tc="动感地带的资费是";
hf=ctf*ct+dxf*dx+shf*sh;
}
}
还有2个类也这样改,就是都改到构造方法里面去!
2 public class Factory {
public Huadan fangfa4(int i){
Huadan a;
if(i==1)
{
a=new Donggandidai();
}
if(i==2){
a=new Quanqiutong();
}
else{
a=new Shenzhouxing();
}
return a;
}
}
多个判断用swith case,比较好,你这里上面i==1 等于白判断了
switch (i){
case 1:a=new Donggandidai();break;
case 2:a=new Quanqiutong(); break;
case 3:a=new Shenzhouxing(); break;
}
return a
再简单分析下拉
你Factory类的fangfa4方法返回一个Huadan类,你用创建它的子类返回这没有问题的,你在主方法中用一个Huadan来指向这个子类也没有问题,这相当于用一个父类的变量指向一个子类的对象,也就是多态!但是这个时候你是不能再调用子类的独有而父类没有的方法,而且你没有定义子类的构造方法,所以父类的2个成员变量hf和tc没有赋你想赋的值,都只有它们的默认值null和0.0,所以你打印出来的是null0.0,而且你Factory中的fangfa4方法中的判断也不对,如果判断比较多,建议用switch case
大致知道你要干什么拉
有几个地方要改
1 public class Donggandidai extends Huadan {
public Donggandidai(){
float ctf=0.6f;
float dxf=0.1f;
float shf=0.25f;
tc="动感地带的资费是";
hf=ctf*ct+dxf*dx+shf*sh;
}
}
还有2个类也这样改,就是都改到构造方法里面去!
2 public class Factory {
public Huadan fangfa4(int i){
Huadan a;
if(i==1)
{
a=new Donggandidai();
}
if(i==2){
a=new Quanqiutong();
}
else{
a=new Shenzhouxing();
}
return a;
}
}
多个判断用swith case,比较好,你这里上面i==1 等于白判断了
switch (i){
case 1:a=new Donggandidai();break;
case 2:a=new Quanqiutong(); break;
case 3:a=new Shenzhouxing(); break;
}
return a
再简单分析下拉
你Factory类的fangfa4方法返回一个Huadan类,你用创建它的子类返回这没有问题的,你在主方法中用一个Huadan来指向这个子类也没有问题,这相当于用一个父类的变量指向一个子类的对象,也就是多态!但是这个时候你是不能再调用子类的独有而父类没有的方法,而且你没有定义子类的构造方法,所以父类的2个成员变量hf和tc没有赋你想赋的值,都只有它们的默认值null和0.0,所以你打印出来的是null0.0,而且你Factory中的fangfa4方法中的判断也不对,如果判断比较多,建议用switch case
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询