• Java基础递归、枚举、泛型、聚合


    ----------------------------------------递归----------------------------------------

    //递归的概念:方法的方法体中出现了调用本身的现象

    //递归:先传递值,再回归值

    public class AddRecursion{

      public static void main(String[] args){

        System.out.println(add(100));//显示1+2+3+..+99+100的和

      }

      public static int add(int i){

        if(i==1)

        return 1;//出口

        return i+add(i-1);//递归

      }

    }

    --------------------递归举例:求1!+2!+3!+...+15! 的和--------------------

    /**
     * 2016-5-8上午10:30:58
     * 
     */
    
    public class FactorialSum {
    
        public static void main(String[] args) {
            long sum = 0;
            for (long i = 1; i <= 15; i++) {
                sum += factorial(i);
            }
            System.out.println("1!+2!+3!+...+15!=" + sum);
        }
    
        public static long factorial(long n) {
            if (n == 1)
                return 1;
            return n * factorial(n - 1);
        }
    
    }

    ----------------------------------------枚举----------------------------------------

    //还是做一个小Demo来看吧~

    class SexOfStudent {

      public SexEnum sex;

    }

    //生成一个枚举类

    enum SexEnum {

      男, 女;//将值一一列出

    }

    //使用枚举类

    public class EnumTest {

      public static void main(String[] args) {

        SexOfStudent sexStu = new SexOfStudent();

        sexStu.sex = SexEnum.男;
        System.out.println(sexStu.sex);

      }
    }

    ----------------------------------------泛型----------------------------------------

    import java.util.*;
    
    //在二维平面画一个点
    
    class Point<T, M> {//T和M代表两个类型,字母可以任意
        //个性
        private T x;
        private M y;
    
        //构造器
        public Point() {
        }
    
        public Point(T x, M y) {
            super();
            this.x = x;//横坐标
            this.y = y;//纵坐标
        }
    
        //设定器、访问器
        public T getX() {
            return x;
        }
    
        public void setX(T x) {
            this.x = x;
        }
    
        public M getY() {
            return y;
        }
    
        public void setY(M y) {
            this.y = y;
        }
    
        //功能代码
        @Override
        public String toString() {
            return "横坐标:" + x + " 纵坐标:" + y;
        }
    
    }
    
    public class MyGenericDemo {
    
        public static void main(String[] args) {
            //开始画各种点,因为使用了泛型,编译和运行都不会报错
            Point point01 = new Point(2, 20);
            Point point02 = new Point(2.2, 20);
            Point point03 = new Point("x", "y");
            Point point04 = new Point(null, 20);
            Point point05 = new Point("日本", true);
            Set<Point> set = new HashSet<Point>();
            set.add(point01);
            set.add(point02);
            set.add(point03);
            set.add(point04);
            set.add(point05);
            for (Point point : set) {
                System.out.println(point);
            }
        }
    
    }

     ----------------------------------------聚合----------------------------------------

    我们写代码,要高内聚,即聚合度要高,如以下代码中的地址类,聚合到人的属性里了,就是提高聚合度~~

    //地址的类
    class Addr {
        private String street;
        private int zipCode;
        private String city;
    
        public Addr() {
        }
    
        public Addr(String street, int zipCode, String city) {
            super();
            this.street = street;
            this.zipCode = zipCode;
            this.city = city;
        }
    
        public String getStreet() {
            return street;
        }
    
        public void setStreet(String street) {
            this.street = street;
        }
    
        public int getZipCode() {
            return zipCode;
        }
    
        public void setZipCode(int zipCode) {
            this.zipCode = zipCode;
        }
    
        public String getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
    }
    
    //普通人的类
    public class Person {
        //人的家庭地址可以抽象出来放到地址里,而不是全部放到这里
        static Addr pAddr = new Addr("百老汇街", 100866, "北京");
        private static String name;
    
        public static void main(String[] args) {
            name = "刘德华";
            System.out.println(name + "住在" + pAddr.getCity() + pAddr.getStreet());
            System.out.println("邮编:" + pAddr.getZipCode());
        }
    
    }
  • 相关阅读:
    java学习6-java基础类库
    java学习5-面向对象(下)
    java学习4-面向对象(上)
    java学习3-流程控制与数组
    java学习2-数据类型和运算符
    pycharm(社区版2019.1版本)打开README.md文件卡死解决办法
    python深拷贝与浅拷贝的区别
    python中list切片详解
    python使用input().split()接收多个用户输入
    python学习-语言概述(一)
  • 原文地址:https://www.cnblogs.com/qixiawentang/p/5464943.html
Copyright © 2020-2023  润新知