1 第一题:
2 package JavaPractice;
3 class Monkey {
4 private String s;
5 public Monkey() {
6 }
7 public Monkey(String s) {
8 this.s=s;
9 }
10 public String getS() {
11 return s; }
12 public void speak() {
13 System.out.println("咿咿呀呀......");
14 }
15 }
16 class People extends Monkey {
17 public void speak() {
18 System.out.println("小样的,不错嘛!会说话了!");
19 }
20 public void think() {
21 System.out.println("别说话!认真思考!");
22 }
23 }
24 public class E {
25 public static void main(String[] args) {
26 Monkey m=new Monkey();
27 People p=new People();
28 m.speak();
29 p.speak();
30 p.think();
31 }
32 }
33 第二题:
34 package JavaPractice;
35 import java.util.Scanner;
36 class Rectangle {
37 private double length;
38 private double width;
39 public double getLength() {
40 return length; }
41 public double getWidth() {
42 return width; }
43 public Rectangle(double length, double width) {
44 this.length = length;
45 this.width = width; }
46 public double getArea() {
47 return length*width; }
48 }
49 class Cuboid extends Rectangle {
50 private double height;
51 public double getHeight() {
52 return height;
53 }
54 public Cuboid(double length, double width, double height) {
55 super(length, width);
56 this.height = height;
57 }
58 public double getVolume() {
59 return getLength()*getWidth()*height;
60 }
61 }
62 public class TestPattern {
63 public static void main(String[] args) {
64 Scanner sc=new Scanner(System.in);
65 System.out.println("请分别输入长方形的长、宽:");
66 Rectangle rc=new Rectangle(sc.nextDouble(),sc.nextDouble());
67 System.out.println("长方形的面积为:"+rc.getArea()); System.out.println();
68 System.out.println("请分别输入长方体的长、宽、高:");
69 Cuboid cb=new Cuboid(sc.nextDouble(),sc.nextDouble(),sc.nextDouble()); System.out.println("长方体的底面积为:"+cb.getArea()); System.out.println("长方体的体积为:"+cb.getVolume());
70 }
71 }
72 第三题:
73 package LHB.inherit;
74 import java.util.*;
75 class Vehicle
76 {
77 int wheels,weight;/*车轮个数,车重*/
78 public Vehicle(int wheels,int weight)
79 {
80 this.wheels=wheels;
81 this.weight=weight;
82 }
83 public void print()
84 {
85 System.out.println("车轮个数是:"+wheels+"个\n"+"车重是:"+weight);
86 }
87 }
88 class Car extends Vehicle
89 {
90 int loader;/*车载人数*/
91 public Car(int wheels, int weight,int loader)
92 {
93 super(wheels, weight);
94 this.loader=loader;
95 }
96 public void print()
97 {
98 System.out.println("车载人数是:"+loader);
99 }
100 }
101 class Truck extends Car
102 {
103 int payload;/*载重量*/
104 public Truck(int wheels, int weight, int loader,int payload)
105 {
106 super(wheels, weight, loader);
107 this.payload=payload;
108 }
109 public void print()
110 {
111 System.out.println("载重量是:"+payload);
112 }
113 }
114 public class Testcar
115 {
116
117 public static void main(String[] args)
118 {
119 Scanner in=new Scanner(System.in);
120 int wheels,weight,loader,payload;
121 System.out.print("请输入车轮个数,车重,车载人数,载重量:");
122 wheels=in.nextInt();
123 weight=in.nextInt();
124 loader=in.nextInt();
125 payload=in.nextInt();
126 Vehicle p1=new Vehicle(wheels,weight);
127 Car p2=new Car(wheels,weight,loader);
128 Truck p3=new Truck(wheels,weight,loader,payload);
129 p1.print();
130 p2.print();
131 p3.print();
132 }
133
134 }