简单增删改

简单增删改

  • User类
package com.guoba.interfacetest;

import com.sun.org.apache.xpath.internal.operations.String;

import java.util.Scanner;

public class User implements Function{
    String[] xinxiarr= new String[10];
    @Override
    public void add(int num) {//增


        if (num == 0 ||num<0||num>3){
            System.out.println("不合法");
        }else if(num==1){
            System.out.println("添加成功!");
        }else {
            System.out.println("这是添加功能");
        }
    }

    @Override
    public void updata(int num) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择操作【1】添加【2】修改【3】删除");
        num = scanner.nextInt();
        if (num == 0 ||num<0||num>3){
            System.out.println("不合法");
        }else if(num==2){
            System.out.println("修改成功!");
        }else {
            System.out.println("这是修改功能");
        }
    }

    @Override
    public void delete(int num) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择操作【1】添加【2】修改【3】删除");
        num = scanner.nextInt();
        if (num == 0 ||num<0||num>3){
            System.out.println("不合法");
        }else if(num==3){
            System.out.println("删除成功!");
        }else {
            System.out.println("这是删除功能");
        }
    }
}
  • UserService类
package com.guoba.interfacetest;

import java.util.Scanner;

public class UserService extends User{
    public static void main(String[] args) {
        Function function = new Function() {
            @Override
            public void add(int num) {

            }

            @Override
            public void updata(int num) {

            }

            @Override
            public void delete(int num) {

            }
        };
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择操作【1】添加【2】修改【3】删除");
        int num = scanner.nextInt();
        user.add(num);
        user.delete(num);
        user.updata(num);
    }
}
  • Function接口类
package com.guoba.interfacetest;
//包含增删改三个抽象方法
public interface Function {
    void add(int num);
    void updata(int num);
    void delete(int num);
}

运行效果图

posted @ 2021-12-14 08:42  锅巴编程  阅读(28)  评论(0编辑  收藏  举报