方法的重载:在同一个类中存在很多个方法名相同的方法但参数列表或返回类型不同
import java.util.Scanner;
/**
*@author 作者 Your-Name:
*@version 创建时间:
*类说明:某个商城的搜索(军火商场)
*/
public class SJSouSuoYIQ {
int ID;
String Name;
String color;
float money;
public SJSouSuoYIQ (int ID,String Name,String color,float money)
{
this.ID = ID;//this关键字:在构造方法中对成员变量进行初始化时为解决成员变量与局部变量重名的冲突即this.成员变量名
this.Name = Name;
this.color = color;
this.money = money;
}
public SJSouSuoYIQ (int ID,String Name,String color)
{
this.ID = ID;
this.Name = Name;
this.color = color;
}
public void Print1()
{
System.out.print("编号\t名称\t\t颜色\t单价\n");
}
public void Print()
{
System.out.printf("%d\t%s\t\t%s\t%.2f元\n",ID,Name,color,money);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
SJSouSuoYIQ sy = new SJSouSuoYIQ (1001,"沙漠之鹰","黄色",1243);
SJSouSuoYIQ sy1 = new SJSouSuoYIQ (1002,"M416步枪","红色",3245);
SJSouSuoYIQ sy2 = new SJSouSuoYIQ (1003,"M762步枪","灰色",2300);
SJSouSuoYIQ sy5 = new SJSouSuoYIQ (1003,"M24狙击枪","灰色",13000);
SJSouSuoYIQ sy6 = new SJSouSuoYIQ (1003,"AWM狙击枪","灰色",23000);
SJSouSuoYIQ sy3 = new SJSouSuoYIQ (1004,"M24狙击枪","灰色");//方法的重载
SJSouSuoYIQ sy4 = new SJSouSuoYIQ (1005,"Awm狙击枪","绿色");//方法的重载
DeLu a = new DeLu();
int choice;
String name;
String pass;
System.out.println("******<<<<<<<枪子贩卖系统>>>>>>>*************");
System.out.print("请输入用户名:");
name = input.next();
System.out.print("请输入秘钥:");
pass = input.next();
if(a.yanZhen(name,pass) == 1)
{
System.out.println("欢迎" + name + "来到枪子贩卖系统");
System.out.println("枪子品种如下");
sy.Print1();
System.out.print("1,");
sy.Print();
System.out.print("2,");
sy1.Print();
System.out.print("3,");
sy2.Print();
System.out.print("4,");
sy5.Print();
System.out.print("5,");
sy6.Print();
System.out.print("请输入你的选择:");
choice = input.nextInt();
switch(choice)
{
case 1:
if(sy.money != 0)
{
System.out.println("沙漠之鹰:沙漠之鹰是1980年由MRI发布的一部狩猎手枪。原型枪则在1981年完成,而最终定型则是在以色列军事工业公司(IMI)。");
}
break;
case 2:
if(sy1.money != 0)
{
System.out.println("M416突击步枪:M416既可靠又准确,具有中等后座力和射速,为全方位的有效武器。");
}
break;
case 3:
if(sy2.money != 0)
{
System.out.println("M762突击步枪:在中近距离能爆发出强大的火力,但后坐力较大");
}
break;
case 4:
if(sy3.money == 0)
{
System.out.println("由于疫情流行,暂时售空");
}
break;
case 5:
if(sy4.money == 0)
{
System.out.println("由于疫情流行,暂时售空");
}
}
}
else
{
System.out.println("系统关闭,已通知用户本人");
System.out.println("倒计时:");
for(int i = 5;i >= 0;i --)
{
System.out.println(i + "秒");
}
System.exit(0);
}
System.out.println("欢迎下次使用\n");
}
}
class DeLu
{
public int yanZhen(String name,String pass)//成员方法
{
int count = 0;
String name1 = "沃尔";
String pass1 = "WR1254";
if(name.equals(name1) && pass.equals(pass1))//equals比较字符串
{
count = 1;
}
return count;
}
}