java学习 day2

打印三角形

public class TestDemo {
	public static void main(String[] args) {
		for(int i = 1; i <= 5; i++) {
			for(int j = 5; j >= i; j--) {
				Syetem.out.print(" ")
			}
			for(int j = 1; j <= i; j++) {
				Syetem.out.print("*")
			}
			for(int j = 1; j = i; j++) {
				Syetem.out.print("*")
			}
		}
	}

}

方法的定义

  1. 何谓方法
public class Demo01 {
	public static void main(String[] args) {
		// 实际参数:实际调用传递给它的参数
		int sum = add(1, 2);
		System.out.println(sum);
		test()}
	// 加法
	// 形式参数:用来定义方法作用的
	public int add(int a, int b) {
		return a + b;
	}
	// 无输入,无输出
	public static void test() {
		// 这里输入方法定义
	}
}
  1. 方法的定义以及调用
  • 当方法有返回值时,方法调用通常被当作一个值,例如

int larger = max(30, 40);

  • 如果方法返回值是void,方法调用一定是一条语句,例如

System.out.println(“Hello world!”);

public class Demo02 {
	public static void main(String[] args) {
		
	}
	// 比大小
	public static int max(int num1, int num2) {
		int result = 0;
		// int result = num1 > nuum2 ? num1 : num2;
		if(num1 == num2) {
			System.out.println("num1 == num2");
			return 0;
		}
		if(num1 > num2) {
			result = num1;
		}else {
			result = num2;
		}
		return result;
	}

}
  1. 方法重载
  2. 相同的名字,里面参数定义不同是没问题的,即形参不同。

public static int max(int num1, int num2)
public static double max(double num1, double num2)
public static int max(int num1, int num2, int num3)

  1. 命令行传参

  2. 可变参数

Alt+Enter 可以自动补全

  1. 可变参数本身就是个数组,传参时可以直接给个数组
public class Demo04 {
	public static void main(String[] args) {
		Demo04 demo04 = new Demo04();
		demo04.test(1, 2, 3, 4, 5);
	}
	// public void test(int x, double... i) {
	public void test(int... i) {
		System.out.println(i[0]);
	}
}
  1. 递归
public class Demo06 {
	public static void main(String[] args) {
		System.out.println(f(2));
	}
	public static int f(int n) {
		if(n == 1) {
			return 1;
		}else {
			return n * f(n - 1);
		}
	}
	
	
}

数组

  1. 数组的创建

int[] man = new int[10];

  • 数组是相同类型的;
  • 数组也是对象。数组元素相当于对象的成员变量;
  • 数组长度是确定的,是不可变的。
  1. 数组的使用
public class Demo05 {
	public static void main(String[] args) {
		int[] arrays = {1, 2, 3, 4, 5};
		// 打印全部数组元素
		for(int i = 0; i < arrays.length; i++) {
			System.out.println(arrays[i]);
		}
		// 计算所有元素的和
		int sum = 0;
		for(int i = 0; i < arrays.length; i++) {
			sum += arrays[i];
		}
		System.out.println("sum=" + sum);

		// 查找最大元素
		int max = arrays[0];
		for(int i = 1; i < arrays.length; i++) {
			if(arrays[i] > max) {
				max = arrays[i];
			}
		}
		System.out.println("max=" + max);

		// 输出反转数组
		int[] reverse = new reverse(arrays);
		printArrays(reverse);
		
	}

	// 反转数组
	public static int[] reverse(int[] arrays) {
		int[] result = new int[arrays.length];

		// 反转的操作
		for(int i = 0, j = result.length - 1; i < arrays.length; i++, j--) {
			result[j] = arrays[i];
		]
	}

	// 打印数组元素
	public static void printArray(int[] arrays) {
		for(int i = 0; i < arrays.length; i++) {
			System.out.println(arrays[i] + " ");
		}
	}
}
  1. 二维数组
int[][] arrays = new int[10][10]; 
  1. 打印数组
import java.util.Arrays;
public class ArrayDemo06 {
	public static void main(String[] args) {
		int[] a = {1, 2, 3, 4, 9, 11, 555};
		System.out.println(a); // 输出的是Hash值
		// 打印数组元素Arrays.toString()
		System.out.println(Arrays.toString(0);
	}
	public void printArray(int[] a) {
		for(int i = 0; i < a.length; i++) {
			if(i == 0) {
				System.out.println("[");
			}
			if(i == a.length - 1) {
				System.out.pringln("]");
			}else{
				System.out.println(a[i] + ",");
			}
			
		]
	}
} 

冒泡排序

public class ArrayDemo07 {
	public static void main(String[] args) {
		int[] a = {1, 52, 423, 228, 54, 642, 77, 48, 98};
		int[] arraySort = sort(a);
		System.out.println(Arrays.toString(arraySort));
	}

