java继承相关
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java继承相关相关的知识,希望对你有一定的参考价值。
实现如下类之间的继承关系,并编写Music类来测试这些类。
package eryi; public class Instrument { public void play() { System.out.println("弹奏乐器"); } }
wind:
package eryi; public class Wind extends Instrument { public void play() { System.out.println("弹奏wind"); } public void play2() { System.out.println("调用wind的play2"); } }
brass:
package eryi; public class Brass extends Instrument { public void play() { System.out.println("弹奏brass"); } public void play2() { System.out.println("调用brass的跑play2"); } }
package eryi; public class MUSIC { public static void tune(Instrument i) { i.play(); } public static void main(String args[]) { Wind w = new Wind(); Brass b = new Brass(); w.play(); w.play2(); b.play(); b.play2(); } }
编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类
E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()
方法,在speak方法中输出“咿咿呀呀......”的信息。
package eryi; public class Monkey { Monkey(String s) { } public void speak() { System.out.println("咿咿呀呀..."); } }
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法
中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”
的信息。
package eryi; public class People1 extends Monkey { People1(String s) { super(s); } public void speak() { System.out.println("小样的,不错嘛!会说话了!"); } public void think() { System.out.println("别说话!认真思考!"); } }
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功
能。
package eryi; public class E { public static void main(String[] args) { // TODO 自动生成的方法存根 Monkey m=new Monkey("猴子"); m.speak(); People1 p=new People1("人类"); p.speak(); p.think(); } }
创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
People:
package eryi; public class People { protected double height; protected double weight; private String person; public void speakHello() { System.out.println(); } public void averageHeight() { System.out.println(); } public void averageWight() { System.out.println(); } 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 String getPerson() { return person; } public void setPerson(String person) { this.person = person; } }
ChinaPeople:
package eryi; public class ChinaPeople extends People{ public void speakHello() { System.out.println("你好"); } public void averageHeight() { System.out.println("平均身高:175cm"); } public void averageWight() { System.out.println("平均体重:60kg"); } public void chinaGongFu() { System.out.println("我可以坐如钟,站如松,睡如弓"); } }
AmericanPeople:
package eryi; public class AmericanPeople extends People { public void speakHello() { System.out.println("Hello"); } public void averageHeight() { System.out.println("平均身高:178cm"); } public void averageWight() { System.out.println("平均体重:65kg"); } public void americanBoxing() { System.out.println("我会直拳"); } }
测试:
package eryi; public class Person { public static void main(String[] args) { // TODO 自动生成的方法存根 ChinaPeople cp=new ChinaPeople(); cp.setPerson("中国人"); System.out.println(cp.getPerson()); cp.speakHello(); cp.averageHeight(); cp.averageWight(); cp.chinaGongFu(); AmericanPeople ap=new AmericanPeople(); ap.setPerson("美国人"); System.out.println(ap.getPerson()); ap.speakHello(); ap.averageHeight(); ap.averageWight(); ap.americanBoxing(); } }
以上是关于java继承相关的主要内容,如果未能解决你的问题,请参考以下文章