1 public class People { 2 3 private double height;//身高 4 private double weight;//体重 5 6 public double getHeight() { 7 return height; 8 } 9 10 public void setHeight(double height) { 11 this.height = height; 12 } 13 14 public double getWeight() { 15 return weight; 16 } 17 18 public void setWeight(double weight) { 19 this.weight = weight; 20 } 21 22 23 //方法打招呼 24 public void speakHello(){ 25 System.out.println("hello !"); 26 }
1 //extends 继承 2 public class ChinaPeople extends People { 3 4 public void ChinaKongFu(){ 5 System.out.println("坐如钟,站如松。"); 6 } 7 //重写 覆盖 8 public void speakHello(){ 9 System.out.println("你好!"); 10 } 11 }
1 public class AmericanPeople extends People { 2 3 public void americanBoxing(){ 4 System.out.println("直拳,左勾拳"); 5 } 6 }
1 public class Text_People { 2 3 public static void main(String[] args) { 4 People p1 = new People(); 5 p1.speakHello(); 6 7 ChinaPeople p2 = new ChinaPeople(); 8 p2.speakHello(); 9 p2.ChinaKongFu(); 10 11 12 13 } 14 }
运行:
时间: 2024-10-24 17:06:09