数组和面向对象
1、一维数组
变量:在Java程序中,当需要存储单个数据内容时,需要声明一个变量,声明变量的本质就是在内存空间中申请一块存储单元。
概念:在Java程序中,当需要存储多个数据类型相同的内容时,则需要声明一个数组。声明数组的本质,就是在内存中申请一块连续的存储单元。
声明变量:数据类型 变量名 = 初始值;
声明数组:数据类型[] 数组名 = new 数据类型[数组的长度];
例如:定义一个长度为3的int类型的一维数组
int[] arr = new int[3]; //动态定义数组
int[] arr1 = new int[]{1, 2, 3}; //静态定义数组
数组的初始化:
注意:在数组声明时,如果不给数组直接赋值,那么,byte、short、int、long类型的默认值为0,float、double类型的默认值为0.0,boolean类型的默认值为false,char类型的默认值为0或者\u0000(Unicode编码)
public class ArrayDemo01 {
public static void main(String[] args) {
int[] arr = new int[5];
System.out.println(arr[0]);
char[] brr = new char[5];
System.out.println(brr[0]);
if (brr[0] == 0) {
System.out.println("char型数组初始化值为0");
}
boolean[] crr = new boolean[5];
System.out.println(crr[0]);
}
}
public class ArrayDemo02 {
public static void main(String[] args) {
int[] arr = new int[20];
// System.out.println(arr[0]);
// System.out.println(arr[1]);
// System.out.println(arr[2]);
// System.out.println(arr[3]);
// System.out.println(arr[4]);
// System.out.println(arr[5]); //ArrayIndexOutOfBoundsException
// 数组的遍历
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
import java.util.Random;
/**
* @author zhangyutong
* @date 2024/01/24
* @description 双色球案例 共 7个号码,
* 其中红球 6个,范围 1-33,蓝球 1个,范围 1-17
*/
public class DoubleColorBall {
public static void main(String[] args) {
//声明一个int类型,长度为7的一维数组,动态
int[] arr = new int[7];
//创建一个随机数对象
Random random = new Random();
//开始摇号(向数组中添加值)
//先摇6个红球
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = random.nextInt(33) + 1; // 0+1 - 32+1 即1 - 33
//去重 有可能多个重复,不直接用 if
for (int j = i - 1; j >= 0; j--) {
if (arr[i] == arr[j]) {
//若号码出现重复,需要重新摇号
i--;
break;
}
}
}
//再摇1个蓝球
arr[arr.length - 1] = random.nextInt(17) + 1;
System.out.println("本期中将结果是:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
2、面向对象
面向对象的本质就是分析事物的特征(名词)和行为(动词)
面向对象编程就是先用面向对象的思想进行分析,再用面向对象的语言进行翻译的过程。
对象:在Java中,对象是指客观存在的实体
语法格式:
new 引用数据类型();
类:简单来说就是“分类”,是指具有相同特征和行为的多个事物共性的抽象,在Java中表现为一种引用数据类型,其中包含描述特征的成员变量和描述行为的成员方法。
语法格式:
[权限修饰符] class 类名 {
类体;
}
注意:通常情况下,类名的首字母要大写,当类名由多个单词构成时,每个单词的首字母都要大写(大驼峰命名法——UpperCamelCase)
成员变量(特征、属性)
语法格式:
[权限修饰符] 数据类型 成员变量名 (= 成员变量值);
注意:通常情况下,成员变量名的首字母要小写,当成员变量名由多个单词构成时,从第二个单词开始,首字母要大写(小驼峰命名法——lowerCamelCase)
引用:引用类型的成员变量名简称“引用”。
语法格式:
引用数据类型 引用变量名 = new 引用数据类型();
例:Person p = new Person();
注意:当一个类被定义完毕后,可以使用new关键字来创建该类的对象,这个过程被称为类的实例化。
类是对象的抽象,对象是类的实例。
/**
* @author zhangyutong
* @date 2024/01/24
* @description:
* 定义一个坐标类,特征有横坐标 x和纵坐标 y,
* 创建坐标类的对象,将对象的信息打印出来,修改特征信息,并再次打印。
*/
public class Point {
int x;
int y;
public static void main(String[] args) {
//创建一个 Point类型的引用 point,指向 Point类型的对象
Point point = new Point();
System.out.println("x = " + point.x + ", y = " + point.y);
point.x = 3;
point.y = 4;
System.out.println("x = " + point.x + ", y = " + point.y);
}
}
public class Phone {
String brand;
double price;
public void show() {
System.out.println("品牌:" + brand + ",价格:" + price);
}
public static void main(String[] args) {
Phone phone = new Phone();
phone.show();
phone.brand = "Nokia";
phone.price = 598.5;
phone.show();
}
}
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.show();
p.name = "zhangfei";
p.age = 30;
p.show();
}
}
成员方法 (行为)
注意:通常情况下,成员方法的首字母要小写,当成员方法名由多个单词构成时,从第二个单词开始,首字母要大写(小驼峰命名法——lowerCamelCase)
public class MethodDemo01 {
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
MethodDemo01 m = new MethodDemo01();
int sum = m.add(1, 2);
System.out.println("sum = " + sum);
}
}
public class Girl {
String name;
int age;
boolean bf;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setBf(boolean bf) {
this.bf = bf;
}
public boolean isBf() {
return bf;
}
public void show() {
System.out.println("我叫" + name + ",今年" + age + "岁,是否有男朋友:" + bf);
}
public static void main(String[] args) {
Girl g = new Girl();
g.show();
System.out.println("----------");
g.name = "貂蝉";
g.age = 18;
g.bf = true;
g.show();
System.out.println("----------");
g.setName("张三");
g.setAge(20);
g.setBf(false);
g.show();
}
}
3、封装
public class Person {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0 && age < 150) {
this.age = age;
} else {
this.age = 18;
System.out.println("输入的年龄有误!");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println("我叫" + name + ", 今年" + age + "岁!");
}
public static void main(String[] args) {
Person p = new Person();
p.show();
p.name = "zhangfei";
p.age = 30;
p.show();
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class PersonTest {
@Test
public void test01() {
Person person = new Person();
person.show();
// person.name = "张飞";
// person.age = 35;
person.setName("张飞");
person.setAge(-1);
person.show();
}
}