在线等 孙鑫程序出现的问题

shunzyt 2007-12-08 09:31:19
我在孙鑫老师的视频里看到他的例子代码,然后我自己默写修改,最后出现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);
}
}
...全文
118 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
leem06 2007-12-10
  • 打赏
  • 举报
回复

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);
}
}

Awing911 2007-12-10
  • 打赏
  • 举报
回复
package cn.edu.csu;

public class Test1 {

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

ltc_mouse 2007-12-09
  • 打赏
  • 举报
回复
1楼提到“有main方法的类才能是public的”
这个似乎不正确吧

通常,main方法只是提供了一个如何使用一个或多个Java类的例子,不是必需的
2楼指出的在dos下编译运行,需要使用不同参数,就是因为Student类中没有main方法,main方法是放在Test类中的(这种做法是有好处的,将来打包程序时,不用改源代码,直接删除Test.class就可以了)
  • 打赏
  • 举报
回复
1楼正解
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,p

main方法是静态方法,
所以要改成:

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
dracularking 2007-12-08
  • 打赏
  • 举报
回复

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中使用

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