java学习-day06-数组(二) 面向对象(一)-类的创建

数组的遍历

输出每个月的天数
public class Test1{
	public static void main(String[] args){
		method();
	}
	public static void method(){
		int[] a =  {31,28,31,30,31,30,31,31,30,31,30,31};
		for(int i = 0; i < a.length; i++){
			System.out.println(i+1+"月有"+a[i]+"天");
		}
	}
}
遍历数组,存入 1 到 10

注: 打印数组名(字符数组名除外,打印字符数组名可得到字符数组内容)得到的结果是数组的地址值.

public class Test2{
	public static void main(String[] args){
		method();
	}
	public static void method(){
		int[] a = new int[10];
		for(int i = 0;i < a.length;i++){
			a[i] = i + 1;
		}
		System.out.println(a); //[I@1edf1c96 打印出数组名地址
		// 遍历取出数组内容
		for(int i = 0;i < a.length; i++){
			System.out.print(a[i]+" "); // 1 2 3 4 5 6 7 8 9 10
		}
	}
}
创建随机数组
public class Test3{
	public static void main(String[] args){
		int num = 5;//  记录随机数组个数
		method(num);
	}
	public static void method(int num){
		int[] a = new int[num];
		for(int i = 0; i < a.length;i++){
			a[i] = new Random().nextInt(100); //产生 0 - 100 的随机整数
		}
		for(int i = 0;i < a.length;i++){
			System.out.print(a[i]+" ");
		}
		
	}
}

数组工具类 Arrays

介绍数组工具类 Arrays 三种常用的方法 toString() sort(), copyOf();

toString() 显示数组中的数据
import java.util.Arrays;
public class Test4 {
	public static void main(String[] args) {
		method1();
	}
	public static void method1(){
		double[] a = {1.1,2.2,3.3,4.4,5.5};
		System.out.println(Arrays.toString(a));
	}
}
sort() 对数组进行排序
import java.util.Arrays;
public class Test5 {
	public static void main(String[] args) {
		method2();
	}
	public static void method2(){
		int[] a = {34,24,54,23,23,0,12,2,5,7,35};
		Arrays.sort(a);
		System.out.println(Arrays.toString(a)); // [0, 2, 5, 7, 12, 23, 23, 24, 34, 35, 54]
	}
}
copyOf() 对数组进行复制
import java.util.Arrays;
public class Test6 {
	public static void main(String[] args) {
		method3();
	}
	public static void method3(){
		int[] old = {1,2,3,4,5};
		int[] now = Arrays.copyOf(old, 8);
		System.out.println(Arrays.toString(now));//[1, 2, 3, 4, 5, 0, 0, 0]
		
		int[] now2 = Arrays.copyOf(old, 3);
		System.out.println(Arrays.toString(now2)); // [1, 2, 3]
	}
}

面向对象OOP

三大特性: 1 封装 : 把相关数据封装成"类"的组件
2 继承: 子类自动共享父类的属性和方法,是类之间的一种关系
3 多态 增强软件的灵活性和重用性

类的创建及对象的使用

注: 一个.java文件中,可以包含多个类,但是只能有一个类被public修饰,而且这个public的类名就是.java文件名

public class Test7 {
	public static void main(String[] args) {
		Phone phone = new Phone();
		phone.call(); // 打电话...
		phone.message(); //发信息...
		phone.music();  //听音乐...
		
		phone.color = "red";
		phone.pinpai = "xiaomi";
		phone.size = 7;
		phone.price = 3599;
		
		System.out.println(phone.color); // red
		System.out.println(phone.pinpai);// xiaomi
		System.out.println(phone.size);// 7
		System.out.println(phone.price);// 3599.0
	}
}
class Phone{
	String color;
	String pinpai;
	double price;
	int size;
	
	public void call(){
		System.out.println("打电话...");
	} 
	public void message(){
		System.out.println("发信息...");
	}
	public void music(){
		System.out.println("听音乐...");
	}
}

对象内存管理

JAVA内存共分为五大区域,在此重点关注 堆 和 栈
以下为 类 创建 对象 时的内存管理
在这里插入图片描述

发布了15 篇原创文章 · 获赞 9 · 访问量 273

猜你喜欢

转载自blog.csdn.net/qq_34681728/article/details/105371034