Java学习Day3——类和对象

61 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情

1.类和对象

image.png

代码结构:

image.png

代码实现:

类实现

public class Phone {
    String brand;
    String memory;
    String size;
    String color;
    double prize;

    Phone(String b,String m,String s,String c,double p){
        brand =b;
        memory =m;
        size = s;
        color = c;
        prize = p;
    }
}

对象实例化

public class TestPhone {
    public static void main(String[] args) {
        Phone phone1 = new Phone("苹果","128G","6.1","星光色",5799.0);
        Phone phone2 = new Phone("红米","4G","6.53","金色",1249.0);
        Phone phone3 = new Phone("华为","48G","6.3","黑色",999.0);

    }
}

2.默认值

char byte short int long 默认值为 0

float double 默认值为 0.0

boolean 默认值为 false

剩余类型默认值都为 null

2.this

有参构造(注意this位置)

public class Phone {
    String brand;
    String memory;
    String size;
    String color;
    double prize;


    Phone(String brand,String memory,String size,String color,double prize){
        this.brand =brand;
        this.memory =memory;
        this.size = size;
        this.color = color;
        this.prize = prize;
    }
}

无参构造

public class Student {
    String name;
    int age;

    Student(){

    }
}
public class TestStudent {
    public static void main(String[] args) {
        Student student = new Student();
        student.name = "张三";
        System.out.println(student.name);
    }
}

3.快速构建Spring Boot模块(IDEA社区版)

image.png 选择Maven image.png 相关配置 image.png 此时生成pom.xml文件 image.png

之后去官方网站配置,相关配置与上述配置要一致,选择EXPLORE image.png 同样生成pom.xml文件 image.png

然后Copy,粘贴替换之前pom.xml文件即可

完成上述操作,之后再次新建Spring Boot框架只需要把pom.xml中的module名字进行相应替换即可,例:module4->module5

快捷键: ctrl+F ctrl+R替换 image.png

最后需要自己写一个入口类

image.png