深一点了解面向对象

——————面向对象———————

在这里插入图片描述

  • 编写Account类,添加带参

  • 账号初始金额是0元

    public class Demon01 {
        / /定义学生数组
     String[] s=new String[30];
      //单个参数的传入,学员姓名添加方法
     public void add(String name) {
     for (int i = 0; i < s.length; i++){ 
     	if (s[i]==null) {
     		s[i]=name;
     		break;
     	}	
     }
     }
    public void  show() {
     for (int i = 0; i < s.length; i++) {
     	if (s[i]!=null) {
     		System.out.print(s[i]+"\t");
     	}
     }
    
  • main方法:

  •  public class Demon01Test {
    
    	public static void main(String[] args) {
      Scanner input =new Scanner(System.in);
      Demon01 s1=new Demon01(); 
      while(true){
      System.out.println("请输入客户的姓名");
      String name =input.next();
      s1.add(name);
      System.out.println("继续输入吗?(y/n)");
      String s2=input.next();
      if (s2.equals("n")) {
      	break;	
    	}
      }
     System.out.println("***********");
     System.out.println("客户姓名列表");
     s1.show();   	       
    }
    
  • 2.对象的封装
    请输入姓名T
    请输入姓名V
    请输入姓名Q
    请输入姓名P
    请输入姓名A
    修改信息
    请输入原始名称
    A
    请输入新名称
    B
    修改成功
    T V Q P B

     public class StuSystem {
    String[] Stuarr = new String[30];
     public void add( String name) {	
    for (int i = 0; i < Stuarr.length; i++) {
    	if (Stuarr[i] == null) {
    		Stuarr[i] = name;	
    		break;
    	}
    	}
    }
      public boolean  modfiy(String  oldname,String  newname) {
      boolean flag=false;
     for (int i = 0; i < Stuarr.length; i++) {
    if (Stuarr[i].equals(oldname)) {
        Stuarr[i]=newname; 
        flag=true;
        break;
    }
    }
     return  flag;
    }
     public void  show() {
    for (int i = 0; i < Stuarr.length; i++) {
    	if (Stuarr[i]!=null) {
    		System.out.print(Stuarr[i]+"\t");
    	}
    }	
    }
    }
    
    • main方法:
      public class stuTest {
     public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    StuSystem  ssy=new StuSystem();//创建对象
    for(int i=0;i<5;i++){
    System.out.print("请输入姓名");
    String name=input.next();
    ssy.add(name);
    }
    System.out.println("******修改信息******");
    System.out.println("请输入原始名称");
    String oldname=input.next();
    System.out.println("请输入新名称");
    String newname=input.next();
    if (ssy.modfiy(oldname, newname)) {
    	System.out.println("修改成功");
    }else {
    	System.out.println("修改失败");
    }
    //调用对象方法
    ssy.show();
    }      }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值