1.修改手机默认语言
//手机类 class phone{
private String language="英文"; //默认语言为英文
public phone(){
System.out.println("智能手机的默认语言为"+this.language);
}
//创建通过有参构造将手机语言设置为中文
public phone(String language) {
this.language = language;
System.out.println("将智能手机的默认语言设置为"+this.language);
}
}
public class main {
public static void main(String[] args) {
//创建两个手机对象,演示效果
phone apple=new phone(); //在创建手机的时刻,将手机的语言改为中文
phone huawei=new phone("中文");
}
}
2.设置信用卡密码:
class creditCard{
String id; //卡号
String password="123456"; //密码 //设置卡号,默认密码
public creditCard(String id) {
this.id=id;
System.out.println("信用卡"+this.id+"的默认密码为:"+this.password);
}
//设置初始密码与初始密码
public creditCard(String id,String Password){
this.id=id;
this.password=Password;
System.out.println("信用卡"+this.id+"的密码为:"+this.password);
}
//重置信用卡密码
public creditCard(creditCard data,String Password){
this.id=data.id;
this.password=Password;
System.out.println("重置信用卡"+this.id+"的密码为:"+this.password);
}
}
public class Main_2 {
public static void main(String[] args) {
//创建信用卡,不设置初始密码
creditCard num1=new creditCard("40137356156462146");
//重置密码,将需要重置的对象传递过去,使用其卡号,并返回一个新对象
num1=new creditCard(num1,"168779");
//重置密码
}
}
3.飞速的高铁
class train{
double speed;
public train() {
}
public train(double speed) {
this.speed = speed;
System.out.printf("火车的速度为 %.1f 公里/小时\n",speed);
}
}
class high_speed_railway extends train{
public high_speed_railway(double speed) {
super(speed);
//构造父类
this.speed=(super.speed*2);
//高铁的速度是火车的二倍
System.out.printf("高铁的速度为 %.1f 公里/小时\n",this.speed);
}
}
public class Main_3 {
public static void main(String[] args) {
//创建高铁类,传入火车的速度
high_speed_railway h=new high_speed_railway(145.8);
}
}
4.计算机械钟和石英手表的时间
class clock{
String type;
double price;
public static void getTime(){
//不确定题目要求的是获取当前时间,还是只是模拟获取时间
System.out.println("当前时间:10点10分");
}
public void show(){
System.out.printf("%s的价格为 %.2f元RMB\n",this.type,this.price);
}
public clock(String type, double price) {
this.type = type;
this.price = price;
}
}
public class Main_4 {
public static void main(String[] args) {
//创建两个钟表的对象,并赋值数据
clock clockNum1=new clock("机械钟",189.99);
clock clockNum2=new clock("石英表",69);
clockNum1.show();
clockNum1.getTime();
clockNum2.show();
clockNum2.getTime();
}
}
5.多功能参数(方法重载) ![](https://i-blog.csdnimg.cn/blog_migrate/067f66e5fe5f9b377444459c19263407.png)
public class main {
static final double PI=3.141592653589793;
public static void main(String[] args) {
//获取PI值
System.out.println(calculation());
//获取圆的面积
System.out.println(calculation(4));
//获取矩阵的面积
System.out.println(calculation(3, 4));
}
public static double calculation(double r){
return PI*(r*r); //计算圆的面积:pi*r方
}
public static double calculation(double wide,double height){
return wide*height; //计算矩形的面积:宽*高
}
public static double calculation(){
return PI; //无输入返回PI
}
}