package debug; /**
1、定义一个公共的动物类,包含名字、年龄、颜色和吃饭东西方法
2、定义一个猫类,继承动物类,同时拥有玩游戏的本领
3、定义一个狗类,继承动物类,同时拥有看门的本领
*/ class Animal{
private String name;
private int age;
private String color; public Animal() { } public Animal(String name,int age,String color) {
this.name = name;
this.age = age;
this.color = color;
} public String getName() {
return name;
} public int getAge() {
return age;
} public String getColor() {
return color;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public void setColor(String color) {
this.color = color;
} public void eat() {
System.out.println("饿了就要吃饭");
}
} class Cat extends Animal{
public Cat() {} public Cat(String name,int age,String color) {
super(name,age,color);
} public void playGame() {
System.out.println("猫都会玩游戏了");
} } public class Demo16 {
public static void main(String[] args) {
Cat c1 = new Cat();
c1.setName("tom");
c1.setAge(3);
c1.setColor("white");
c1.playGame();
System.out.println("猫的名字叫:" + c1.getName() + "\n年龄为:" + c1.getAge() + "\n颜色为:" + c1.getColor()); Cat c2 = new Cat("jerry",5,"yellow");
System.out.println("猫的名字叫:" + c2.getName() + "\n年龄为:" + c2.getAge() + "\n颜色为:" + c2.getColor());
} }
上面代码是经常调试修改后正确的代码,下面将在调试过程中遇到的错误一一罗列如下:
1、在类中的方法经常会忘记带上方法的返回类型: String, int, void.....
2、在每句结束时忘记以分号结束该语句
3、在输出语句中的字符串连接涉及对象调方法时忘记以()结束