我编写了以下java程序
(1)
MyMax.javaclass MyMax
{
private float price; public MyMax()//构构函数
{ price=0.0f; } public void updateMax( float i )
{ if (price<i)
{ price=i; }
} public float getPrice()
{ return price; }
}javac MyMax.java 编译通过(2)
UseMyMaxClass.javapublic class UseMyMaxClass
{ public static void main(String args[])
{ MyMax a=new MyMax();
System.out.println(a.getPrice());
a.updateMax(20.0f); System.out.println(a.getPrice());
}
}javac UseMyMaxClass.java
编译通过
javac 运行正常
---------------------------------------------------------
但在我在程序(1)前加入package 语句;(2)加入import语句后却老是有问题
(1)
package MyClass;class MyMax
{
private float price; public MyMax()//构构函数
{ price=0.0f; } public void updateMax( float i )
{ if (price<i)
{ price=i; }
} public float getPrice()
{ return price; }
}javac -d . MyMax
编译通过,生成了 MyClass/MyMax.class(2)
import MyClass.MyMax;public class UseMyMaxClass
{ public static void main(String args[])
{ MyMax a=new MyMax();
System.out.println(a.getPrice());
a.updateMax(20.0f); System.out.println(a.getPrice());
}
}javac UseMyMaxClass.java
一直提示:UseMyMaxClass.java:5: cannot access MyMax
bad class file: .\MyMax.java
file does not contain class MyMax
Please remove or make sure it appears in the correct subdirectory of the cla
th.
{ MyMax a=new MyMax();
^
1 error
(1)
MyMax.javaclass MyMax
{
private float price; public MyMax()//构构函数
{ price=0.0f; } public void updateMax( float i )
{ if (price<i)
{ price=i; }
} public float getPrice()
{ return price; }
}javac MyMax.java 编译通过(2)
UseMyMaxClass.javapublic class UseMyMaxClass
{ public static void main(String args[])
{ MyMax a=new MyMax();
System.out.println(a.getPrice());
a.updateMax(20.0f); System.out.println(a.getPrice());
}
}javac UseMyMaxClass.java
编译通过
javac 运行正常
---------------------------------------------------------
但在我在程序(1)前加入package 语句;(2)加入import语句后却老是有问题
(1)
package MyClass;class MyMax
{
private float price; public MyMax()//构构函数
{ price=0.0f; } public void updateMax( float i )
{ if (price<i)
{ price=i; }
} public float getPrice()
{ return price; }
}javac -d . MyMax
编译通过,生成了 MyClass/MyMax.class(2)
import MyClass.MyMax;public class UseMyMaxClass
{ public static void main(String args[])
{ MyMax a=new MyMax();
System.out.println(a.getPrice());
a.updateMax(20.0f); System.out.println(a.getPrice());
}
}javac UseMyMaxClass.java
一直提示:UseMyMaxClass.java:5: cannot access MyMax
bad class file: .\MyMax.java
file does not contain class MyMax
Please remove or make sure it appears in the correct subdirectory of the cla
th.
{ MyMax a=new MyMax();
^
1 error
MyMax.java必须是publicpublic class MyMax
{.......
}