//People类 package 继承; public class People { private String name; private double height; private double weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public void speakHello() { System.out.println("Hello"); } }
//Chinapeople类 package 继承; //继承 //Object 是所有类的父类 public class Chinapeople extends People { public void Chinesekongfu() { System.out.println("坐如钟,站如松"); } //重写 也叫覆盖 public void speakHello() { System.out.println("你好"); } }
//Americanpeople类 package 继承; public class Americanpeople extends People { public void americanBoxing() { System.out.println("直拳,勾拳"); } }
//Testpeople类 package 继承; public class Textpeople { public static void main(String[] args) { People p=new People(); p.speakHello(); System.out.println("中国人:"); Chinapeople cp=new Chinapeople(); cp.speakHello(); cp.Chinesekongfu(); System.out.println("美国人:"); Americanpeople ap=new Americanpeople(); ap.speakHello(); ap.americanBoxing(); } }
运行结果