27.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish
和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
package zuoye5; public class Animal { private String zhonglei; private String qixide; public String getZhonglei() { return zhonglei; } public void setZhonglei(String zhonglei) { this.zhonglei = zhonglei; } public String getQixide() { return qixide; } public void setQixide(String qixide) { this.qixide = qixide; } public Animal(String zhonglei) { super(); this.zhonglei = zhonglei; } public Animal() { super(); } public void chi() { System.out.println("我饿了,我要吃东西"); } public void shui() { System.out.println("我要休息"); } }
package zuoye5; public class Fish extends Animal { public void chi() { System.out.println("我饿了,我要吃小鱼"); } public void shui() { System.out.println("我在水里,睡觉也是动的"); } }
package zuoye5; public class Fish extends Animal { public void chi() { System.out.println("我饿了,我要吃小鱼"); } public void shui() { System.out.println("我在水里,睡觉也是动的"); } }
package zuoye5; public class Dog extends Animal { private int tui; public int getTui() { return tui; } public void setTui(int tui) { this.tui = tui; } public void chi() { System.out.println("我饿了,我要吃骨头"); } public void shui() { System.out.println("我是主人的保护者,起得早,睡的晚"); } }
package zuoye5; public class E { public static void main(String[] args) { Animal a=new Animal("<。)#)))≦ "); Fish f=new Fish(); f.setQixide("海洋"); System.out.println("种类是:"+a.getZhonglei()+"生活在:"+f.getQixide()); f.chi(); f.shui(); Dog d=new Dog(); d.setZhonglei("犬类 "); d.setQixide("陆地 "); d.setTui(4); System.out.println("种类是: "+d.getZhonglei()+"生活在:"+d.getQixide()+" 我有"+d.getTui()+"条腿"); d.chi(); d.shui(); } }
28.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,
和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、
宽、高,输出其底面积和体积。
package zuoye6; public class Rect { public double width; public double chang; //有参的构造方法 public Rect(double width,double chang) { this.width=width; this.chang=chang; } //无参的 public Rect() { } //自定义成员方法 面积 public double area(){ double area; area=width*chang; return area; } }
package zuoye6; public class Cangfangti extends Rect { public double height; public Cangfangti(double width,double chang,double height) { this.width=width; this.chang=chang; this.height=height; } //自定义成员方法 体积 public double tiji(){ double tiji; tiji=width*chang*height; return tiji; } public double diarea(){ double diarea; diarea=width*chang; return diarea; } }
package zuoye6; public class Ceshi { public static void main(String[] args) { Rect r=new Rect(10,20); System.out.println("矩形的面积="+r.area()); Cangfangti c=new Cangfangti(10,20,30); System.out.println("长方体的体积="+c.tiji()); System.out.println("长方体的底面积="+c.diarea()); } }
29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数
wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数
loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个
类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功
能。
package zuoye7; 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; } public Vehicle(int wheels, double weight) { super(); this.wheels = wheels; this.weight = weight; } public Vehicle() { super(); } }
package zuoye7; public class Car extends Vehicle { private double loader;//载人数 public double getLoader() { return loader; } public void setLoader(double loader) { this.loader = loader; } }
package zuoye7; public class Truck extends Vehicle { private double payload;//载重量 public double getPayload() { return payload; } public void setPayload(double payload) { this.payload = payload; } }
package zuoye7; public class Ceshi_vehicle { public static void main(String[] args) { Car c=new Car(); c.setLoader(5); c.setWeight(4.5); c.setWheels(4); System.out.println("小汽车的载人数是:"+c.getLoader()+"人 车的重量="+c.getWeight()+"车轮个数="+c.getWheels()); Truck t=new Truck(); t.setPayload(5); t.setWeight(5); t.setWheels(6); System.out.println("卡车的载重量="+"吨"+"车的重量="+t.getWeight()+"车轮数="+t.getWheels()); } }