day09--继承的入门&练习

This commit is contained in:
2026-01-05 11:56:29 +08:00
parent 92db74e2fb
commit fe349c9f53
4 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.inmind.extend01.test;
public class Animal {
String name;
int age;
String color;
public void eat(){
System.out.println("到点了,该吃饭了");
}
}

View File

@@ -0,0 +1,7 @@
package com.inmind.extend01.test;
public class Cat extends Animal{
public void catchMouse(){
System.out.println("猫能抓老鼠");
}
}

View File

@@ -11,4 +11,21 @@ package com.inmind.extend01.test;
创建猫和狗对象,设置属性,并调用各自的方法
*/
public class Demo02 {
public static void main(String[] args) {
//创建一只狗
Dog dog = new Dog();
dog.name = "小黄";
dog.age = 1;
dog.color = "黄色";
dog.eat();
dog.watchDoor();
//创建一只猫
Cat cat = new Cat();
cat.name = "小白";
cat.age = 2;
cat.color = "白色";
cat.eat();
cat.catchMouse();
}
}

View File

@@ -0,0 +1,8 @@
package com.inmind.extend01.test;
public class Dog extends Animal{
public void watchDoor(){
System.out.println("狗在看门");
}
}