定义类Human,具有若干属性和功能;定义其子类Man、Woman; 在主类Test中分别创建子类、父类和上转型对象,并测试其特性。
package com.hanqi.test; public class Humen { private String speak; private String read; private String listen; public String getSpeak() { return speak; } public void setSpeak(String speak) { this.speak = speak; } public String getRead() { return read; } public void setRead(String read) { this.read = read; } public String getListen() { return listen; } public void setListen(String listen) { this.listen = listen; } public void run() { System.out.println("我会跑"); } public void jump() { System.out.println("我会跳"); } }
package com.hanqi.test; public class Men extends Humen { }
package com.hanqi.test; public class Women extends Humen { }
package com.hanqi.test; public class Testmen { public static void main(String[] args) { Humen h=new Men(); Men m=(Men)h; m.run(); Women w=new Women(); w.jump(); } }