第三次实训作业

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

运行结果如下:

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

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

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

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

程序如下:

 

 1 package JavaPractice;
 2 import java.util.Scanner;
 3 class Rectangle
 4 {
 5     private double length; //定义长
 6     private double width;  //定义宽
 7     public double getLength()
 8     {
 9         return length;
10     }
11     public double getWidth() 
12     {
13         return width;
14     }
15     public Rectangle(double length, double width)
16     {
17         
18         this.length = length;
19         this.width = width;
20     }
21     public double getArea() //计算面积
22     {
23         return length*width;
24     }
25 }
26 class Cuboid extends Rectangle
27 {
28     private double height; //定义高
29     public double getHeight() 
30     {
31         return height;
32     }
33     public Cuboid(double length, double width, double height) 
34     {
35         super(length, width);
36         this.height = height;
37     }
38     public double getVolume() //计算体积
39     {
40         return getLength()*getWidth()*height;
41     }
42 }
43 public class TestPattern
44 {
45     public static void main(String[] args) 
46     {
47         Scanner sc=new Scanner(System.in);
48         
49         System.out.println("请分别输入长方形的长、宽:"); //测试长方形
50         Rectangle rc=new Rectangle(sc.nextDouble(),sc.nextDouble());
51         System.out.println("长方形的面积为:"+rc.getArea());
52         System.out.println();
53         
54         System.out.println("请分别输入长方体的长、宽、高:"); //测试长方体
55         Cuboid cb=new Cuboid(sc.nextDouble(),sc.nextDouble(),sc.nextDouble());
56         System.out.println("长方体的底面积为:"+cb.getArea());
57         System.out.println("长方体的体积为:"+cb.getVolume()); 
58     }
59 }

 

运行结果如下:

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

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

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

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

(4).每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。

 程序如下:

 1 package JavaPractice;
 2 class Vehicle
 3 {
 4     private int wheels; //定义车轮个数
 5     private double weight; //定义车重
 6     public int getWheels() 
 7     {
 8         return wheels;
 9     }
10     public double getWeight()
11     {
12         return weight;
13     }
14     public Vehicle(int wheels, double weight) 
15     {
16         this.wheels = wheels;
17         this.weight = weight;
18     }
19     public void printInformation()
20     {
21         System.out.println("Vehicle的信息如下:");
22         System.out.println("车轮数:"+wheels
23                 +"    车重:"+weight);
24         System.out.println();
25     }
26 }
27 class Car extends Vehicle
28 {
29     private int loader;
30     public int getLoader() 
31     {
32         return loader;
33     }
34     public Car(int wheels, double weight, int loader) 
35     {
36         super(wheels, weight);
37         this.loader = loader;
38     }
39     public void printInformation()
40     {
41         System.out.println("Car的信息如下:");
42         System.out.println("车轮数:"+getWheels()
43                 +"    车重:"+getWeight()+"\n有载人数:"+loader);
44         System.out.println();
45     }
46 }
47 class Truck extends Car
48 {
49     private double payload;
50     public double getPayload()
51     {
52         return payload;
53     }
54     public Truck(int wheels, double weight, int loader, double payload) 
55     {
56         super(wheels, weight, loader);
57         this.payload = payload;
58     }
59     public void Author()
60     {
61         System.out.println("杜文泽");
62     }
63     public void printInformation()
64     {
65         System.out.println("Truck的信息如下:");
66         System.out.println("车轮数:"+getWheels()+"    车重:"+getWeight()
67                         +"\n有载人数:"+getLoader()+"    载有重量:"+payload);
68         System.out.println();
69     }    
70 }
71 public class Test 
72 {
73     public static void main(String[] args) 
74     {
75         Vehicle S[]=new Vehicle[]{    
76                                     new Vehicle(4,50.0),
77                                     new Car(4,90.0,2),
78                                     new Truck(6,200,2,100)
79                                  };
80         for(int i=0;i<S.length;i++)
81         {
82             S[i].printInformation();
83         }
84         //System.out.println(S[i].getPayload());
85     }
86 }

运行结果如下:

总结:

  将子类对象赋值给父类对象,编译时为父类对象,但运行时为子类对象,具体特征如下:

  (1)被声明为父类对象

  (2)拥有父类属性

  (3)占用子类的内存空间

  (4)子类方法覆盖父类的方法时,此时对象调用的是子类的方法;否则调用继承父类的方法

    (5)  无法访问子类中非覆盖的变量和方法,如在第3个程序中不能运行System.out.println(S[i].getPayload());

  此次实训作业中,还是对这向上转型有丁点懵,既然无法访问子类的非覆盖的变量,那么卡车的载有重量又从何而来?

 

转载于:https://www.cnblogs.com/duwenze/p/10793880.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值