java从入门到入土第9天

Day09

package com.qf.test01;

public class Person {
	
	//属性
	String name;
	char sex;
	int age;
	
	//构造方法 -- 无参构造
	public Person(){
		System.out.println("Person的无参构造");
		this.name = "彭于晏";
		this.sex = '男';
		this.age = 43;
	}
	
	//构造方法 -- 有参构造
	public Person(String name,char sex,int age){
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	
	//成员方法
	public void eat(){
		System.out.println(this.name + "吃饭饭");
	}
	
	public void sleep(){
		System.out.println(this.name + "睡觉觉");
	}
	
	//静态方法
	public static void method(){
		System.out.println("静态方法");
	}
}

package com.qf.test01;

public class Test01 {
	/**
	 * 知识点:构造方法/构造器
	 * 
	 * 含义:与类名相同,且没有返回项的方法
	 * 作用:
	 * 		1.和new关键字一起表示创建对象
	 * 		2.初始化数据
	 * 
	 * 注意:
	 * 		1.如果写了有参构造,系统不会默认实现无参构造
	 * 		2.构造方法可以重载
	 * 
	 * 经验:写了有参构造,自己把无参构造写上
	 * 	
	 */
	public static void main(String[] args) {
		
//		Person p = new Person();
//		p.name = "侯小康";
//		p.sex = '男';
//		p.age = 23;
		
		Person p = new Person("侯小康", '男', 23);
		
		System.out.println(p.name);
		System.out.println(p.sex);
		System.out.println(p.age);
		
		p.eat();
		p.sleep();
	}
}

package com.qf.test02;

public class Person {
	
	//属性
	String name;
	char sex;
	int age;
	
	//构造方法 -- 无参构造
	public Person(){
		//this():在构造方法的第一句调用另外一个构造方法
		this("彭于晏", '男', 46);
		System.out.println("Person的无参构造");
	}
	
	//构造方法 -- 有参构造
	public Person(String name,char sex,int age){
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	
	//成员方法
	public void eat(){
		
		//this.属性:调用本对象的成员属性
		System.out.println(this.name + "吃饭饭");
	}
	
	public void sleep(){
		//this.属性:调用本对象的成员属性
		System.out.println(this.name + "睡觉觉");
	}
	
	public void writerCode(){
		//this.属性:调用本对象的成员属性
		System.out.println(this.name + "写代码");
	}
	
	public void study(){
		//this.方法:调用本对象的成员方法
		this.eat();
		this.writerCode();
		this.sleep();
	}
	
	//静态方法
	public static void method(){
		System.out.println("静态方法");
	}
}

package com.qf.test02;

public class Test01 {
	/**
	 * 知识点:this
	 * 含义:this表示调用该方法的对象
	 * 	
	 * 作用:
	 * 		1.this.属性:调用本对象的成员属性
	 * 		2.this.方法:调用本对象的成员方法
	 * 		3.this():在构造方法的第一句调用另外一个构造方法
	 */
	public static void main(String[] args) {
		
		Person p = new Person();
		
		p.study();
	}
}

package com.qf.test03;

public class A {

	private String attr = "A类的私有化属性";
	
	private void method01(){
		
		System.out.println(this.attr);
		System.out.println("A类的私有化方法");
	}
	
	public void method02(){
		method01();
	}
	
}

package com.qf.test03;

public class Test01 {
	/**
	 * 知识点:private
	 * 理解:private是访问修饰符的一种,表示私有
	 * 扩展:访问修饰符可以修饰属性和方法
	 * 
	 * 作用:
	 * 		1.修饰属性:该属性不能在类的外面使用
	 * 		2.修饰方法:该方法不能在类的外面使用
	 * 
	 * 应用场景:
	 * 		1.不想让外界直接调用的属性就使用private修饰
	 * 		2.不想让外界直接调用的方法就使用private修饰
	 */
	public static void main(String[] args) {
		
		A a = new A();
		
		a.method02();
	}
}

package com.qf.test04;

import java.time.LocalDateTime;

public class User {

	private String username;
	private String password;
	private double money;
	
	public User() {
	}

