博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第三次实训作业
阅读量:5288 次
发布时间:2019-06-14

本文共 3601 字,大约阅读时间需要 12 分钟。

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 Car102 {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 }

 

转载于:https://www.cnblogs.com/wangdashuai/p/10853680.html

你可能感兴趣的文章
小心!Ubuntu14.04 升级到16.04 的几个坑
查看>>
PHP date
查看>>
51Nod 1299 监狱逃离
查看>>
Mvc 刷新PartialView
查看>>
<php>统计整个文件夹的大小
查看>>
PE文件详解(五)
查看>>
非node环境 vue-rouder 学习笔录1
查看>>
利用 JMetal 实现大规模聚类问题的研究(二) JMetal代码总览
查看>>
.Net 4.0特性 Tuple元组
查看>>
Linux相关操作
查看>>
MySQL两个时间相减
查看>>
闭包经典面试题,不看会后悔噢
查看>>
4. 寻找两个有序数组的中位数
查看>>
MySQL错误:Can't connect to MySQL server (10060)
查看>>
jdk1.6下使用sardine和jackrabbit-webdav的问题
查看>>
iOS应用主流UI架构实现
查看>>
C++ opencv高速样例学习——读图显示
查看>>
看看C# 6.0中那些语法糖都干了些什么(中篇)
查看>>
Docker使用小记
查看>>
记一次前端面试~~有兴趣的戳
查看>>