java 继承练习题2
Posted 风起,唯有努力生存
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 继承练习题2相关的知识,希望对你有一定的参考价值。
创建如下三个类:(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(); } }
以上是关于java 继承练习题2的主要内容,如果未能解决你的问题,请参考以下文章