	public static int[] sort(int[] array) {
		// 临时变量
		int temp = 0;
		// 外层循环, 判断我们要走多少次
		for(int i = 0; i < array.length - 1; i++) {
			for(int j = 0; j < array.length - j - 1; j++) {
				if(array[j + 1] > array[j]) {
					temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
		}
		return array;
	}
}

稀疏数组

需求:编写五子棋游戏中,有存盘退出和续上盘的功能
分析问题:因为该二维数组的很多值是默认值0,因此记录了很多没有意义的数据
解决:稀疏数组

public class ArrayDemo08 {
	public static void main(String[] args) {
		// 1.创建一个二维数组 11 * 11  0:没有棋子, 1:黑棋, 2:白棋
		int[][] array1 = new int[11][11];
		array1[1][2] = 1;
		array1[2][3] = 2;
		// 输出原始的数组
		System.out.println("输出原始的数组");
	
		for(int[] ints : array1) {
			for(int anInt : ints) {
				System.out.print(anInt + "\t");
			}
			System.out.println();
		}
		//==================================================
		// 转换为稀疏数组保存
		// 获取有效值的个数
		int sum = 0;
		for(int i = 0; i < 11; i++) {
			for(int j = 0; j < 11; j++) {
				if(array1[i][j] != 0) {
					sum++;
				}
			}
		}
		System.out.println("有效值的个数:" + sum);

		// 2.创建一个稀疏数组的数组
		int[][] array2 = new int[sum + 1][3];
	
		array2[0][0] = 11;
		array2[0][1] = 11;
		array2[0][2] = sum;
		
		// 遍历二维数组,将非零的值存放在稀疏数组中
		int count = 0;
		for(int i = 0; i < array1.length; i++) {
			for(int j = 0; j < array1[i].length; j++) {
				if(array1[i][j] != 0) {
					count++;
					array2[count][0] = i;
					array2[count][1] = j;
					array2[count][2] = array1[i][j];
				}
			}
		}
		// 输出稀疏数组
		System.out.println("稀疏数组");
		for(int i = 0; i < array2.length; i++) {
			System.out.println(array2[i][0] + "\t"
							+ array[i][1] + "\t"
							+ array[i][2]);
		}

		//=================================================
		System.out.println("还原")// 1.读取稀疏数组
		int[][] array3 = new int[array2[0][0]][array2[0][1]];
	
		// 1.给其中的元素还原它的值
		for(int i = 1; i < array2.length; i++) {
			array3[array2[i][0]][array2[i][1]] = array2[i][2]
		}
		// 3.打印
		System.out.println("输出还原的数组")for(int[] ints : array3) {
			for(int[] anInt : ints) {
				System.out.print(anInt + "\t");
			}
			System.out.println();
		}
	}
}

面向对象

  1. 什么是面向对象

本质是:以类的方式组织代码,以对象的方式组织(封装)数据。
三大特性:封装、继承、多态。

  1. 类和对象的关系
// 学生类
public class Student {
	String name; // null
	int age; // 0

	// 方法
	public void study() {
		System.out.println(this.name + "在学习");
	}
}
// 一个项目应该只存一个main方法
public class Application {
	public static void main(String[] args) {
		// 类是抽象的,需要实例化
		// 类实例化后会返回一个自己的对象
		// student对象就是一个Student类的具体实例
		Student xiaoming = new Student();
		Student xiaohong = new Student();

		xiaoming.name = "小明";
		xiaoming.age = 3;

		System.out.println(xiaoming.name);
		System.out.println(xiaoming.age);
	}
}
  1. 构造器
public class Person {
	String name;
	
	// 实例化初始值
	// 1.使用new关键字,本质是在调用构造器
	public Person() {
		
	}

	// 有参构造:一旦定义了有参构造,无参就必须显式定义
	public Person(String name) {
		this.name = name;
	}

	// Alt + Insert : idea快速生成构造器
	
}
public class Application {
	public static void main(String[] args) {
		Person person = new Person();
		System.out.println(person.name);
	}
}
  1. 封装
public class Application {
	public static void main(String[] args) {
		Student s1 = new Student();
		s1.setName("zyj");
		System.out.println(s1.getName());
	}
}

封装的好处:
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.统一接口
4.系统可维护性增加了

public class Student {
	// 类   private:私有

	// 属性私有
	private String name; // 姓名
	private int id; // 学号
	private char gender; // 性别
	private int age;

	// 提供一些可以操作这些私有属性的方法
	// 提供一些public的get、set方法
	// get获得这个数据
	public String getName() {
		return this.name;
	}
	// set给这个数据设置值
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return this.age;
	}
	// set给这个数据设置值
	public void setAge(int age) {
		if(age > 120 || age < 0) {
			age = 3;
		}else {
			this.age = age;
		}
}

get和set可以通过Alt+Insert直接快速生成

  1. 继承

extends

public class Application {
	public static void main(String[] args) {
		Student student = new Student();
		student.say();
		System.out.println(student.money);
		student.test("zzz");
	}
}
// Person 人:父类
public class Person {
	protected String name = "Zhai";
	public int monsey = 10_0000_0000;
	public void say() {
		System.out.println("说了一句话");
	}
}
// 学生 is 人: 派生类,子类
public class Student extends Person {
	private String name = "zyj";

	public void test(String name) {
		System.out.println(name); // 这里是main里面的zzz
		System.out.println(this.name); // 这里是本类里的zyj
		System.out.println(super.name); // 这里是父类Person里的Zhai
	}
}

Ctrl + H 可以显示继承的层级
super 是调用父类的

super注意点:

  • super调用父类的构造方法,必须在构造方法的第一个
  • super必须只能出现在子类的方法或者构造方法中!
  • super和this不能同时调用构造方法

VS this

  • 代表对象不同:
    this:本身调用者这个对象
    super:代表父类对象的应用
  • 前提
    this:没有继承也可以使用
    super:只能在继承条件才可以使用
  • 构造方法
    this():本类的构造
    super():父类的构造
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值