java第四次上机(继承)

 

代码

1 package person;
2 
3 public class Instrument {
4     
5     public void play() {
6         System.out.println("弹奏乐器");
7     }
8     
9 }
 1 package person;
 2 
 3 class Wind extends Instrument{
 4     
 5     public void play() {
 6         System.out.println("弹奏Wind");
 7     }
 8     public void play2() {
 9         System.out.println("调用wind的play2");
10     }
11 
12 }
 1 package person;
 2 
 3 class Brass extends Instrument{
 4     
 5     public void play() {
 6         System.out.println("弹奏brass");
 7     }
 8     public void play2() {
 9         System.out.println("调用brass的play2");
10     }
11 
12 }
 1 package person;
 2 
 3 public class Music {
 4     public static void tune(Instrument i){
 5         i.play();
 6 }
 7     public static void main(String[] args) {
 8         Music m = new Music();
 9         Wind w = new Wind();
10         Brass b = new Brass();
11         
12         m.tune(w);
13         m.tune(b);
14     }
15 
16 }

运行界面

 

 

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 person;
 2 
 3 public class Monkey {
 4     public Monkey(String s){
 5         
 6     }
 7     public void speak(){
 8         System.out.println("咿咿呀呀.....。");
 9     }
10 
11 }
 1 package person;
 2 
 3 class People extends Monkey{
 4     public People(String s) {
 5         super(s);
 6     }
 7     public void speak(){
 8         System.out.println("小样的,不错嘛!会说话了!");
 9     }
10     void think(){
11         System.out.println("别说话!认真思考!");
12     }
13     
14 }
 1 package person;
 2 
 3 public class E {
 4 
 5     public static void main(String[] args) {
 6         Monkey m = new Monkey("m");
 7         People p = new People("p");
 8         
 9         m.speak();
10         p.speak();
11         p.think();
12         
13     }
14 }

运行界面

 

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

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

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

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

代码

 1 package Person;
 2 import java.util.*;
 3 
 4 class Rectangle {
 5     double length;
 6     double width;
 7     
 8     public void area(double length,double width){        //计算面积
 9         double s;
10         s=length*width;
11         System.out.println("长方体的底面积为:"+s);
12     }
13 }
14 
15 class Rectangle2 extends Rectangle{
16     public Rectangle2() {
17         super();
18     }
19     
20     double length;
21     double width;
22     double height;
23     
24     public void volume(double length,double width,double height){        //计算体积
25         double v;
26         v=length*width*height;
27         System.out.println("长方体的体积为:"+v);
28     }
29 }
30 
31 public class TextRectangle {
32     
33     public static void main(String[] args) {
34          Rectangle r = new Rectangle();
35          Rectangle2 r2 = new Rectangle2();
36          Scanner sc = new Scanner(System.in);
37 
38          System.out.println("请依次输入长方体的长、宽、高:");
39          double length = sc.nextDouble();
40          double width = sc.nextDouble();
41          double height = sc.nextDouble();
42             
43          r.area(length,width);
44          r2.volume(length,width,height);
45      }
46 }

运行界面

 

 

4.设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。

小车类Car是Vehicle的子类,其中包含的属性有载人数loader。

卡车类Truck是Car类的子类,其中包含的属性有载重量payload。

每个类都有构造方法和输出相关数据的方法。

最后,写一个测试类来测试这些类的功能。

代码

 1 //设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。
 2 //小车类Car是Vehicle的子类,其中包含的属性有载人数loader。
 3 //卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
 4 //每个类都有构造方法和输出相关数据的方法。
 5 //最后,写一个测试类来测试这些类的功能。
 6 package Person;
 7 import java.util.*;
 8 
 9 class Vehicle{
10     int wheels;
11     double weight;
12     
13     public void print(){
14         System.out.println("请依次输入车轮的个数、车重为:");
15         input();
16         System.out.println("车轮的个数为:"+wheels+"   车重为:"+weight);
17     }
18     public void input(){
19          Scanner sc = new Scanner(System.in);
20          wheels=sc.nextInt();
21          weight=sc.nextDouble();
22          }
23 }
24 
25 class Car extends Vehicle{
26     int loader;
27     
28     public void print(){
29         System.out.println("请输入载人数为:");
30         input();
31         System.out.println("载人数为:"+loader);
32     }
33     public void input(){
34          Scanner sc = new Scanner(System.in);
35          loader=sc.nextInt();
36          }
37 }
38 
39 class Truck extends Car{
40     double payload;
41     
42     public void print(){
43         System.out.println("请输入有载重量为:");
44         input();
45         System.out.println("有载重量为:"+payload);
46 }
47     public void input(){
48         Scanner sc = new Scanner(System.in);
49         payload=sc.nextInt();
50         }
51 }
52 
53 public class TestCar {
54     public static void main(String[] args) {
55         Vehicle v = new Vehicle();
56         Car c = new Car();
57         Truck t = new Truck();
58         
59         v.print();
60         c.print();
61         t.print();
62     }
63 }

运行界面

 


今日小结:因为上继承那一课的时候根本没听懂 也不知道是做什么用的 所以本次相当于预习+复习 都像是新知识 写起来很困难 尤其是现在仍然有一些知识不大清楚 虽然能够运行但也不知道自己写的程序是否正确

 

posted @ 2019-05-05 23:11  Unicodee  阅读(181)  评论(0编辑  收藏  举报