java继承8个题
1、实现如下类之间的继承关系,并编写Music类来测试这些类。
public class Instrument { public void play(){ System.out.println("弹奏乐器!"); } }
public class Wind extends Instrument{ //复写play方法 public void play(){ System.out.println("弹奏Wind!"); } public void play2(){ System.out.println("调用wind的play2"); } }
public class Brass extends Instrument { //复写play方法 public void play(){ System.out.println("弹奏brass!"); } public void play2(){ System.out.println("调用brass的play2!"); } }
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(); tune(w); tune(b); } }
2、创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople和AmericanPeople类重写父类的三个方法)。
public class People { protected double height; protected 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("你好!!!"); } public void averageHeight(){ System.out.println("平均身高是:"+height); } public void averageWeight(){ System.out.println("平均体重是:"+weight); } }
public class ChinaPeople extends People { public void speakHello(){ System.out.println("你好,我是中国人!"); } public void averageHeight(){ System.out.println("中国人的平均身高是:"+height); } public void averageWeight(){ System.out.println("中国人的平均体重是:"+weight); } public void chinaGongfu(){ System.out.println("我会中国功夫:坐如钟,站如松,睡如弓!"); } }
public class AmericanPeople extends People { public void speakHello(){ System.out.println("你好,我是美国人!"); } public void averageHeight(){ System.out.println("美国人的平均身高是:"+height); } public void averageWeight(){ System.out.println("美国人的平均体重是:"+weight); } public void amercianBoxing(){ System.out.println("我会美式拳击:直拳,上勾拳!"); } }
public class TestPeople { public static void main(String[] args) { ChinaPeople chp=new ChinaPeople(); chp.height=180; chp.weight=130; chp.speakHello(); chp.averageHeight(); chp.averageWeight(); chp.chinaGongfu(); AmericanPeople amp=new AmericanPeople(); amp.setHeight(175); amp.setWeight(150); amp.speakHello(); amp.averageHeight(); amp.averageWeight(); amp.amercianBoxing(); } }
3、编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。
public class Monkey { private String name; //声明属性name //getter、setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } //构造方法 Monkey(String s){ this.name=s; } //成员方法 public void speak(){ System.out.println("咿咿呀呀........."); } }
public class People00 extends Monkey { People00(String s) { super(s); } //重写speak()方法 public void speak(){ System.out.println("小样的,不错嘛!会说话了!"); } void think(){ System.out.println("别说话!认真思考!"); } }
public class E { public static void main(String[] args) { Monkey m=new Monkey("Toney"); System.out.println("我是猴子"+m.getName()); m.speak(); People00 p=new People00("Tom"); System.out.println("我是人类"+p.getName()); p.speak(); p.think(); } }
4、定义类Human,具有若干属性和功能;定义其子类Man、Woman;在主类Test中分别创建子类、父类和上转型对象,并测试其特性。
public class Human { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void speak(){ System.out.println("I am a human."); } public void run(){ System.out.println("I can run."); } public void getInfo(){ System.out.println("我的名字是:"+getName()+" 我的年龄是:"+getAge()+" 我的性别是:"+getSex()); Man m=new Man(); } }
public class Man extends Human{ public void man(){ if(getSex()=="男人"){ System.out.println("I am a man."); } else{ System.out.println("I am not a man."); } } public void run(){ System.out.println("我是雄狮一样强壮的男人."); } }
public class Woman extends Human { public void woman(){ if(getSex()=="女"){ System.out.println("I am a woman."); } else{ System.out.println("I am not a woman."); } } }
public class TestHuman { public static void main(String[] agrs){ Man m=new Man(); m.setName("Dave"); m.setAge(28); m.setSex("男"); m.getInfo(); m.speak(); m.run(); m.man(); //隐式转换 //上转型对象 Human h=new Woman(); h.setName("Jude"); h.setAge(58); h.setSex("女"); h.speak(); h.run(); h.getInfo(); if(h instanceof Woman){ Woman w=(Woman)h; w.woman(); } else{ System.out.println("类型不符!"); } } }
5、编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
public class Animal { private String zhonglei; private String name; public String getZhonglei() { return zhonglei; } public void setZhonglei(String zhonglei) { this.zhonglei = zhonglei; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void eat(){ System.out.println("我需要觅食!"); } public void sleep(){ System.out.println("我需要睡觉!"); } public void getInfo(){ System.out.println("我的名字是:"+getName()+" 我的种类是:"+getZhonglei()); } }
public class Fish extends Animal { public void speak(){ System.out.println("我是"+getZhonglei()); } public void eat(){ System.out.println("我吃鱼食!"); } }
public class Dog extends Animal{ public void speak(){ System.out.println("我是"+getZhonglei()); } public void eat(){ System.out.println("我吃狗粮!"); } }
public class TestAnimal { public static void main(String[] args){ Animal a=new Fish(); a.setName("Tom"); a.setZhonglei("动物"); a.getInfo(); a.eat(); a.sleep(); if(a instanceof Fish){ Fish f=(Fish)a; f.setZhonglei("鱼类"); f.eat(); f.getInfo(); } else{ System.out.println("类型不符!"); } Dog d = new Dog(); d.setName("哮天犬"); d.setZhonglei("哺乳动物"); d.getInfo(); Animal aa=(Dog)d; aa.eat(); aa.sleep(); } }
6、按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
public class Juxing { private double chang; private double kuan; public double getChang() { return chang; } public void setChang(double chang) { this.chang = chang; } public double getKuan() { return kuan; } public void setKuan(double kuan) { this.kuan = kuan; } public double area(){ return chang*kuan; } }
public class ChangFangTi extends Juxing { private double gao; public double getGao() { return gao; } public void setGao(double gao) { this.gao = gao; } public double tiji(){ return area()*gao; } }
public class Test { public static void main(String[] args){ ChangFangTi a=new ChangFangTi(); a.setChang(12); a.setKuan(10); a.setGao(5); System.out.println("长方体的底面积是:"+a.area()); System.out.println("长方体的体积是:"+a.tiji()); } }
7、编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。
public class Vehicle { private int wheels; private double weight; public int getWheels() { return wheels; } public void setWheels(int wheels) { this.wheels = wheels; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } Vehicle(){ } Vehicle(int wheels,double weight){ this.wheels=wheels; this.weight=weight; } public void getInfo(){ System.out.println("车轮的个数是:"+wheels+" 车重:"+weight); } }
public class Car extends Vehicle { private int loader; public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } Car(){ } Car(int wheels,double weight,int loader){ super(wheels,weight); this.loader=loader; } public void loader(){ if(loader<=6){ System.out.println("这辆车能载"+loader+"人"); } else{ System.out.println("超员了"); } } }
public class Truck extends Car { Truck(){ super(); } private double payload; public double getPayload() { return payload; } public void setPayload(double payload) { this.payload = payload; } public void payload(){ if(payload<=1000){ System.out.println("这辆车的载重是"+payload); } else{ System.out.println("超重了"); } } }
public class Testcar { public static void main(String[] args){ Car c=new Car(4,600,4); c.getInfo(); c.loader(); Truck t =new Truck(); t.setLoader(3); t.setPayload(800); t.setWeight(1000); t.setWheels(6); t.getInfo(); t.loader(); t.payload(); } }
8、编写一个Shape类,具有属性:周长和面积;定义其子类三角形和矩形,分别具有求周长的方法。定义主类E,在其main方法中创建三角形和矩形类的对象,并赋给Shape类的对象a、b,使用对象a、b来测试其特性。
public class Shape { private int bian; private double zhouchang; private double mianji; public int getBian() { return bian; } public void setBian(int bian) { this.bian = bian; } public double getZhouchang() { return zhouchang; } public void setZhouchang(double zhouchang) { this.zhouchang = zhouchang; } public double getMianji() { return mianji; } public void setMianji(double mianji) { this.mianji = mianji; } public void bian(){ System.out.println("您创建了一个"+bian+"边形!"); } }
public class San extends Shape{ double[] bianchang=new double[3]; San(){ } San(double a,double b, double c){ bianchang[0]=a; bianchang[1]=b; bianchang[2]=c; } public void zhouchang(){ if(getBian()==3&&bianchang[0]+bianchang[1]>bianchang[2]&&bianchang[0]-bianchang[1]<bianchang[2]){ double sum=0; for(int i=0;i<bianchang.length;i++){ sum+=bianchang[i]; System.out.print(" 边长"+(i+1)+"是"+ bianchang[i]); } System.out.println(" 三角形的周长是:"+sum); } else{ System.out.println("这不是一个三角形"); } } }
public class Juxx extends Shape { double[] bianchang=new double[4]; Juxx(){} Juxx(double a,double b,double c,double d){ bianchang[0]=a; bianchang[1]=b; bianchang[2]=c; bianchang[3]=d; } public void zhouchang(){ if(getBian()==4 && bianchang[0]==bianchang[2]&&bianchang[1]==bianchang[3]){ double sum=0; for(int i=0;i<bianchang.length;i++){ sum+=bianchang[i]; System.out.print(" 边长"+(i+1)+"是"+bianchang[i]); } System.out.println(" 矩形的周长是:"+sum); } else{ System.out.println("这不是一个矩形"); } } }
public class TestE { public static void main(String[] args){ Shape s=new Shape(); s.setBian(5); s.bian(); San sa=new San(3,4,5); sa.setBian(3); sa.zhouchang(); Juxx ju=new Juxx(3,4,3,4); ju.setBian(4); ju.zhouchang(); } }