一维数组
概念:再java程序中,当需要存储多个数据类型相同的内容时,则需声明一个数组,声明数组的本质,就是在内存中申请一段连续的存储单元。
声明变量
数组类型 变量名 = 初始值;
数组名:数组中变量的名称;
数组的长度:数组名.length
数组中的元素:数组当中值
数组角标(下标):从0开始
声明数组的格式:
数组类型【】 数组名 = new 数据类型【数组的长度】;//动态声明的方式,推荐
数组的初始化;
如果不给数组直接赋值,那么整数默认为0,浮点数默认为0.0,布尔类型默认false;
数值的遍历
使用for循环
for(int i = 0;i <arr.length;i++){
System.out.println(a[i]);
}
public class Array {
public static void main(String[] args) {
//声明数组
int[] arr = new int[5];
System.out.println(arr[0]);
boolean[] ar = new boolean[2];
System.out.println(ar[0]);
char[] cr = new char[4];
System.out.println(cr[1]);
if(cr[0] == 0){
System.out.println("char数组的默认值为0");
}
}
}
public class Array01 {
public static void main(String[] args) {
int a[] = new int[5];
for(int i = 0;i <a.length;i++){
System.out.println(a[i]);
}
}
}
面向对象和面向过程的区别
将大象装冰箱需要几步
面向过程:打开冰箱,放入大象,放入冰箱(3步)
面向对象:找人放进去(1步)
面向对象
类,对象,引用
对象:
在java中,对象是指客观存在的实体
语法格式:
new 引用数据类型();
eg:
new Person()://表示创建一个Person类型的对象
public class Person {
String name;
int age;
public void show(){
System.out.println("姓名是"+name+",年龄为"+age);
}
public static void main(String[] args) {
Person p =new Person();
p.age = 30;
p.name = "zhangfei";
p.show();
}
}
public class Phone {
String name;
double money;
public void show(){
System.out.println("品牌是"+name+",价格为:"+money);
}
public static void main(String[] args) {
Phone p =new Phone();
p.name = "诺基亚";
p.money = 598.5;
p.show();
}
}
public class Point {
int x;
int y;
public static void main(String[] args) {
//创建一个Point类型的引用p,指向Point类型的对象
Point p = new Point();
System.out.println("x = "+p.x+",y = "+p.y);
//修改坐标值
p.x = 3;
p.y = 4;
System.out.println("x = "+p.x+",y = "+p.y);
}
}
public class Girl {
String name;
int age;
boolean you;
public void show(){
System.out.println("名字是"+name+",年龄"+age+",是否有男朋友"+you);
}
public static void main(String[] args) {
Girl g = new Girl();
g.age=18;
g.name = "貂蝉";
g.you = true;
g.show();
}
}
类:
简单来说就是分类,具有相同特征和行为的多个事物共性的抽象,在java表现为一种引用数据类型,其中包含描述特征的成员变量和描述行为的成员方法;
语法格式:
【权限修饰符】 class 类名{
类体
}
类名首字母大写
UpperCameCase(大驼峰命名法)
成员变量
语法格式:
【权限修饰符】数据类型 成员变量名 = 成员变量值:
成员变量名首字母小写,由多个单词构成时,从第二个开始,首字母大写
LowerCameCase(小驼峰命名法)
成员方法(行为):
语法格式:
【权限修饰符】返回值类型 成员方法名(形参数据类型1 形参变量名1){
方法体;
}
public class Point01 {
int x;
int y;
Point01(){}
Point01(int x,int y){
this.x = x;
this.y = y;
}
public void add(int x,int y){
x = x + 1;
}
public String show(){
return "[横坐标为:"+ x +",纵坐标为:"+ y +"]";
}
public static void main(String[] args) {
Point01 p = new Point01();
System.out.println(p.show());
Point01 p1 = new Point01(1,2);
System.out.println(p1.show());
p.add(p.x,p.y);
System.out.println(p.show());
}
}
public class Circle {
double r;
public double Area(double r){
return Math.PI*r*r;
}
public double Long(double r){
return Math.PI*r*2;
}
public static void main(String[] args) {
Circle c = new Circle();
c.r = 5;
System.out.println("圆的面积为"+c.Area(c.r)+",周长为"+c.Long(c.r));
}
}
import java.util.Random;
public class Rectangle {
int length;
int width;
public void Rectangle(int width,int length){
this.width = width;
this.length = length;
}
public int getArea(){
return length * width;
}
public int getPer(){
return 2 * (length + width);
}
public void show(){
System.out.println("长为"+ length+",宽为"+width+",面积为"+getArea()+",周长为"+getPer());
}
public static void main(String[] args) {
Rectangle re = new Rectangle();
re.Rectangle(2,4);
re.show();
}
}
public class Vehicle {
double speed;
String type;
public double getSpeed() {
return speed;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void move(){
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void speedUp(double s){
speed+=s;
}
public void speedDown(double s){
speed-=s;
}
public static void main(String[] args) {
Vehicle v = new Vehicle();
v.setType("黑色捷达");
v.setSpeed(5);
System.out.println("一辆"+v.getType()+"正以每小时"+v.getSpeed()+"千米速度过来");
v.speedUp(5);
System.out.println("一辆"+v.getType()+"正以每小时"+v.getSpeed()+"千米速度过来");
v.speedDown(8);
System.out.println("一辆"+v.getType()+"正以每小时"+v.getSpeed()+"千米速度过来");
}
}
返回值类型:
返回值:只是从方法体内向方法体外返回的数据内容
返回值类型:返回值的数据类型
要返回数据内容,可以使用”return“关键字,将其返回并结束
形参列表:
形式参数:是指从方法体外向内传递数据内容
形参列表:形式参数可以有多个
alt + insert快捷键定义方法
Random
import java.util.Random
用法与Scanner相似
//创建一个随机数对象
Random ra = new Random();
ra.nextInt(17)://0-16的随机数
import java.util.Random;
public class DoubleColorBall {
public static void main(String[] args) {
//声明一个int类型长度为7的一维数组,并且使用动态方式声明
int[] arr = new int[7];
//创建一个随机数对象
Random ra = new Random();
//开始摇号(向数组元素中添加值),需要先摇6个红球,范围1-33
for(int i = 0; i < 6; i++){
//摇红球
arr[i] = ra.nextInt(33) + 1;
for(int j = i-1;j >= 0;j--){
if(arr[i] == arr[j]){
i--;
break;
}
}
}
//蓝色球
arr[arr.length-1] =ra.nextInt(17) + 1;
//遍历数组
System.out.println("本期中奖结果:");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
this关键字(原理、理解)
基本概念
在构造方法中和成员方法中访问成员变量时,编译器会加上this.的前缀,而this.相当于汉语中"我的",当不同的对象调用同一个方法时,由于调用方法的对象不同导致this关键字不同,从而this.方式访问的结果也就随之不同。
使用方式
(1)当形参变量名与成员变量名相同时,在方法体中会优先使用形参变量(就近原则),若希望使用 成员变量,则需要在成员变量的前面加上this.的前缀,明确要求该变量是成员变量。 (2)在构造方法的第一行可以使用this()的方式来调用本类中的其它构造方法(了解)。
public class Girl01 {
String name;
int age;
boolean have;
public void setnah(String name,int age,boolean have){
this.name = name;
this.age = age;
this.have = have;
}
public String show(){
return "姓名"+name+",年龄"+age+",是否有男朋友:"+have;
}
public static void main(String[] args) {
Girl01 g = new Girl01();
g.setnah("zh",18,true);
System.out.println(g.show());
}
}
封装
基本概念
通常情况下可以在测试类给成员变量赋值一些合法但不合理的数值,无论是编译阶段还是运行阶段都不会报错或者给出提示,此时与现实生活不符。为了避免上述错误的发生,就需要对成员变量进行密封包装处理,来隐藏成员变量的细节以及保证成员变量数值的合理性,该机制就叫做封装。
实现流程
(1)私有化成员变量,使用private关键字修饰;
(2)提供公有的get和set方法,并在方法体中进行合理值的判断;
(3)在构造方法中调用set方法进行合理值的判断;
实体类的封装:
在项目开发中,通常com.XXXX.bean;com.XXXX.domain;com.XXXX.entity;com.XXXX.pojo
这些包当中通常情况下存放的都是实体类。
实体类封装的步骤:
1、私有化成员变量;
2、提供公有的get/set方法
3、提供无参/有参/全参的构造方法
4、重写toString()、equals和hashCode()
import java.util.Objects;
public class Edu {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getStudy() {
return study;
}
public void setStudy(String study) {
this.study = study;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Edu getNext() {
return next;
}
public void setNext(Edu next) {
this.next = next;
}
private int id;
private int userId;
private String start;
private String end;
private String school;
private String study;
private String description;
private Edu next;
public Edu(int userId, String start, String end, String school, String study, String description) {
this.userId = userId;
this.start = start;
this.end = end;
this.school = school;
this.study = study;
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Edu edu = (Edu) o;
return id == edu.id &&
userId == edu.userId &&
Objects.equals(start, edu.start) &&
Objects.equals(end, edu.end) &&
Objects.equals(school, edu.school) &&
Objects.equals(study, edu.study) &&
Objects.equals(description, edu.description) &&
Objects.equals(next, edu.next);
}
@Override
public int hashCode() {
return Objects.hash(id, userId, start, end, school, study, description, next);
}
@Override
public String toString() {
return "Edu{" +
"id=" + id +
", userId=" + userId +
", start='" + start + '\'' +
", end='" + end + '\'' +
", school='" + school + '\'' +
", study='" + study + '\'' +
", description='" + description + '\'' +
", next=" + next +
'}';
}
public Edu(int id, int userId, String start, String end, String school, String study, String description, Edu next) {
this.id = id;
this.userId = userId;
this.start = start;
this.end = end;
this.school = school;
this.study = study;
this.description = description;
this.next = next;
}
public Edu() {
}
public static void main(String[] args) {
Edu edu = new Edu(1,"","","","","");
Edu edu1 = new Edu(1,2,"","","","","",edu);
System.out.println(edu1.toString());
}
}
import java.util.Objects;
public class Work {
private int id;
private int userId;
private String start;
private String end;
private String company;
private String job;
private String description;
Work 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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Work getNext() {
return next;
}
public void setNext(Work next) {
this.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);
}
@Override
public String toString() {
return "Work{" +
"id=" + id +
", userId=" + userId +
", start='" + start + '\'' +
", end='" + end + '\'' +
", company='" + company + '\'' +
", job='" + job + '\'' +
", description='" + description + '\'' +
", next=" + next +
'}';
}
public Work(int id, int userId, String start, String end, String company, String job, String description, Work next) {
this.id = id;
this.userId = userId;
this.start = start;
this.end = end;
this.company = company;
this.job = job;
this.description = description;
this.next = next;
}
public Work() {
}
public static void main(String[] args) {
Work work = new Work();
Work work1 = new Work(1,2,"","","","","",work);
System.out.println(work1.toString());
}
}
import java.util.Objects;
public class User {
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setCity(String city) {
this.city = city;
}
public void setAddress(String address) {
this.address = address;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setWeixin(String weixin) {
this.weixin = weixin;
}
public void setQq(String qq) {
this.qq = qq;
}
public void setWeibo(String weibo) {
this.weibo = weibo;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setDescription(String description) {
this.description = description;
}
//用户编号
private int id;
//用户姓名
private String name;
//年龄
private int age;
//城市
private String city;
//详细住址
private String address;
//邮箱
private String email;
//电话
private String phone;
//微信
private String weixin;
//qq
private String qq;
//微博
private String weibo;
//性别
private String sex;
//简介
private String description;
public User(String name, int age, String city, String address, String email, String phone, String weixin, String qq, String weibo, String sex, String description) {
this.name = name;
this.age = age;
this.city = city;
this.address = address;
this.email = email;
this.phone = phone;
this.weixin = weixin;
this.qq = qq;
this.weibo = weibo;
this.sex = sex;
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCity() {
return city;
}
public String getAddress() {
return address;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getWeixin() {
return weixin;
}
public String getQq() {
return qq;
}
public String getWeibo() {
return weibo;
}
public String getSex() {
return sex;
}
public String getDescription() {
return description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id &&
age == user.age &&
Objects.equals(name, user.name) &&
Objects.equals(city, user.city) &&
Objects.equals(address, user.address) &&
Objects.equals(email, user.email) &&
Objects.equals(phone, user.phone) &&
Objects.equals(weixin, user.weixin) &&
Objects.equals(qq, user.qq) &&
Objects.equals(weibo, user.weibo) &&
Objects.equals(sex, user.sex) &&
Objects.equals(description, user.description);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age, city, address, email, phone, weixin, qq, weibo, sex, description);
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", city='" + city + '\'' +
", address='" + address + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", weixin='" + weixin + '\'' +
", qq='" + qq + '\'' +
", weibo='" + weibo + '\'' +
", sex='" + sex + '\'' +
", description='" + description + '\'' +
'}';
}
public User() {
}
public User(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.name = name;
this.age = age;
this.city = city;
this.address = address;
this.email = email;
this.phone = phone;
this.weixin = weixin;
this.qq = qq;
this.weibo = weibo;
this.sex = sex;
this.description = description;
}
public static void main(String[] args) {
User user = new User(1,"",20,"山西","太原","","","","","","","");
System.out.println(user.toString());
}
}
import java.util.Objects;
public class Specialty {
private int id;
private int userId;
private String name;
private String description;
private Specialty next;
public Specialty(int id, int userId, String name, String description) {
this.id = id;
this.userId = userId;
this.name = name;
this.description = description;
}
public Specialty(int userId, String name, String description) {
this.userId = userId;
this.name = name;
this.description = description;
}
@Override
public String toString() {
return "Specialty{" +
"id=" + id +
", userId=" + userId +
", name='" + name + '\'' +
", description='" + description + '\'' +
", next=" + next +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Specialty specialty = (Specialty) o;
return id == specialty.id &&
userId == specialty.userId &&
Objects.equals(name, specialty.name) &&
Objects.equals(description, specialty.description) &&
Objects.equals(next, specialty.next);
}
@Override
public int hashCode() {
return Objects.hash(id, userId, name, description, next);
}
public Specialty(int id, int userId, String name, String description, Specialty next) {
this.id = id;
this.userId = userId;
this.name = name;
this.description = description;
this.next = next;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Specialty getNext() {
return next;
}
public void setNext(Specialty next) {
this.next = next;
}
public Specialty() {
}
public static void main(String[] args) {
Specialty sp = new Specialty(1,2,"","");
Specialty sp1 = new Specialty(2,3,"","",sp);
System.out.println(sp1.toString());
}
}
import java.util.Objects;
public class Skill {
private int id;
private int userId;
private String keyWords;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getKeyWords() {
return keyWords;
}
public void setKeyWords(String keyWords) {
this.keyWords = keyWords;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Skill skill = (Skill) o;
return id == skill.id &&
userId == skill.userId &&
Objects.equals(keyWords, skill.keyWords);
}
@Override
public int hashCode() {
return Objects.hash(id, userId, keyWords);
}
@Override
public String toString() {
return "Skill{" +
"id=" + id +
", userId=" + userId +
", keyWords='" + keyWords + '\'' +
'}';
}
public Skill(int userId, String keyWords) {
this.userId = userId;
this.keyWords = keyWords;
}
public Skill(int id, int userId, String keyWords) {
this.id = id;
this.userId = userId;
this.keyWords = keyWords;
}
public Skill() {
}
public static void main(String[] args) {
Skill skill = new Skill(1,2,"");
System.out.println(skill.toString());
}
}
import java.util.Objects;
public class Info {
private User user;
private Edu edu;
private Skill skill;
private Work work;
private Specialty specialty;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Info info = (Info) o;
return Objects.equals(user, info.user) &&
Objects.equals(edu, info.edu) &&
Objects.equals(skill, info.skill) &&
Objects.equals(work, info.work) &&
Objects.equals(specialty, info.specialty);
}
@Override
public int hashCode() {
return Objects.hash(user, edu, skill, work, specialty);
}
@Override
public String toString() {
return "Info{" +
"user=" + user +
", edu=" + edu +
", skill=" + skill +
", work=" + work +
", specialty=" + specialty +
'}';
}
public Info(User user, Edu edu, Skill skill, Work work, Specialty specialty) {
this.user = user;
this.edu = edu;
this.skill = skill;
this.work = work;
this.specialty = specialty;
}
public Info() {
}
public static void main(String[] args) {
User user = new User();
Edu edu = new Edu();
Skill skill = new Skill();
Work work = new Work();
Specialty specialty = new Specialty();
Info info = new Info(user,edu,skill,work,specialty);
System.out.println(info.toString());
}
}
import jdk.nashorn.internal.runtime.JSONFunctions;
import java.util.Objects;
public class Result {
private int status;
private String msg;
private Object data;
public Result(int status, String msg) {
this.status = status;
this.msg = msg;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Result result = (Result) o;
return status == result.status &&
Objects.equals(msg, result.msg) &&
Objects.equals(data, result.data);
}
@Override
public int hashCode() {
return Objects.hash(status, msg, data);
}
public Result(int status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public Result() {
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static void main(String[] args) {
Result re = new Result(1,"", 1);
System.out.println(re.toString());
}
}