创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
package com.hanqi; public class People { private double height; private double weight; 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!"); } }
package com.hanqi; //extends 继承 //Object 是所有类的父类】 public class ChinaPeople extends People { public void gongfu() { System.out.println("站如松"); } public void speakHello() { System.out.println("你好"); } }
package com.hanqi; public class AmericanPeople extends People { //输出拳击的信息 public void americanBoxing() { System.out.println("直拳,勾拳"); } }
package com.hanqi; public class Testpeople { public static void main(String[] args) { // TODO 自动生成的方法存根 People p=new People(); p.speakHello(); ChinaPeople cp=new ChinaPeople(); cp.setWeight(100); cp.setHeight(100); System.out.println(cp.getHeight()); cp.speakHello(); cp.gongfu(); AmericanPeople a=new AmericanPeople(); a.americanBoxing(); } }
时间: 2024-10-15 15:05:02