我正在尝试学习 java 中的对象和类,在线教程给了我无法在 Eclipse 上编译/运行的代码。
当我在保存类后编译程序时,它给了我错误:
线程“主”java.lang.Error 中的异常:未解决
的编译问题:方法 setAge(int) 未定义 Puppy
类型 Puppy.main(Puppy.java:23) 的方法 getAge() 未定义
public class Puppy{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public setAge( int age ){
puppyAge = age;
}
public getAge( ){
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args){
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
http://www.tutorialspoint.com/java/java_object_classes.htm
(代码在页面中间)
它是一个构造函数类程序。不应该有返回类型。