day09--继承的入门&练习
This commit is contained in:
11
day09/src/com/inmind/extend01/test/Animal.java
Normal file
11
day09/src/com/inmind/extend01/test/Animal.java
Normal 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("到点了,该吃饭了");
|
||||||
|
}
|
||||||
|
}
|
||||||
7
day09/src/com/inmind/extend01/test/Cat.java
Normal file
7
day09/src/com/inmind/extend01/test/Cat.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package com.inmind.extend01.test;
|
||||||
|
|
||||||
|
public class Cat extends Animal{
|
||||||
|
public void catchMouse(){
|
||||||
|
System.out.println("猫能抓老鼠");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,4 +11,21 @@ package com.inmind.extend01.test;
|
|||||||
创建猫和狗对象,设置属性,并调用各自的方法
|
创建猫和狗对象,设置属性,并调用各自的方法
|
||||||
*/
|
*/
|
||||||
public class Demo02 {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
day09/src/com/inmind/extend01/test/Dog.java
Normal file
8
day09/src/com/inmind/extend01/test/Dog.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.extend01.test;
|
||||||
|
|
||||||
|
public class Dog extends Animal{
|
||||||
|
|
||||||
|
public void watchDoor(){
|
||||||
|
System.out.println("狗在看门");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user