实训day3

1.数组的简单实现

/**
 * Description: JavaPractice
 *
 * @Author: liuxiaofeng
 * @Date Created in 2024/1/24 8:20
 * @Date Modified by: liuxiaofeng on 2024/1/24
 */
public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr = new int[20];
        for(int i=1;i< arr.length;i++){
            System.out.print(arr[i]);
        }
    }
}

2.有参/无参

/**
 * Description: JavaPractice
 *
 * @Author: liuxiaofeng
 * @Date Created in 2024/1/24 16:37
 * @Date Modified by: liuxiaofeng on 2024/1/24
 */
public class Car {
    private String brand; // 品牌
    private String color; // 颜色
    private double price; // 价格

    // 无参构造方法
    public Car() {
    }

    // 三个参数的构造方法
    public Car(String brand, String color, double price) {
        this.brand = brand;
        this.color = color;
        this.price = price;
    }

    // 打印特征
    public void printFeatures() {
        System.out.println("品牌: " + brand);
        System.out.println("颜色: " + color);
        System.out.println("价格: " + price);
    }

    // 其他方法测试
    public void testMethod() {
        System.out.println("其他方法的测试");
    }

    public static void main(String[] args) {
        // 使用无参形式构造对象并打印特征
        Car car1 = new Car();
        car1.printFeatures();

        System.out.println("===================");

        // 使用有参形式构造对象并打印特征
        Car car2 = new Car("BMW", "黑色", 200000);
        car2.printFeatures();

        System.out.println("===================");

        // 使用有参形式构造的对象调用其他方法测试
        car2.testMethod();
    }
}

3.简单的实体类和测试类

/**
 * Description: JavaPractice
 * 实体类 提供共有的方法   封装
 * @Author: liuxiaofeng
 * @Date Created in 2024/1/24 10:52
 * @Date Modified by: liuxiaofeng on 2024/1/24
 */
public class Person {
    private String name;
    private int age;
    public void show(){
        System.out.println("姓名:"+name+",年龄:"+age);
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName(){
        return name;
    }


}

4.练习

**
 * Description: JavaPractice
 *
 * @Author: liuxiaofeng
 * @Date Created in 2024/1/24 16:31
 * @Date Modified by: liuxiaofeng on 2024/1/24
 */
public class Rectangle {
    private int width; // 宽度
    private int length; // 长度

    // 构造方法


    // 求面积
    public int getArea() {
        return width * length;
    }

    // 求周长
    public int getPer() {
        return 2 * (width + length);
    }

    // 输出长、宽、面积、周长
    public void showAll() {
        System.out.println("长: " + length);
        System.out.println("宽: " + width);
        System.out.println("面积: " + getArea());
        System.out.println("周长: " + getPer());
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public static void main(String[] args) {
        // 创建一个Rectangle对象并初始化
        Rectangle rectangle = new Rectangle();

        // 输出相关信息
        rectangle.showAll();
    }
}

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值