**
输入日期格式字符串作为商品的生产日期,输入保质期(天);计算截止到今天,该商品还有多少天会过保质期
1、控制台输入字符串转为日期格式
2、将输入日期增加保质期天数
3、取得当前系统日期,比较两个日期,如果过期输出“该商品已经过期”
**
@Test
public void test7() throws ParseException{
System.out.println("请输入生产日期和过期时间");
//得到生产日期
String date_str=new Scanner(System.in).next();
//得到过期时间
int days=new Scanner(System.in).nextInt();
//指定日期转换格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//把用户输入的字符串转成日期型
Date d1=sdf.parse(date_str);
Calendar c = Calendar.getInstance();
c.setTime(d1);
//加上n天
c.add(c.DATE, days);
//过期日期
Date d_guo=c.getTime();
//打印过期日期
System.out.println("过期日期为:"+d_guo);
//获取当前日期
Date now=sdf.parse(sdf.format(new Date()));
System.out.println("当前日期为:"+now);
//比较两个日期先后,得到结果
if(now.before(d_guo)){
System.out.println("已过期");
}else{
System.out.println("未过期");
}
}
public static long days(String begin,String end) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d1=sdf.parse(begin);
Date d2=sdf.parse(end);
long begintime=d1.getTime();
System.out.println("毫秒数:"+begintime);
long endtime=d2.getTime();
//毫秒数换数成天 1天=24小时 1小时=60分 1分=60秒 1秒=1000毫秒
long day=(endtime-begintime)/24/60/60/1000;
System.out.println(day);
return day;
}
)编程实现、以电话Phone为父类(例、电话有本机号码、打电话、接电话等属性和功能,当然还有一些其它的特性;
移动电话Mobilephone和固定电话Fixedphone为两个子类,并使移动电话实现接口Moveable,接口里有移动信息功能;
固定电话又有子类、无绳电话Cordlessphone。
设计并定义这几个类,明确它们的继承关系,定义子类时给出子类有别于父类的新特性。(可以自已添加)
2)声明测试类、声明Phone类的数组(含5个元素),生成五个对象存入数组、其中二个Phone类的对象、一个Mobilephone类的对象、一个Fixedphone类的对象和一个Cordlessphone类的对象,打印输出每个对象的某个成员变量。将一个父类的引用指向一个子类对象,用这个塑型后的对象来调用某个方法实现多态性。
Phone类
public class Phone {
private String Number;
private String color;
private String brand;
public void call(){
System.out.println("打电话");
}
public void getCall(){
System.out.println("接电话");
}
public Phone(String number, String color, String brand) {
super();
this.Number = number;
this.color = color;
this.brand = brand;
}
public String getNumber() {
return Number;
}
public String getColor() {
return color;
}
public String getBrand() {
return brand;
}
}
Mobilephone类
public class Mobilephone extends Phone implements Moveable {
private int screenSize;
public void moveMessage(){
System.out.println("发短信");
}
@Override
public void call() {
System.out.println("移动着打电话");
}
@Override
public void getCall() {
System.out.println("移动着接电话");
}
public Mobilephone(String number, String color, String brand, int screenSize) {
super(number, color, brand);
this.screenSize = screenSize;
}
public int getScreenSize() {
return screenSize;
}
}
Fixedphone类
public class Fixedphone extends Phone {
private String locNum;
@Override
public void call() {
System.out.println("固定地点打电话");
}
@Override
public void getCall() {
System.out.println("固定地点接电话");
}
public Fixedphone(String number, String color, String brand, String locNum) {
super(number, color, brand);
this.locNum = locNum;
}
public String getLocNum() {
return locNum;
}
}
Cordlessphone类
public class Cordlessphone extends Fixedphone {
public Cordlessphone(String number, String color, String brand, String locNum) {
super(number, color, brand, locNum);
}
@Override
public void call() {
System.out.println("在一定范围内移动着打电话");
}
@Override
public void getCall() {
System.out.println("在一定范围内移动着接电话");
}
}
Moveable接口
public interface Moveable {
public void moveMessage();
}
TestPhone类
public class TestPhone {
public static void main(String[] args) {
Phone p[]= new Phone[5];
p[0] = new Phone("13998373955", "黑色", "samsung");
p[1] = new Phone("13998373966", "白色", "apple");
p[2] = new Mobilephone("13998373911", "蓝色", "XiaoMi", 6);
p[3] = new Fixedphone("83739223", "红色", "ChinaTelecom", "024");
p[4] = new Cordlessphone("73922346", "黄色", "ChinaUnicom","0414");
for (int i = 0; i < 5; i++) {
String s = p[i].getBrand();
System.out.println(s);
p[i].getCall();
}
Phone p1 = new Mobilephone("13998373933", "绿色", "oppo", 5);
p1.call();
}
单例
public class danli {
//懒汉式
//1.私有静态的类型为自身的成员变量
private static danli instace=new danli();
//2.私有的构造方法
private danli(){
//do something
}
//3.共有的静态的方法,返回创建后的对象
public static danli getInstance(){
return getInstance();
}
}
}
// 冒泡排序(下沉法)
public static void bubbleSort(int[] a) {
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < a.length - i ; j++) {
if (a[j] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
小车初速度:
Vehicle类
package com.neuedu.chap07;
public class Vehicle {
private String brand;
private String color;
private double speed;
public Vehicle(String brand, String color, double speed) {
super();
this.brand = brand;
this.color = color;
this.speed = 0;
}
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public double getSpeed() {
return speed;
}
public void run() {
System.out.println(color + "的" + brand + "行驶的速度为" + speed);
}
}
Car类
package com.neuedu.chap07;
public class Car extends Vehicle {
private int loader;
public Car(String brand, String color, double speed, int loader) {
super(brand, color, speed);
this.loader = loader;
// TODO Auto-generated constructor stub
}
public void run() {
System.out.println(getColor() + "的" + getBrand() + "行驶的速度=" + getSpeed() + "载客人数=" + loader);
}
}
TestVehicle类
package com.neuedu.chap07;
public class TestVehicle {
public static void main(String[] args) {
Vehicle v1 = new Vehicle("benz", "black", 100);
v1.run();
Car v2 = new Car("Honda", "red", 150, 2);
v2.run();
}
}