	public User(String username, String password, double money) {
		this.username = username;
		this.password = password;
		this.money = money;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setMoney(double money){
		//额外的功能
		double num = money - this.money;
		System.out.println(LocalDateTime.now() + "设置了金额:" + num);
		
		this.money = money;
	}
	
	public double getMoney(){
		return this.money;
	}
}

package com.qf.test04;

public class Test01 {
	/**
	 * 知识点:封装
	 * 概念:将属性封装到对象的内部,通过get/set方法去操作属性
	 * 步骤:
	 * 		1.私有化属性
	 * 		2.添加get(获取)/set(设置)方法
	 * 好处:
	 * 		将属性封装到对象的内部,外界不能直接操作属性
	 * 		必须通过get/set方法去操作属性
	 * 		可以在get/set方法中做额外的功能
	 * 经验:属性都必须封装
	 * 
	 * 
	 * 需求:模拟银行用户操作金额的过程
	 * 分析:怎么操作金额?
	 * 		1.设置金额
	 * 		2.获取金额
	 * 
	 *  经验:操作属性 --> 设置和获取		
	 *
	 */
	public static void main(String[] args) {
		
		User user = new User("1445584980", "123123", 2000);
		
		//获取金额 -- 2000 --> 获取金额
		//减200  --- 1800
		//将1800赋值给金额属性 --> 设置金额
//		user.money = user.money - 200;
//		System.out.println(user.money);//1800
		
		user.setMoney(user.getMoney() - 200);
		System.out.println(user.getMoney());
	}
}

 package com.qf.test;

public class Gobang {

	//棋盘长度
	private int length = 20;
	//棋盘容器
	private String[][] gobang = new String[length][length];
	//棋盘坐标
	private String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
	//棋盘符号
	private String add = "┼";
	private String black = "●";
	private String white = "○";
	
	public Gobang() {
		this.init();
		this.printGobang();
	}
	
	//初始化棋盘
	private void init(){
		for (int i = 0; i < gobang.length; i++) {
			for (int j = 0; j < gobang[i].length; j++) {
				if(j == length-1){//每行的一列,设置行数

					gobang[i][j] = nums[i];
					
				}else if(i == length-1){//最后一行,设置列数
					
					gobang[i][j] = nums[j];
					
				}else{
					gobang[i][j] = add;
				}
			}
		}
	}
	
	//打印棋盘
	public void printGobang(){
		for (String[] strings : gobang) {
			for (String str : strings) {
				System.out.print(str);
			}
			System.out.println();
		}
	}
	
	//判断坐标是否在棋盘内
	public boolean isIndexOutOfGobang(int x,int y){
		if(x<0 || x>length-2 || y<0 || y>length-2){
			return false;
		}
		return true;
	}
	
	//判断坐标上是否有棋子
	public boolean isPiece(int x,int y){
		if(!gobang[x][y].equals(add)){
			return false;
		}
		return true;
	}
	
	//落子
	public int play(int x,int y,boolean flag){
		
		if(!isIndexOutOfGobang(x, y)){
			return -1;
		}
		
		if(!isPiece(x, y)){
			return -2;
		}
		
		String piece = (flag)?black:white;
		gobang[x][y] = piece;
		return 1;
	}
	
}

package com.qf.test;

import java.util.Scanner;

public class Test {

	/**
	 * 知识点:面向对象版五子棋
	 */
	public static void main(String[] args) {
		
		Gobang gobang = new Gobang();
		
		Scanner scan = new Scanner(System.in);
		boolean flag = true;//true-黑子  false-白子
		
		while(true){
			
			//输入坐标
			System.out.println("请" + ((flag)?"黑":"白") + "子输入坐标:");
			int x = scan.nextInt() - 1;//-1是因为用户看到的界面是坐标(从1开始),数组是下标(从0开始)
			int y = scan.nextInt() - 1;//-1是因为用户看到的界面是坐标(从1开始),数组是下标(从0开始)
			
			//落子
			int play = gobang.play(x, y, flag);
			if(play == -1){
				System.out.println("坐标错误 - 坐标超出棋盘范围,请重新输入");
				continue;
			}else if(play == -2){
				System.out.println("坐标错误 - 坐标上已有棋子,请重新输入");
				continue;
			}
			
			//打印棋盘
			gobang.printGobang();
			
			//置反
			flag = !flag;
		}		
	
		
		
	}
}

package com.qf.test05;

/**
 * 编写一个类的步骤:
 *		1.属性
 *		2.私有化属性
 *		3.构造方法 - 无参构造、有参构造
 *		4.get/set的
 *		5.独有的方法 -- eat()、sleep()...
 */
public class Person {

