package com.oop.demo05; public class Monkey { private String name; private boolean sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public Monkey() { super(); } public Monkey(String name, boolean sex) { this.name = name; this.sex = sex; } public void speak(){ System.out.println(name+"咿咿呀呀!");
package com.oop.demo05; public class People extends Monkey{ public void speak(){ System.out.println(this.getName()+"说了一句话!"); } public void play(){ System.out.println(this.getName()+"正在打篮球!"); } } package com.oop.demo05; public class Test02 { public static void main(String[] args) { Monkey m1 = new Monkey(); m1.setName("金丝猴"); m1.speak(); //People p2 = (People) m1; if (m1 instanceof People){ People p2 = (People)m1; p2.play(); } System.out.println("====================="); People p1 = new People(); p1.setName("秦汉"); p1.play(); p1.speak(); } }
} }