public class littlePenguin {
public void everyDay() {
System.out.println("吃饭");
System.out.println("睡觉");
System.out.println("用小翅膀打豆豆");
}
}
public class middlePenguin {
public void everyDay() {
System.out.println("吃饭");
System.out.println("睡觉");
System.out.println("用圆圆的肚子打豆豆");
}
}
public class bigPenguin {
public void everyDay() {
System.out.println("吃饭");
System.out.println("睡觉");
System.out.println("拿鸡毛掸子打豆豆");
}
}
public class test {
public static void main(String[] args) {
System.out.println("littlePenguin:");
littlePenguin penguin_1 = new littlePenguin();
penguin_1.everyDay();
System.out.println("middlePenguin:");
middlePenguin penguin_2 = new middlePenguin();
penguin_2.everyDay();
System.out.println("bigPenguin:");
bigPenguin penguin_3 = new bigPenguin();
penguin_3.everyDay();
}
}
复制代码
public class littlePenguin {
public void eating() {
System.out.println("吃饭");
}
public void sleeping() {
System.out.println("睡觉");
}
public void beating() {
System.out.println("用小翅膀打豆豆");
}
}
public class middlePenguin {
public void eating() {
System.out.println("吃饭");
}
public void sleeping() {
System.out.println("睡觉");
}
public void beating() {
System.out.println("用圆圆的肚子打豆豆");
}
}
// bigPenguin相同,省略...
public class test {
public static void main(String[] args) {
System.out.println("littlePenguin:");
littlePenguin penguin_1 = new littlePenguin();
penguin_1.eating();
penguin_1.sleeping();
penguin_1.beating();
// 下同,省略...
}
}
复制代码
public abstract class penguin {
public void eating() {
System.out.println("吃饭");
}
public void sleeping() {
System.out.println("睡觉");
}
public abstract void beating();
public void everyDay() {
this.eating();
this.sleeping();
this.beating();
}
}
复制代码
每只企鹅单独实现自己“打豆豆”的方式:
public class littlePenguin extends penguin {
@Override
public void beating() {
System.out.println("用小翅膀打豆豆");
}
}
public class middlePenguin extends penguin {
@Override
public void beating() {
System.out.println("用圆圆的肚子打豆豆");
}
}
public class bigPenguin extends penguin {
@Override
public void beating() {
System.out.println("拿鸡毛掸子打豆豆");
}
}
复制代码
最后看调用方式:
public class test {
public static void main(String[] args) {
System.out.println("littlePenguin:");
littlePenguin penguin1 = new littlePenguin();
penguin1.everyDay();
System.out.println("middlePenguin:");
middlePenguin penguin2 = new middlePenguin();
penguin2.everyDay();
System.out.println("bigPenguin:");
bigPenguin penguin3 = new bigPenguin();
penguin3.everyDay();
}
}
复制代码
public abstract class penguin {
public void eating() {
System.out.println("吃饭");
}
public void sleeping() {
System.out.println("睡觉");
}
public abstract void beating();
}
复制代码
每只企鹅单独实现自己“打豆豆”的方式:
public class littlePenguin extends penguin {
@Override
public void beating() {
System.out.println("用小翅膀打豆豆");
}
}
public class middlePenguin extends penguin {
@Override
public void beating() {
System.out.println("用圆圆的肚子打豆豆");
}
}
public class bigPenguin extends penguin {
@Override
public void beating() {
System.out.println("拿鸡毛掸子打豆豆");
}
}
复制代码