	private String name;
	private char sex;
	private int age;
	
	static String star = "星球";
	
	public Person() {
	}

	public Person(String name, char sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public void eat(){
		System.out.println(this.name + "吃饭饭");
	}
	
	public void sleep(){
		System.out.println(this.name + "睡觉觉");
	}
}

package com.qf.test05;

public class Test01 {
	/**
	 * 知识点:分包
	 * 理解:最基本的功能就是防止类的重名
	 * 项目中的功能:做类的分类(一个项目有成千上万的类,需要分包做管理)
	 * 		com.qf.utils/tools -- 工具类的包
	 * 		com.qf.pojo/entity/vo -- 实体类的包
	 * 		com.qf.map/mapper -- 操作数据库类的包
	 * 		com.qf.biz/service -- 操作业务类的包
	 * 命名规范:网络倒序
	 * 		com/net/cn.公司名.项目名/功能名
	 * 
	 * 知识点:static
	 * 理解:静态的
	 * 作用:
	 * 		1.静态属性
	 * 		2.静态方法
	 * 		3.静态代码块
	 * 
	 * 知识点:static修饰属性
	 */
	public static void main(String[] args) {
		
		Person p1 = new Person();
		Person p2 = new Person();
		
		p1.setName("侯小康");
		p2.setName("波多野结衣");
		System.out.println(p1.getName());//侯小康
		System.out.println(p2.getName());//波多野结衣
		
//		p1.star = "地球";
//		p2.star = "火星";
//		System.out.println(p1.star);
//		System.out.println(p2.star);
		
		//注意:静态属性属于每个对象共享的属性,直接使用类名调用
		Person.star = "地球";
		System.out.println(Person.star);
	}
}

package com.qf.test06;

import com.qf.array.MyArrays;

public class Test01 {
	/**
	 * 知识点:static修饰方法
	 * 应用场景:工具类(该类中的方法都是静态的)
	 */
	public static void main(String[] args) {
		
		int[] arr = {5,72,9,4,21,1,6,3,81};	
		
		//排序 - 1,3,4,5,6,9,21,72,81
		MyArrays.sort(arr);
		
		//查找
		//返回值规则:搜索键的索引,如果它包含在数组中; 否则, (-(insertion point) - 1) 
		//返回值规则:如果元素包含在数组中,返回下标;否则,(-(插入点) - 1) 
		int index = MyArrays.binarySearch(arr,30);
		System.out.println("查找到元素的下标为:" + index);
		
		//拷贝数组 - [1, 3, 4, 5, 6, 9, 21, 72, 81, 0, 0, 0, 0, 0, 0]
		int[] newArr1 = MyArrays.copyOf(arr,15);
		
		//拷贝数组区间 - [4, 5, 6, 9, 21, 72, 81, 0, 0]
		int[] newArr2 = MyArrays.copyOfRange(newArr1,2,11);//(目标数组,开始下标-包含,结束下标-排他)
		
		//替换所有元素 - [888, 888, 888, 888, 888, 888, 888, 888, 888]
		MyArrays.fill(newArr2,888);
		
		//替换区间元素
		MyArrays.fill(newArr2,2,4,666);//(目标数组,开始下标-包含,结束下标-排他,替换元素)
	
		//将数组转换为字符串
		String str = MyArrays.toString(newArr2);
		System.out.println(str);
	}
}

package com.qf.test07;

public class A {
	//成员属性
	String str1;
	
	//静态属性
	static String str2;
	
	//静态代码块:类加载到方法区时调用,可以初始化静态属性
	static{
		str2 = "ccc";//A.str2 = "bbb";
		System.out.println("A类的静态代码块:" + str2);
	}
	
	//代码块:创建对象时优先于构造方法调用,可以初始化成员属性和静态属性
	{
		str1 = "bbb";//this.str1 = "bbb";
		str2 = "bbb";//A.str2 = "bbb";
		System.out.println("A类的代码块:" + str1 + " -- " + str2);
	}
	
