Java 继承——2
创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
package text1; public class People { protected double height; protected double weight; public void speak() { System.out.println("hello"); } public void averageHeight() { System.out.println("身高"); } public void averageWeight() { System.out.println("体重"); } }
package text1; public class Chinapeople extends People { public void speak() { System.out.println("你好"); } public void averageHeight() { System.out.println("中国身高"); } public void averageWeight() { System.out.println("中国体重"); } public void chinagongfu() { System.out.println("站如松"); } }
package text1; public class Americanpeople extends People { public void speak() { System.out.println("Hello!!!!!!"); } public void averageHeight() { System.out.println("height"); } public void averageWeight() { System.out.println("weight"); } public void americanboxing() { System.out.println("拳击"); } }
package text1; public class text12 { public static void main(String[] args) { // TODO 自动生成的方法存根 People p=new People(); p.speak(); p.averageHeight(); p.averageWeight(); Chinapeople c=new Chinapeople(); c.speak(); c.averageHeight(); c.averageWeight(); c.chinagongfu(); Americanpeople a=new Americanpeople(); a.speak(); a.averageHeight(); a.averageWeight(); a.americanboxing(); } }