一、修改手机默认语言
代码
public class Hol6_4 {
private Hol6_4() {
}
// 静态公开方法,向图书馆借书
static public Hol6_4 libraryBorrow() { // 创建静态方法,返回本类实例对象
System.out.println("智能手机的默认语言为英文");
return new Hol6_4();
}
{
System.out.println("将智能手机的默认语言设置为中文");
}
public static void main(String[] args) {
// 创建一个书的对象,不是new实例化的,而是通过方法从图书馆借来的
Hol6_4 book = Hol6_4.libraryBorrow();
}
}
结果
二、设置信用卡密码
代码
public class Hol6_5 {
String carnum; //卡号
String passward; //密码
public Hol6_5(String carnum,String passward) { //有密码的构造方法
this.carnum = carnum;
this.passward = passward;
if(passward.equals("123456")) { //判断密码是否修改
System.out.println("信用卡"+carnum+"的默认密码为123456");
}
else {
System.out.println("重置信用卡"+carnum+"的密码为"+passward);
}
}
public Hol6_5(String carnum) { //无密码的构造方法
this(carnum,"123456"); //默认为123456
}
public static void main(String[] args) {
Hol6_5 people1 = new Hol6_5("4013735633800642");
Hol6_5 people2 = new Hol6_5("4013735633800642"+"168779");
}
}
结果
三、飞速的高铁
代码
public class Hol6_6 {
final static float PI=145.8f;
public static void main(String[] args) {
int a=2;
float speed=PI*a;
huoche b=new huoche(PI);
gaotie c=new gaotie(speed);
}
}
class huoche{
float PI;
public huoche(float PI){
this.PI=PI;
System.out.println("火车的速度为"+PI+"公里/小时");
}
}
class gaotie{
float speed;
public gaotie(float speed){
this.speed=speed;
System.out.println("高铁的速度为"+speed+"公里/小时");
}
}
结果
四、计算机械钟和石英手表的时间
代码
import java.util.Date;
public class Hol6_7 {
public void time(){
Date date=new Date();
String hour=String.format("%tH",date);
String minute=String.format("%tM",date);
System.out.println("当前时间:"+hour+"点"+minute+"分");
}
public static void main(String[] args) {
Hol6_7 a=new Hol6_7();
System.out.println("机械钟的价格为189.99元RMB");
a.time();
System.out.println("石英手表的价格为69.0元RMB");
a.time();
}
}
结果