1 public class instrument 2 { 3 public void play() 4 { 5 System.out.println("弹奏乐器"); 6 } 7 }
1 public class wind extends instrument 2 { 3 public void play() 4 { 5 System.out.println("弹奏wind"); 6 } 7 public void play2() 8 { 9 System.out.println("调用wind的play2"); 10 } 11 }
1 public class brass extends instrument 2 { 3 public void play() 4 { 5 System.out.println("弹奏brass"); 6 } 7 public void play2() 8 { 9 System.out.println("调用brass的play2"); 10 } 11 }
1 public class music { 2 public static void tune(instrument i) 3 { 4 wind w = new wind(); 5 w.play(); 6 brass b = new brass(); 7 b.play(); 8 } 9 public static void main(String[] args) 10 { 11 music.tune(new wind()); 12 music.tune(new brass()); 13 } 14 }
19.创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
1 public class people 2 { 3 protected double height; 4 protected double weight; 5 6 public void speakhello() 7 { 8 System.out.println("hello"); 9 } 10 public void averageheight() 11 { 12 System.out.println("height"); 13 } 14 public void averageweight() 15 { 16 System.out.println("weight"); 17 } 18 }
1 public class chinapeople extends people 2 { 3 public void speakhello() 4 { 5 System.out.println("hello"); 6 } 7 public void averageheight() 8 { 9 System.out.println("height"); 10 } 11 public void averageweight() 12 { 13 System.out.println("weight"); 14 } 15 16 public void chinagongfu() 17 { 18 System.out.println("中国功夫"); 19 } 20 }
1 public class americanpeople extends people 2 { 3 public void speakhello() 4 { 5 System.out.println("hello"); 6 } 7 public void averageheight() 8 { 9 System.out.println("height"); 10 } 11 public void averageweight() 12 { 13 System.out.println("weight"); 14 } 15 16 public void americanboxing() 17 { 18 System.out.println("其他功夫"); 19 } 20 }
21.编写一个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个类的功
能。
1 public class monkey 2 { 3 void monkey(String s) 4 { 5 6 } 7 8 public void speak() 9 { 10 System.out.println("咿咿呀呀......"); 11 } 12 }
1 public class people extends monkey 2 { 3 public void speak() 4 { 5 System.out.println("小样的,不错嘛!会说话了!"); 6 } 7 void think() 8 { 9 System.out.println("别说话!认真思考!"); 10 } 11 }
1 public class E 2 { 3 public static void main(String[] args) 4 { 5 monkey m = new monkey(); 6 m.monkey("haha"); 7 m.speak(); 8 9 people p = new people(); 10 p.speak(); 11 p.think(); 12 } 13 }
26.定义类Human,具有若干属性和功能;定义其子类Man、Woman;
在主类Test中分别创建子类、父类和上转型对象,并测试其特性。
27.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish
和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
1 public class animal 2 { 3 public String zhonglei; 4 5 public String getZhonglei() { 6 return zhonglei; 7 } 8 public void setZhonglei(String zhonglei) { 9 this.zhonglei = zhonglei; 10 } 11 12 public void chi() 13 { 14 System.out.println(getZhonglei()+"chi"); 15 } 16 public void shui() 17 { 18 System.out.println(getZhonglei()+"shui"); 19 } 20 }
public class fish extends animal { }
public class dog extends animal
{
}
1 public class E 2 { 3 public static void main(String[] args) 4 { 5 fish f = new fish(); 6 f.setZhonglei("鱼"); 7 f.chi(); 8 f.shui(); 9 10 dog d = new dog(); 11 d.setZhonglei("狗"); 12 d.chi(); 13 d.shui(); 14 } 15 }
28.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,
和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、
宽、高,输出其底面积和体积。
1 public class juxing 2 { 3 private double chang; 4 private double kuan; 5 6 public double getChang() { 7 return chang; 8 } 9 public void setChang(double chang) { 10 this.chang = chang; 11 } 12 public double getKuan() { 13 return kuan; 14 } 15 public void setKuan(double kuan) { 16 this.kuan = kuan; 17 } 18 19 public double mianji() 20 { 21 return (getChang()*getKuan()); 22 } 23 }
1 public class changfangti extends juxing 2 { 3 private double gao; 4 5 public double getGao() { 6 return gao; 7 } 8 public void setGao(double gao) { 9 this.gao = gao; 10 } 11 12 public double tiji() 13 { 14 return(super.mianji()*this.getGao()); 15 } 16 17 }
1 public class 测试类 { 2 3 public static void main(String[] args) 4 { 5 juxing a1 = new juxing(); 6 changfangti a2 = new changfangti(); 7 8 a1.setChang(20); 9 a1.setKuan(10); 10 System.out.print("面积:"); 11 System.out.println(a1.mianji()); 12 13 14 a2.setGao(15); 15 System.out.print("体积:"); 16 System.out.println(a2.tiji()); 17 } 18 }