第三次作业
package JavaPractice;
class Monkey {
private String s;
public Monkey() {
}
public Monkey(String s) {
this.s=s;
}
public String getS() {
return s; }
public void speak() {
System.out.println("咿咿呀呀......");
}
}
class People extends Monkey {
public void speak() {
System.out.println("很不错滴!长大了!");
}
public void think() {
System.out.println("请认真思考!");
}
}
public class E {
public static void main(String[] args) {
Monkey m=new Monkey();
People p=new People();
m.speak();
p.speak();
p.think();
}
}
第二题:
package JavaPractice;
import java.util.Scanner;
class Rectangle {
private double length;
private double width;
public double getLength() {
return length; }
public double getWidth() {
return width; }
public Rectangle(double length, double width) {
this.length = length;
this.width = width; }
public double getArea() {
return length*width; }
}
class Cuboid extends Rectangle {
private double height;
public double getHeight() {
return height;
}
public Cuboid(double length, double width, double height) {
super(length, width);
this.height = height;
}
public double getVolume() {
return getLength()*getWidth()*height;
}
}
public class TestPattern {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请分别输入长方形的长、宽:");
Rectangle rc=new Rectangle(sc.nextDouble(),sc.nextDouble());
System.out.println("长方形的面积为:"+rc.getArea()); System.out.println();
System.out.println("请分别输入长方体的长、宽、高:");
Cuboid cb=new Cuboid(sc.nextDouble(),sc.nextDouble(),sc.nextDouble()); System.out.println("长方体的底面积为:"+cb.getArea()); System.out.println("长方体的体积为:"+cb.getVolume());
}
}
第三题:
package LHB.inherit;
import java.util.*;
class Vehicle
{
int wheels,weight;/*车轮个数,车重*/
public Vehicle(int wheels,int weight)
{
this.wheels=wheels;
this.weight=weight;
}
public void print()
{
System.out.println("车轮个数是:"+wheels+"个\n"+"车重是:"+weight);
}
}
class Car extends Vehicle
{
int loader;/*车载人数*/
public Car(int wheels, int weight,int loader)
{
super(wheels, weight);
this.loader=loader;
}
public void print()
{
System.out.println("车载人数是:"+loader);
}
}
class Truck extends Car
{
int payload;/*载重量*/
public Truck(int wheels, int weight, int loader,int payload)
{
super(wheels, weight, loader);
this.payload=payload;
}
public void print()
{
System.out.println("载重量是:"+payload);
}
}
public class Testcar
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int wheels,weight,loader,payload;
System.out.print("请输入车轮个数,车重,车载人数,载重量:");
wheels=in.nextInt();
weight=in.nextInt();
loader=in.nextInt();
payload=in.nextInt();
Vehicle p1=new Vehicle(wheels,weight);
Car p2=new Car(wheels,weight,loader);
Truck p3=new Truck(wheels,weight,loader,payload);
p1.print();
p2.print();
p3.print();
}
}