	//构造方法:创建对象时调用,可以初始化成员属性和静态属性
	public A() {
		str1 = "aaa";//this.str1 = "aaa";
		str2 = "aaa";//A.str2 = "aaa";
		System.out.println("A类的构造方法:" + str1 + " -- " + str2);
	}
}

package com.qf.test07;

public class Test01 {
	/**
	 * 知识点:静态代码块
	 * 
	 * 经验:
	 * 		初始化成员属性一般在构造方法中进行
	 * 		初始化静态属性一般在静态代码块中进行
	 * 
	 */
	public static void main(String[] args) {
		
		A a1 = new A();
		A a2 = new A();
		
	}
}

package com.qf.array;

/**
 * 世上最牛逼的数组工具类
 * @author 侯小康
 * @version 1.0
 */
public class MyArrays {

	/**
	 * 数组的排序
	 * @param arr 目标数组
	 */
	public static void sort(int[] arr){
		for (int i = 0; i < arr.length-1; i++) {
			for (int j = 0; j < arr.length-1-i; j++) {
				if(arr[j]>arr[j+1]){
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
	}
	
	/**
	 * 数组的查找
	 * @param arr 目标数组
	 * @param key 要搜索的键
	 * @return 如果元素包含在数组中,返回下标;否则,(-(插入点) - 1) 
	 */
	public static int binarySearch(int[] arr,int key){
		int start = 0;
		int end = arr.length-1;
		while(start <= end){
			int mid = (start+end)/2;
			if(key < arr[mid]){
				end = mid-1;
			}else if(key > arr[mid]){
				start = mid+1;
			}else{
				return mid;
			}
		}
		return -start-1;
	}
	
	/**
	 * 拷贝数组
	 * @param arr 目标数组
	 * @param newLength 拷贝长度
	 * @return 新数组
	 */
	public static int[] copyOf(int[] arr,int newLength){
		
		int copyLen = arr.length;
		if(copyLen > newLength){
			copyLen = newLength;
		}
		
		int[] newArr = new int[newLength];
		
		for (int i = 0; i < copyLen; i++) {
			newArr[i] = arr[i];
		}
		return newArr;
	}
	
	/**
	 * 拷贝区间数组
	 * @param arr 目标数组
	 * @param start 开始下标-包含
	 * @param end 结束下标-排他
	 * @return 新数组
	 */
	public static int[] copyOfRange(int[] arr,int start,int end){
		
		int length = end-start;
		int[] newArr = new int[length];
		
		int index = 0;
		for (int i = start; i < end; i++) {
			newArr[index++] = arr[i];
		}
		return newArr;
	}
	
	/**
	 * 替换区间元素
	 * @param arr 目标数组
	 *  @param start 开始下标-包含
	 * @param end 结束下标-排他
	 * @param key 需要替换的键
	 */
	public static void fill(int[] arr,int start,int end,int key){
		for (int i = start; i < end; i++) {
			arr[i] = key;
		}
	}
	
	/**
	 * 替换全部元素
	 * @param arr 目标数组
	 * @param key 需要替换的键
	 */
	public static void fill(int[] arr,int key){
		fill(arr, 0, arr.length, key);
	}
	
	/**
	 * 获取数组的字符串表示
	 * @param arr 目标数组
	 * @return 数组字符串
	 */
	public static String toString(int[] arr){
		
		String str = "[";
		
		for (int element : arr) {
			if(str.length() != 1){
				str += ",";
			}
			str += element;
		}
		str += "]";
		return str;
	}
}

1.构造方法(概念、内存图)

2.this
this.属性
this.方法
this()

3.private
修饰属性、修饰方法

4.封装
1.私有化属性
2.get/set方法

5.面向对象版本的五子棋

6.static修饰属性、修饰方法、静态代码块

作业:
1.知识点梳理文档 – 手写
2.代码编写三遍
3.画内存图
4.课后作业:小明把大象装进冰箱
5.复习:之前所有的知识点
6.预习:继承、重写、super、抽象类及抽象方法

提升作业:把面向对象版本的五子棋判断输赢实现!!!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值