class animals {
int id;
String name;
animals(int id,String name){
this.id=id;
this.name=name;
}
void eat(){
System.out.println(name+" is eating");
}
}
class cat extends animals{
String color;
cat(int id,String name,String color){
super(id,name);
this.color=color;
}
}
class testmethod{
public static void main(String[] args) {
cat tom=new cat(1,"tom","blue");
tom.eat();
}
}


这篇博客介绍了Java编程中面向对象的基本概念,通过创建`animals`类和`cat`子类来展示继承的概念。`animals`类包含`id`和`name`属性以及`eat()`方法,而`cat`类在`animals`基础上增加了`color`属性,并调用了父类的构造器。在`testMethod`类中,实例化了`cat`对象并调用其`eat()`方法,展示了类的实例化和方法的使用。
931

被折叠的 条评论
为什么被折叠?



