这个Java代码有哪些错误?
对于上面的代码,它希望显示Java的深层副本。但是myeclipse报告了一些错误。但我不知道这是怎么回事? 有人能帮我指出吗 根据您的建议更改了代码。这个Java代码有哪些错误?,java,Java,对于上面的代码,它希望显示Java的深层副本。但是myeclipse报告了一些错误。但我不知道这是怎么回事? 有人能帮我指出吗 根据您的建议更改了代码。 package example; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.F
package example;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Object;
public static Object copy(Object oldObj) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
public class mytest {
public static void main(String[] args) throws IOException {
clone
}
}
进一步:
谢谢大家!!
我修改了上面的代码,它变得更好了,但仍然不能正常运行。
新的错误消息如下所示:
有什么新建议吗?您需要将方法放入类中
package example;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.Object;
class Utils {
public static Object copy(Object oldObj) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
}
public class mytest {
public static void main(String[] args) throws IOException {
Object clonedObject = Utils.copy(new Object());
clonedObject.notifyAll();
}
}
您可以将其用作:
public class SomeClass{
public static Object copy(Object oldObj) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
在Java中,不能在类之外声明方法(即使是静态方法)。您必须将所有内容(导入除外)都放在一个类中
您将需要以下内容:
public class mytest {
public static void main(String[] args) throws IOException {
Object object = Someclass.copy(someObject);
}
}
方法
publicstaticobjectcopy(objectoldobj)
不在类中。这就是为什么会出现错误 必须添加名为mytest
的类作为此文件的“顶级作用域”,并在其中嵌套所有其他方法
public class YourClass {
public static Object copy(Object oldObj) {
...
}
}
作为旁注,您应该将类名(和文件名)重命名为
MyTest
和MyTest.java
——以匹配java编码约定。java中不能有浮动方法。一切都必须是一个类的成员。在本例中,一个只有静态方法的伪类称为Utils
e、 g
那么你可以这样称呼它:
public class Utils {
// Static methods go here
public static Object copy(Object oldObj) {
// etc
}
}
您应该将您的方法放在一个类中,最好称为
mytest
,因为它是文件名,声明如下
public class mytest {
public static void main(String[] args) throws IOException {
Object clonedObject = Utils.copy(new Object());
// etc
}
}
还请注意,Java中的命名约定是以大写字母开头的类名,因此它将是MyTest
,您的文件应该重命名以反映这一点。试试看
public class mytest {
//Your method here
}
没有不属于类的方法(如JavaScript函数)。
静态意味着该方法属于该类,而不是该类的实例,因为非静态方法会这样做您不能在该类之外编写方法。将你的方法
#复制
放入类中。像这样:
public class MyTest {
public static Object copy(Object oldObj) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
public static void main(String[] args) throws IOException {
clone
}
}
类在哪里?这些方法
应该包含一个类
package example;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.Object;
class Utils {
public static Object copy(Object oldObj) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
}
public class mytest {
public static void main(String[] args) throws IOException {
Object clonedObject = Utils.copy(new Object());
clonedObject.notifyAll();
}
}
您的java类应该如下所示
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class mytest
{
public static void main(String[] args) throws IOException
{
// Now you can use your method here
}
public static Object copy(Object oldObj)
{
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(oldObj);
out.flush();
out.close();
// Retrieve an input stream from the byte array and read
// a copy of the object back in.
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
obj = in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
}
以下是解决错误的以下步骤:
在项目内制作包(特定名称)
在已创建的包内创建名称MyTest
将方法写入或复制到已创建的类中
上面
若要运行该程序,请使一个主方法任意调用该名称,如果
它是静态的
如果您的方法不是静态的,则创建类的对象并通过
这个类的对象
它将解决您的错误并运行您的程序
注意:尝试放置最后
块以释放资源,如object、ByteArrayOutputStream
&ObjectStream
等。。。。它将提高程序的性能。必须将该方法放入类中。无类。。所以它是无效的,但是第11行的公共类mytest{
嘿,Tom,看起来你忘了定义类。请将文件重命名为mytest.java,然后在文档中定义mytest类。这个链接将帮助你,这是基本的java。类名应该以大写字母开头
package name;
imports;
public class MyClass{
public void myMethod(){
}
}