继承

 

 1 package aaa;
 2 
 3 class Instrument {
 4     public void play () {
 5         System.out.println("弹奏乐器:");
 6     }
 7 }
 8 
 9 class Wind extends Instrument {
10     public void play () {
11         System.out.println("弹奏Wind");
12     }
13     public void play2 (){
14         System.out.println("调用wind的play2");
15     }
16 
17 }
18 
19 class Brass extends Instrument {
20     public void play () {
21         System.out.println("弹奏Brass");
22     }
23     public void play2 () {
24         System.out.println("调用brass的play2");
25     }
26 
27 }
28 
29 
30 
31 public class Music {
32     public static void tune (Instrument i) {
33         i.play();
34     }
35 
36     public static void main(String[] args) {
37         Music mm = new Music();
38         Instrument dd = new Instrument();
39         Wind w = new Wind();
40         Brass xx = new Brass();
41 mm.tune(xx);
42 mm.tune(w);
43 } 44 }

 

 

2.编写一个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 package aaa;
 2 
 3 class Monkey {
4 5 public void speak(){ 6 System.out.println("咿咿呀呀"); 7 } 8 9 } 10 11 12 13 class People extends Monkey{ 14 15 public void speak(){ 16 17 System.out.println("小样的,不错嘛!会说话了!"); 18 } 19 void think(){ 20 System.out.println("别说话,认真思考!"); 21 } 22 } 23 24 25 public class E{ 26 27 public static void main(String[] args){ 28 29 Monkey m = new Monkey(); 30 People a = new People(); 31 People b = new People(); 32 m.speak(); 33 a.speak(); 34 b.think(); 35 } 36

 

3.、按要求编写一个Java应用程序:

(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。

(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。

(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

 1 package aaa;
 2 import java.util.Scanner;
 3 class area{
 4     
 5     public void Arithtic(){
 6         int a,b,c;
 7         Scanner S = new Scanner(System.in);
 8         System.out.println("请分别输入长和宽:");
 9         a = S.nextInt();
10         b = S.nextInt();
11         System.out.println("计算的面积为:"+a*b);
12     }
13 }
14 class bulk extends area{
15     
16     public void Arithtic(){
17         int a,b,c;
18         Scanner V = new Scanner(System.in);
19         System.out.println("请分别输入长、宽和高");
20         a = V.nextInt();
21         b = V.nextInt();
22         c = V.nextInt();
23         System.out.println("计算的体积为:"+a*b*c);
24     }
25 }
26 public class Father {
27     
28     public static void main(String[] args){
29         
30         area a = new area();
31         bulk b = new bulk();
32         a.Arithtic();
33         b.Arithtic();
34     }
35 
36 }

 

posted @ 2019-05-07 11:19  你若是天  阅读(198)  评论(1编辑  收藏  举报