2011-09-03 111 views
0

Eclipse说在下面的代码中关键字“new”和“dog”有错误,但我直接从书中复制了这个示例。我不知道什么是错在这里在Eclipse中的类中创建新对象时遇到问题

Eclipse的错误#1:狗不能被解析为一个变量 错误#2:语法错误令牌“新”,删除此令牌

package pkg; 

// creating the Dog class 
class Dog { 
    int size; 
    String breed; 
    String name; 

    void bark(){ 
     System.out.println("Ruff! Ruff!"); 
    } 
} 

// this function does the testDrive 
public class HelloWorld { 
    public static void main(String[] args) { 
     // problem occurs here, both "new" and "dog" underlined 
     Dog d = new Dog; 
     d.size = 40; 
     d.bark(); 

    } 
} 

回答

2

你缺少构造支撑,更具体:

Dog d = new Dog(); 
+0

它的工作原理!!!!谢谢 – user133466

+0

没问题,很高兴帮助。 –

2

您需要说new Dog(),而不仅仅是new Dog

唉,Java不是C++;您不会因为使用默认构造函数而忽略括号。

相关问题