我在孙鑫老师的视频里看到他的例子代码,然后我自己默写修改,最后出现3个问题,我不知道为什么,代码如下,希望高手编译一下,看下为什么,告诉我下,不胜感激,我想了好久没想明白
public class Student implements Cloneable
{
int age;
String name;
Professor p;
Student(int age,String name,Professor p)
{
this.name=name;
this.age=age;
this.p=p;
}
public Object clone()
{ Object o=null;
try
{
o=super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return o;
}
}
class Professor implements Cloneable
{
int age;
String name;
Professor(int age,String name)
{
this.name=name;
this.age=age;
}
public Object clone()
{ Object o=null;
try
{
o=super.clone();
}
catch(Exception e)
{
System.out.println(e.toString());
}
return o;
}
}
class Test
{
Professor p=new Professor(44,"ccc");
Student s1=new Student(22,"aaa",p);
public static void main(String[] args)
{
System.out.println(s1.p.name);
}
}
public class Student implements Cloneable
{
int age;
String name;
Professor p;
Student(int age,String name,Professor p)
{
this.name=name;
this.age=age;
this.p=p;
}
public Object clone()
{ Object o=null;
try
{
o=super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return o;
}
}
class Professor implements Cloneable
{
int age;
String name;
Professor(int age,String name)
{
this.name=name;
this.age=age;
}
public Object clone()
{ Object o=null;
try
{
o=super.clone();
}
catch(Exception e)
{
System.out.println(e.toString());
}
return o;
}
}
class Test
{
Professor p=new Professor(44,"ccc");
Student s1=new Student(22,"aaa",p);
public static void main(String[] args)
{
System.out.println(s1.p.name);
}
}
public class Test {
public static void main(String[] args) {
Professor p = new Professor(44, "ccc");
Student s1 = new Student(22, "aaa", p);
System.out.println(s1.p.name);
}
}
有main方法的类才能是public的
还有p,s1得是static的引用型 如果在main中使用
class Test
{
Professor p=new Professor(44,"ccc");
Student s1=new Student(22,"aaa",p);
public static void main(String[] args)
{
System.out.println(s1.p.name);
}
}这样写是不行的,因为无法从静态上下文中引用非静态 变量 s1,pmain方法是静态方法,
所以要改成:class Test
{
public static void main(String[] args)
{
Professor p = new Professor(44, "ccc");
Student s1 = new Student(22, "aaa", p);
System.out.println(s1.p.name);
}
}
还有如果你是在dos下编译运行,那么应该是:
javac Student.java
java Test
这个似乎不正确吧通常,main方法只是提供了一个如何使用一个或多个Java类的例子,不是必需的
2楼指出的在dos下编译运行,需要使用不同参数,就是因为Student类中没有main方法,main方法是放在Test类中的(这种做法是有好处的,将来打包程序时,不用改源代码,直接删除Test.class就可以了)
public static void main(String args[]){
Professor p = new Professor(44, "ccc");
Student s1 = new Student(22, "aaa", p);
System.out.println(s1.p.name);
}}结果:
ccc
import java.sql.Connection;
import java.sql.DriverManager;class Student implements Cloneable {
int age; String name; Professor p; Student(int age, String name, Professor p) {
this.name = name;
this.age = age;
this.p = p;
} public Object clone() {
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
return o;
}
}class Professor implements Cloneable {
int age; String name; Professor(int age, String name) {
this.name = name;
this.age = age;
} public Object clone() {
Object o = null;
try {
o = super.clone();
} catch (Exception e) {
System.out.println(e.toString());
}
return o;
}
}public class Test {
public static void main(String[] args) {
Professor p = new Professor(44, "ccc"); Student s1 = new Student(22, "aaa", p);
System.out.println(s1.p.name);
}
}