本帖最后由 王德升 于 2012-6-3 23:14 编辑
/*
class Computer
{
void run()
{
System.out.println("电脑运行");
}
}
class Teacher
{
private String name;
Teacher(String name)
{
this.name = name;
}
public void prelect()
{
new Computer().run();
System.out.println("上课");
}
}
class ExceptionTest
{
public static void main(String[] args)
{
new Teacher("毕老师").prelect();
}
}
*/
class Teacher
{
private String name;
private Computer cmpt;
Teacher(String name,Computer cmpt)
{
this.name = name;
//cmpt = new Computer;
this.cmpt = cmpt;
}
public void prelect()
{
cmpt.run();
System.out.println("上课");
}
}
class Computer
{
void run()
{
System.out.println("电脑运行");
}
void select()
{
System.out.println("重启");
}
}
class ExceptionTest
{
public static void main(String[] args)
{
Teacher t = new Teacher("毕老师","new Computer()");
t.prelect();
}
}
/*
为什么注释的编译运行都可以,但是没有注释的却是编译通不过呢,
ExceptionTest.java:77: 错误: 无法将类 Teacher中的构造器 Teacher应用到给定类型;
Teacher t = new Teacher("毕老师","new Computer");
^
需要: String,Computer
找到: String,String
原因: 无法通过方法调用转换将实际参数String转换为Computer
1 个错误
这是为什么?
如果是引号的关系那为什么不能加呢?
*/ |
|