2024.1.24 java 学习成果展示

 数组

1.数组的声明

 int类型数组初始值为0,boolean类型数组初始值为false,char类型数组初始值为0(不显示)

public class ArrayDemo01 {
    public static void main(String[] args) {
        //数组的声明
        int[] arr = new int[5];
        System.out.println(arr[0]);
        boolean[] brr = new boolean[5];
        System.out.println(brr[0]);
        char[] crr = new char[5];
        System.out.println(crr[0]);
        System.out.println("---------");
    }
}

实例1 ,彩票双色球抽奖

//七位数双色球 红球6个 范围1-33 蓝球 1-17
public class DoubleColorBall {
    public static void main(String[] args) {
        //声明一个int类型数组,长度为7,动态
        int[] arr = new int[7];
        //创建一个随机数
        Random random = new Random();

        //开始摇号 红球 长度7 范围 1-33
        for (int i = 0; i < arr.length - 1; i++) {
            arr[i] = random.nextInt(33) + 1;
            //去重
            for (int j = i - 1; j >= 0; j--) {
                if (arr[i] == arr[j]) {
                    //表示出现重复
                    i--;//重新摇号
                    break;
                }
            }
        }

                //蓝球 长度 范围 1-17
                arr[arr.length - 1] = random.nextInt(17) + 1;
                //将数组遍历
                System.out.println("本期双色球中奖结果:");
                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[i] + " ");
        }
    }
}

 类,对象,引用

Point类

//定义一个坐标类
public class Point {
    //定义横纵坐标
    int x;
    int y;
    //无参构造方法
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //x加一方法
    public void  setX(){
        this.x = ++x;
    }
    //y增加指定数方法
    public void setY(int number){
        this.y += number;
    }
    //打印所有特征的方法
    public void show(){
        System.out.println("坐标为( "+ x + ","+ y +" )");
    }
    //mian方法
    public static void main(String[] args) {
        //定义Point对象
        Point p = new Point(1,1);
        p.setX();
        p.setY(3);
        p.show();
    }
}

 方法的调用

Girl类

public class Girl {
    String name;
    int age;
    boolean bool;
    public void show(){
        System.out.println("姓名"+ name+",年龄:"+age+"是否有男朋友 :"+ bool);
    }
    
    public static void main(String[] args) {
        Girl g = new Girl();
        g.show();
        g.age = 18;
        g.name = "貂蝉";
        g.bool = true;
        g.show();
    }
}

构造方法和方法重载

注意 :println();打印方法是所学的最早用的方法重载

实例1

public class Circle {
    //半径
    double r;
    //圆周率PI
    final double PI = Math.PI;
    //求圆的面积
    public double getArea(){
        return PI * r * r;
    }
    //求圆的周长
    public double getPerimeter(double r) {
        return 2 * r * PI;
    }
    //Circle的构造方法
    public Circle(double r) {
        this.r = r;
    }
    //test
    public static void main(String[] args) {
        //创建Circle对象
        Circle c = new Circle(5.0);
        c.getArea();
        c.getPerimeter(5.0);
        //打印周长和面积
        System.out.println("周长为 "+ c.getPerimeter(5.0));
        System.out.println("面积为 "+ c.getArea());
    }
}

实例2 

public class Vehicle{
    double speed;
    String  type;
    //移动方法
    public void move(){
        System.out.println("车辆正在速度为"+ speed );
    }
    //设置速度的方法
    public void setSpeed(double s){
        this.speed = s;
    }
    //填写型号
    public void setType(String type) {
        this.type = type;
    }
    //加速方法
    public void speedUp(double s){
        //speed = speed + s;
        // 改进
        this.speed += s;
    }
    //减速的方法
    public void speedDown(double s){
        //speed = speed - s;
        //改进
        this.speed -= s;
    }

    public static void main(String[] args) {
        //创建Vehicle类对象
        Vehicle v = new Vehicle();
        //设置,减速和加速
        v.setSpeed(30);
        v.speedDown(10);
        v.speedUp(50);
        //打印速度和类型
        System.out.println("车辆类型: " + v.type);
        v.move();
    }
}

封装 

this关键词(理解成“我的”)

实例

import java.util.Objects;

public class Work {
    int id;
    int userId;
    String start;
    String end;
    String company;
    String job;
    String description;
    Work next;

    @Override
    public String toString() {
        return "Work{" +
                "id=" + id +
                ", userId=" + userId +
                ", start='" + start + '\'' +
                ", end='" + end + '\'' +
                ", company='" + company + '\'' +
                ", job='" + job + '\'' +
                ", description='" + description + '\'' +
                ", next=" + next +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Work work = (Work) o;
        return id == work.id &&
                userId == work.userId &&
                Objects.equals(start, work.start) &&
                Objects.equals(end, work.end) &&
                Objects.equals(company, work.company) &&
                Objects.equals(job, work.job) &&
                Objects.equals(description, work.description) &&
                Objects.equals(next, work.next);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, userId, start, end, company, job, description, next);
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setNext(Work next) {
        this.next = next;
    }

    public int getId() {
        return id;
    }

    public int getUserId() {
        return userId;
    }

    public String getStart() {
        return start;
    }

    public String getEnd() {
        return end;
    }

    public String getCompany() {
        return company;
    }

    public String getJob() {
        return job;
    }

    public String getDescription() {
        return description;
    }

    public Work getNext() {
        return next;
    }

    public Work(int userId, String start, String end, String company, String job, String description) {
        this.userId = userId;
        this.start = start;
        this.end = end;
        this.company = company;
        this.job = job;
        this.description = description;
    }

    public Work(int id, String name, int age, String city, String address, String email, String phone, String weixin, String qq, String weibo, String sex, String description) {
        this.id = id;
        this.description = description;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值