JAVA语言程序设计课后习题----第七单元解析(仅供参考)

1  本题水题,就是想让你理解继承的含义

1 public class Animaal {
2     public double weight;
3     public void eat(){
4     }
5 }
1 public class Bird extends Animaal {
2     public int numberOfFins;
3     public void fly(){}
4 
5 
6 }
1 public class Dog extends Animaal {
2     public int numberOflegs;
3     public void walk(){}
4 
5 
6 }
1 public class Fish extends Animaal{
2     public int numberOfFins;
3     public void swim(){}
4 
5 
6 }

 2  本题主要考类的继承和方法的重写,方法的重写关键字@Override

 

1 public class Circle {
2     public int radius;
3     public double getArea(){
4         return Math.PI*radius*radius;
5     }
6 }
 1 public class Cylinder extends Circle {
 2     public double height;
 3 
 4     public Cylinder() {
 5     }
 6 
 7     public Cylinder(double height) {
 8         this.height = height;
 9     }
10     public Cylinder(int radius,double height){
11         this.radius=radius;
12         this.height=height;
13     }
14     //    体积
15     public double getVolume(){
16         return Math.PI*radius*radius*height;
17     }
18     //    覆盖Circle 里面的getArea函数求圆柱的体积
19     @Override
20     public double getArea(){
21         return Math.PI*radius*radius*2+2*Math.PI*radius*height;
22     }
23 }
 1 import java.util.Scanner;
 2 
 3 public class ch08 {
 4     public static void main(String[] args) {
 5 
 6         Scanner input = new Scanner(System.in);
 7         System.out.println("请输入圆柱的半径、高");
 8         Cylinder cylinder = new Cylinder(input.nextInt(),input.nextDouble());
 9         System.out.println("圆柱的体积为:"+cylinder.getVolume());
10         System.out.println("圆柱的面积为:"+cylinder.getArea());
11     }
12 }

 3  本题水题,考的方法和上面两题类似,注意题目要求即可

 1 public class Auto {
 2     public double speed;
 3     //    启动的方法
 4     public void start(){
 5         System.out.println("插上钥匙、系上安全带、一脚把离合踩到底、挂上一档、打左转向、缓慢抬脚、汽车平稳启动");
 6     }
 7     //    加速的方法
 8     public void speedUp(){
 9         System.out.println("脚慢踩油门、松开油门、踩离合到底、换挡、加速完成");
10     }
11     //    停止的方法
12     public void stop(){
13         System.out.println("慢踩刹车、达到额定速度、松开刹车、踩离合、换挡、持续到速度为0");
14     }
15 }
1 public class Bus extends Auto {
2     public int passenger;
3     public Bus(int passenger){this.passenger=passenger;}
4     //    上车
5     public int gotOn(){ return ++passenger;
6     }
7     //    下车
8     public int gotOff(){return --passenger;}
9 }
 1 import java.util.Scanner;
 2 
 3 public class ch09 {
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         System.out.println("请输入开始车上的人数");
 7         Bus bus = new Bus(input.nextInt());
 8         System.out.println("旅客上车");
 9         System.out.println(bus.gotOn());
10         bus.start();
11         bus.speedUp();
12         System.out.println("旅客上下车");
13         System.out.println(bus.gotOff());
14         bus.stop();
15     }
16 }

 

4  本题主要考继承和抽象方法,关键字@Override

 

 

1 public class Shape {
2     public int radius;
3     public int l;
4     public double getPrimeter(){
5         return 0;
6     }
7     public double getArea(){return 0;}
8 }
 1 public class Square extends Shape {
 2     public Square() {
 3     }
 4 
 5     public Square(int radius){this.radius=radius;}
 6     @Override
 7     public double getPrimeter(){
 8         return 4*radius;
 9     }
10     @Override
11     public double getArea(){
12         return radius*radius;
13     }
14 
15 }
1 public class ch10 {
2     public static void main(String[] args) {
3         Square square = new Square(10);
4         System.out.println("正方形的周长为:"+square.getPrimeter());
5         System.out.println("正方形的面积为:"+square.getArea());
6     }
7 }

5  本题主要考方法的覆盖,构造方法的重写

1 public class Rectangle {
2     public double length,width;
3 
4     public Rectangle(double length, double width) {
5         this.length=length;
6         this.width=width;
7     }
8 }
 1 public class Cuboid extends Rectangle{
 2     public double height;
 3 
 4     public Cuboid(double length, double width, double height) {
 5         super(length, width);
 6         this.height = height;
 7     }
 8 
 9     public double volume(){
10         return length*width*height;
11     }
12 }
1 public class ch11 {
2     public static void main(String[] args) {
3         System.out.println("长方体的长、宽、高分别为10、5、2!");
4         Cuboid cuboid = new Cuboid(10,5,2);
5         System.out.println("长方体的体积为:"+cuboid.volume());
6     }
7 }

 

posted @ 2019-05-20 09:39  不愿当个小萌新  阅读(415)  评论(0编辑  收藏  举报