Android学习日记

2021.2.27 星期六

// TODO 2/27
// 标识符 字母数字下划线$组成 不能数字开头 区分大小写
// 关键字
// 变量 变量类型 变量名 驼峰法 变量值
// 类 Pascal 首字母大写
// 数据类型 数值 整数byte short int long 浮点float double 字符char  布尔boolean 类class 接口interface 数组
// 整型字面值 八进制 037 十六进制 0XA1
// 变量声明 int n;  整形变量 long count; 长整形
// 赋值 数据类型  变量名=变量值; int n=3;
// 浮点型 double 123.45d  float 23.4f
// double 是最大的类型
//package com.test;
//
//public class FloatDome {
   
//	public static void main(String[] args) {
   
//		//定义一个单精度浮点型变量,存放1234.328
//		float f=1234.328f;
//		System.out.println("f="+f);
//		//定义一个双精度浮点型变量,存放5623.465
//		double d=5623.464;
//		System.out.println("d="+d);
//		//将整形赋值给浮点型
//		double d1=123;
//		System.out.println("d1="+d1);
//		//变量间的赋值
//		double d2=d;
//		System.out.println("d2="+d2);
//	}
//}
// 数据类型变量存储 方法级 内存 
// 栈 int n=100; 常量池 堆
// 字符型字面值 char a = 'a'; char a=65;输出为A
// 强制转换引起数据丢失 不用
// ASCII码 7位二进制组合
//package com.test;
//
//public class CharDemo {
   
//	public static void main(String[] args) {
   
//		// 定义一个变量存放字符'a'
//		char a='a';
//		System.out.println("a="+a);
//		char ch=65535;
//		// 如果字面值超出char类型所表示的数据范围,需要进行强制类型转换,可能会引起数据丢失
//		char ch1=(char)65536;
//		System.out.println("ch="+ch);
// 		unicode
//		char c='\u005d';
//		System.out.println("c="+c);
//	}
//}
// Unicode 编码 char c='\u005d' 字符
// 布尔类型字面值 boolean b=ture; 
// 字符串字面值	String s1=""  双引号
//package com.test;
//
//public class StringDemo {
   
//	public static void main(String[] args) {
   
//		//定义一个空字符串
//		String s1="";
//		System.out.println("s1="+s1);
//		//定义一个字符串,Hello
//		String s2="Hello";
//		System.out.println("s2="+s2);
//		//定义一个包含Unicode字符的字符串
//		String s3="A\u005d\u005fB";
//		System.out.println("s3="+s3);
//		//定义一个包含空格的字符串
//		String s4="Hello  imooc!";
//		System.out.println("s4="+s4);
//	}
//}
// 变量综合案例 local局部的意思
// 转义字符 \r \n 换行 "\t" tab键的效果
// 字符串连接运算 ""
//package com.test;
//
//public class VarDemo {
   
//	public static void main(String[] args) {
   
//		//定义两个整型变量x,y
//		//intx=3,y=5;
//		int x,y;
//		x=3;y=5;
//		System.out.println("x="+x);
//		System.out.println("y="+y);
//		//关于换行的问题
//		System.out.print(""+x+'\t'+y+'\n');
//		System.out.println(); //换行
//		System.out.print(x+","+y);
//		System.out.println("\n\'"+x+"\'");
//		//定义一个汉字的字符
//		char ch ='幕';
//		System.out.println(ch);
//		//char 中文='中';
//		//System.out.println(中文);
//		//用科学计数法表示浮点型数据
//		double d =1.23E5;
//		float f = 1.23e5f;
//		double d1=.2;
//		System.out.println("d="+d);
//		System.out.println("f="+f);
//		System.out.println("d1="+d1);
//	}
//}
// 类型转换 自动 隐式转换long a =253 转换可能造成精度丢失 
// 强制 char ch=(char)65535; A数据类型大 转换为范围小的
//package com.test;
//
//public class TypeExchange {
   
//
//	public static void main(String[] args) {
   
//		//char类型和int类型之间的转换
//		char c=(char)65536;
//		int n;
//		n=c;//隐式类型转换
//		c=(char)n;
//		
//		//整型和浮点型的类型转换问题
//		int x=100;
//		long y=x;
//		x=(int)y;
//		float f=100000000000000L;
//		System.out.println("f="+f);
//		float f1=103948583923948L;
//		System.out.println("f1="+f1); // 数据丢失
//
//	}
//
//}
// 标识符 关键字 数据类型 基本 引用 八种   变量定义和初始化 基本数据类型字面值 ASCII UNICODE
// 类型转换
// 赋值运算符 int n=5; 变量=表达式; 从右往左运算
// 复合赋值运算符 += - * / %=
// 算术运算符 + - * / %  ++ 自增1 n++ --直减1 --n
//package com.imooc.operator;
//
//public class MathDemo {
   
//	public static void main(String[] args) {
   
//		int	num1=10,num2=5;
//		int result;//存放结果
//		// 加法
//		result=num1+num2;
//		System.out.println(num1+"+"+num2+"="+result);
//		//字符串连接
//		System.out.println(""+num1+num2);
//		//减法
//		result=num1-num2;
//		System.out.println(num1+"-"+num2+"="+result);
//		//乘法
//		result=num1*num2;
//		System.out.println(num1+"*"+num2+"="+result);
//		// 除法
//		result=num1/num2;
//		System.out.println(num1+"/"+num2+"="+result);
//		//分子分母都是整形时,结果为整除后的结果
//		System.out.println(13/5);
//		System.out.println("13.0/5="+13.0/5);
//		//求余数
//		result=13%num2;
//		System.out.println("13%"+num2+"="+result);
//		System.out.println("13.5%5="+13.5%5);
//	}
//}
// 自增 num2=++num1;  num1=num1+1;  num2=num1; num2=num1++;num2=num1;num1=num1+1
// 自减 num2=--num1;num1=num1-1;num2=num1; num2=num1--;num2=num1;num1=num1-1;
//package com.imooc.operator;
//
//public class MathDemo1 {
   
//	public static void main(String[] args) {
   
//		//x++
//		int x=4;
//		int y=(x++)+5;
//		System.out.println("x="+x+",y="+y);
//		// ++x
//		x=4;
//		y=(++x)+5;
//		System.out.println("x="+x+",y="+y);
//		// x--
//		x=4;
//		y=(x--)+5;
//		System.out.println("x="+x+",y="+y);
//		// --x
//		x=4;
//		y=(--x)+5;
//		System.out.println("x="+x+",y="+y);
//	}
//
//}

2021.2.28 星期日

// TODO 2021.2.28
// 关系运算符 大于 小于 大于等于 小于等于 等于== 不等于!=、
// 'A'>'B'  比较ASCII码
//package com.imooc.operator;
//
//public class RelateDemo {
   
//
//	public static void main(String[] args) {
   
//		int a=3,b=5;
//		System.out.println("a<b="+(a<b));
//		System.out.println("a>B="+(a>b));
//
//	}
//
//}
// 条件结构 语句 if(条件){ <语句块> }  
//package com.imooc.operator;
//
//public class ConditionDemo1 {
   
//	
//	public static void main(String[] args) {
   
//		// 商场打折,如果两件商品的价格大于100则减20,并把原价和折后价格分别输出
//		// 定义两个变量,分别存放两件衣服的价格	
//		double price1,price2;
//		price1=80;
//		price2=55;
//		//计算两件商品的总价格
//		double sum=price1+price2;
//		//输出原价
//		System.out.println("原价为:"+sum);
//		if(sum>=100) {
   
//			sum-=20;//sum=sum-20;
//		}
//		System.out.println("折后价格为:"+sum);
//	}
//}
// if-else if(条件){<语句块>}else {<语句块>} <语句块>布尔表达式
//import java.util.Scanner;
//
//public class ConditionDemo2 {
   
//
//	public static void main(String[] args) {
   
//		//判断一个整数是奇数还是偶数,并将结果打印输出
//		//定义一个变量存放数据
//		// int n=11;
//		// 从键盘接收数据
//		System.out.println("请输入一个整数:");
//		Scanner s = new Scanner(System.in);
//		int n=s.nextInt();
//		if(n%2==0){
   
//			System.out.println(n+"是偶数!");
//		}else {
   
//			System.out.println(n+"是奇数!");
//			
//		}
//
//	}
//
//}
//逻辑运算符&& &与 || |或 !非 布尔类型
// 与 &与&&区别 &&短路运算符 第一个表达式的值决定表达式最后的结果,后面的表达式就不计算了
// int	n=3;boolean b=(3<7)&&((n++)<2); b=false,n=3
// 或 |运算符 ||短路运算符
// 非!
//package com.imooc.operator;
//
//import java.util.Scanner;
//
//public class LogicDemo3 {
   
//
//	public static void main(String[] args) {
   
//		//输入一个整数
//		System.out.println("请输入一个整数:");
//		Scanner sc=new Scanner(System.in);
//		int n=sc.nextInt();
//		if(!(n%3==0)) {
   
//			System.out.println(n+"不能被3整除!");
//		}else {
   
//			System.out.println(n+"能被3整除!");
//		}
//
//	}
//
//}
// 条件运算符 三目运算符  ++单目 + - * /双目
// 语法 布尔表达式?表达式1:表达式2 布尔表达式为true 返回表达式1的值
//package com.imooc.operator;
//
//public class ConditionDemo {
   
//
//	public static void main(String[] args) {
   
//		int a=10,b=7;
//		//求a和b的最大值
//		int max;
//		if(a>b) {
   
//			max=a;
//		}else {
   
//			max=b;
//		}
//		System.out.println("max="+max);
//		max=a>b?a:b;
//		System.out.println("max="+max);
//		boolean b1=a>b?(3<6|3<1):(true==false);
//		System.out.println("b1="+b1);
//	}
//
//}
// 运算符的优先级 多加括号
// 案例1
//package com.imooc.operator;
//
//import java.util.Scanner;
//
//public class LearYearDemo {
   
//
//	public static void main(String[] args) {
   
//		System.out.println("请输入年份:");
//		Scanner sc=new Scanner(System.in);
//		int year=sc.nextInt();
//		//闰年判断规则:能被4整除但不能被100整除的年份,或者能被400整除的年份
//		if(((year%4==0)&(year%100!=0))|(year%400==0)) {
   
//			System.out.println(year+"是闰年!");
//		}else {
   
//			System.out.println(year+"不是闰年!");
//		}
//
//	}
//
//}
// 表达式 5, a m+3,sum=a+b,n=x*y+(x%2)-(x/y)
// 运算符 算术 ++ -- +-*/% 赋值 = += 关系 == != 逻辑 ! & && | || 条件运算符 ?:
// 三大流程控制语句 顺序 选择结构if if-else 多重if 嵌套if swich 循环while do-while for 循环嵌套
// 多行注释 /*   */ 
// 多重if else if
//package com.imooc.flow;
//
//import java.util.Scanner;
//
//public class ScoreAssess {
   
//
//	public static void main(String[] args) {
   
//		/*
//		 成绩大于等于90分,输出"优"
//		 成绩大于等于80并且小于90,输出良
//		 成绩大于等于60并且小于80,输出中
//		 成绩小于60分,输出不及格
//		 */
//		System.out.println("请输入成绩:");
//		Scanner sc=new Scanner(System.in);
//		int score=sc.nextInt();
//		if(score>=90)
//			System.out.println("优");
//		else if(score>=80) //相当于score>=80&score<90
//			System.out.println("良");
//		else if(score>=60)
//			System.out.println("中");
//		else
//			System.out.println("不及格");
//	}
//
//}
//嵌套if语句 注意缩进
//package com.imooc.flow;
//
//public class IntCompare {
   
//
//	public static void main(String[] args) {
   
//		//定义两个整型变量并初始化
//		int x=5,y=5;
//		//判断x和y是否相等
//		if(x!=y) {
   
//			if(x>y) {
   
//				System.out.println(x+"大于"+y);
//			}else {
   
//				System.out.println(x+"小于"+y);
//			}
//			
//		}else {
   
//			System.out.println(x+"和"+y+"相等");
//		}
//		
//
//	}
//
//}
// if switch区别
//package com.imooc.flow;
//
//import java.util.Scanner;
//
//public class WeekDemo {
   
//
//	public static void main(String[] args) {
   
//		Scanner sc = new Scanner(System.in);
//		System.out.println("请输入表示星期的英文单词:");
//		String week = sc.next();
//		week = week.toUpperCase();// 把字符串的字符全部改为大写
//		switch (week) {
   
//		case "MONDAY":
//			System.out.println("星期一");
//			break;
//		case "TUESDAY":
//			System.out.println("星期二");
//			break;
//		case "WEDNESDAY":
//			System.out.println("星期三");
//			break;
//		case "THURSDAY":
//			System.out.println("星期四");
//			break;
//		case "FRIDAY":
//			System.out.println("星期五");
//			break;
//		case "SATURDAY":
//			System.out.println("星期六");
//			break;
//		case "SUNDAY":
//			System.out.println("星期日");
//			break;
//		default:
//			System.out.println("单词输入错误!");
//		}
//
//	}
//
//}
// 循环结构 while循环 执行流程 局部变量
//package com.imooc.flow1;
//
//public class PlusDemo {
   
//
//	public static void main(String[] args) {
   
//		// 求1到5的累加和
//		int n=1;
//		int sum=0; //sum是存放和的变量
//		while(n<=5) {
   
//			sum=sum+n;
//			n++;
//		}
//		System.out.println("1到5的累加和为:"+sum);
//	}
//
//}
//package com.imooc.flow1;
//
//public class CharDemo {
   
//
//	public static void main(String[] args) {
   
//		// 循环输出26个英文小写字母,分两行输出
//		char ch='a';
//		int count=1;//控制换行
//		while(ch<='z') {
   
//			System.out.print(ch+" ");
//			if(count==13) {
   
//				System.out.println();
//			}
//			count++;
//			ch++;
//		}
//		
//
//	}
//
//}
// do -while循环
//package com.imooc.flow1;
//
//public class DoWhileDemo {
   
//
//	public static void main(String[] args) {
   
//		// 求1到5的累加和
//		int n=1;
//		int sum=0;
//		do {
   
//			sum+=n;
//			n++;
//		}while(n<=5);
//		System.out.println("sum="+sum);
//	}
//
//}
// Math.random()[0,1)的随机数
//package com.imooc.flow1;
//
//import java.util.Scanner;
//
//public class GuessDemo {
   
//
//	public static void main(String[] args) {
   
//		// 设置要猜的数
//		int number=(int)(Math.random()*10+1);//生成1-10之间的整数
//		System.out.println("number:"+number);
//		int guess;
//		System.out.println("猜一个介于1到10之间的数");
//		do {
   
//			System.out.println("请输入您要猜的数");
//			Scanner sc=new Scanner(System.in);
//			guess=sc.nextInt();
//			if(guess>number) {
   
//				System.out.println("太大!");
//			}else if(guess<number) {
   
//				System.out.println("太小!");
//			}
//		}while(number!=guess);
//		System.out.println("你猜中了!答案为"+number);
//	}	
//
//}
// for 循环 局部变量只在定义它的大括号内有效
//package com.imooc.flow1;
//
//public class ForDemo {
   
//
//	public static void main(String[] args) {
   
//		int sum=0;
//		for(int n=1;n<=5;n++) {
   
//			sum=sum+n;
//		}
//		System.out.println("sum="+sum);
//	}
//
//}
// for 循环注意事项 break  变量不能重复定义 三个表达式可以省略
//package com.imooc.flow1;
//
//import java.util.Scanner;
//
//public class NumberInput {
   
//
//	public static void main(String[] args) {
   
//		// 从键盘接收数据
//		Scanner sc = new Scanner(System.in);
//		int n;
//		while (true) {
   
//			n = sc.nextInt();
//			if (n == 0)
//				break;
//			System.out.println(n);
//		}
//	}
//
//}
// 循环嵌套

2021.3.1 星期一

// TODO 3/1
//package com.imooc.flow1;
//
//public class StarDemo1 {
   
//
//	public static void main(String[] args) {
   
//		int m=1;//外重循环的循环变量
//		int n=1;//内重循环
//		System.out.println("输出4行4列的星号");
//		while(m<=4) {
   
//			n=1;
//			while(n<=m) {
   
//				System.out.print("*");
//				n++;
//			}
//			m++;
//			System.out.println();
//		}
//
//	}
//
//}
//阶乘
//package com.imooc.flow1;
//
//public class JiechengPlus {
   
//
//	public static void main(String[] args) {
   
//		// TODO Auto-generated method stub
//		long s=1,sum=0;
//		for(int i=1;i<=10;i++) {
   
//			s=1;
//			for(int j=1;j<=i;j++) {
   
//				s=s*j;//s存放阶乘计算的结果 	
//			}
//			sum=sum+s;
//		}
//		System.out.println("1!+2!+3!+...+10!="+sum);
//	}
//
//}
// break 跳出当前循环
// continue  结束当前循环
// 程序调试-debug 调试逻辑  设置断点 执行调试 单步调试F6  F5进入执行
// 多断点设置 F8 断点到断点

2021.3.2 星期二

TODO 3/2
// 一维数组 声明 创建 初始化 元素引用 长度length属性获取 默认值为0,null 引用数据类型 开辟连续的内存空间 
// 数组下标越界异常exception 运行时异常
//package array;
//
//public class ArrayDemo {
   
//
//	public static void main(String[] args) {
   
//		//声明一个整形数组
//		int[] intArray;
//		//声明一个字符串类型的数组
//		String strArray[];
//		// 创建数组
//		intArray=new int[5];
//		strArray=new String[10];
//		//声明并创建数组
//		float[] floatArray=new float[4];
//		//初始化数组
//		char[] ch= {'a','b','c','d'};
//		System.out.println("ch数组的长度为:"+ch.length);
//		System.out.println("intArray数组的第2个元素为:"+intArray[1]);
//		System.out.println("strArray数组的第5个元素为:"+strArray[4]);
//		System.out.println("floatArray数组的最后一个元素为:"+floatArray[floatArray.length-1]);
//		//循环为整形数组赋值
//		for(int i=0;i<5;i++) {
   
//			intArray[i]=i+1;
//		}
//		//循环输出整形数组中的元素
//		System.out.println("整形数组intArray的元素为:");
//		for(int i=0;i<5;i++) {
   
//			System.out.print(intArray[i]+" ");
//		}
//
//	}
//
//}
//数组元素的累加和
//package array;
//
//import java.util.Scanner;
//
//public class ArrayDemo1 {
   
//
//	public static void main(String[] args) {
   
//		//求整形数组的累加和
//		//定义整形数组
//		int[] a=new int[5];
//		Scanner sc=new Scanner(System.in);
//		//从键盘接收数据,为数组元素赋值
//		for(int i=0;i<a.length;i++) {
   
//			System.out.println("请输入第"+(i+1)+"个元素:");
//			a[i]=sc.nextInt();
//		}
//		System.out.println("数组元素的内容为:");
//		for(int i=0;i<a.length;i++) {
   
//			System.out.print(a[i]+" ");
//		}
//		System.out.println();
//		System.out.println("使用增强型for循环输出数组内容:");
//		for(int n:a) {
   
//			System.out.print(n+" ");
//		}
///		//求数组元素的累加和
//		int sum=0;
//		for(int i=0;i<a.length;i++) {
   
//			sum+=a[i];
//		}
//		System.out.println();
//		System.out.println(sum);
//
//	}
//
//}
// 求数组最大值
//package array;
//
//public class ArrayDemo2 {
   
//
//	public static void main(String[] args) {
   
//		// 求数组元素的最大值
//		int[] a= {34,24,67,84,33};
//		int max=a[0];
//		for(int i=1;i<a.length;i++) {
   
//			if(max<a[i]) {
   
//				max=a[i];
//			}
//		}
//		System.out.println(max);
//
//	}
//
//}
// 增强型for循环	 foreach  for(intn:a)  a数组 
// 变量a,b值交换 int a=3,b=5,temp; temp=a;a=b;b=temp;
// 冒泡排序 从小到大排序 
//package array;
//
//public class SortDemo {
   
//
//	public static void main(String[] args) {
   
//		// 冒泡排序
//		int[] a= {34,53,12,32,56,17};
//		System.out.println("排序前的数组元素为: ");
//		for(int n:a) {
   
//			System.out.print(n+" ");
//		}
//		System.out.println();
//		int temp;
//		for(int i=0;i<a.length-1;i++) {
   
//			//内重循环控制每趟排序
//			for(int j=0;j<a.length-i-1;j++) {
   
//				if(a[j]>a[j+1]) {
   
//					temp = a[j];
//					a[j] = a[j+1];
//					a[j+1] = temp;
//				}			
//			}
//		}
//		System.out.println("从小到大排序后的数组元素为:");
//		for(int n:a) {
   
//			System.out.print(n+" ");
//		}
//	}
//}

2021.3.3 星期三

// TODO 3/3
// 二维数组  空指针异常 声明 创建 初始化
// 特殊的一维数组 多个一维数组组成
//package array;
//
//public class ArrayDemo5 {
   
//
//	public static void main(String[] args) {
   
//		// 二维数组声明
//		// 三种形式
//		// 声明int类型的二维数组
//		int[][] intArray;
//		// 声明float类型的二维数组
//		float floatArray[][];
//		// 声明double类型的二维数组
//		double[] doubleArray[];
//		// 创建一个三行三列的int类型的数组
//		intArray = new int[3][3];
//		System.out.println("intArray数组的第3行第2列的元素为:"+intArray[2][1]);
//		// 为第二行第3个元素赋值为9
//		intArray[1][2] = 9;
//		System.out.println("intArray数组第2行第3列的元素为:"+intArray[1][2]);
//		// 声明数组的同时进行创建
//		char[][] ch=new char[3][5];
//		// 创建float类型的数组时,只指定行数
//		floatArray = new float[3][];
//		//System.out.println(floatArray[0][0]); 空指针报错
//		// 每行相当于一个一维数组,需要创建	
//		floatArray[0] = new float[3]; //第一行有三列
//		floatArray[1] = new float[4]; //第二行有四列
//		floatArray[2] = new float[5]; //第三行有5列
//		System.out.println(floatArray[0][0]);
//		//System.out.println(floatArray[0][3]); 数组下标越界
//		// 二维数组的初始化
//		int[][] num = {
   {1,2,3},{4,5,6},{7,8,9}};
//		System.out.println("num数组第1行第2列的元素为:"+num[0][1]);
//		System.out.println("num数组的行数为:"+num.length);
//		System.out.println("num数组的列数为:"+num[0].length);
//		int[][] num1= {
   {39,98},{22,33,44},{98}};
//		System.out.println("num1数组的第一行的列数为:"+num1[0].length);
//		// 循环输出二维数组的内容
//		for(int i=0;i<num1.length;i++) {
   
//			for(int j=0;j<num1[i].length;j++) {
   
//				System.out.print(num1[i][j]+" ");
//			}
//			System.out.println();
//		}
//		
//
//	}
//
//}
// 无参无返回值方法
//package method;
//
//import method.MethodDemo;
//
//public class MethodDemo {
   
//
//	// 打印输出星号的方法
//	public void printStar() {
   
//		System.out.println("**************************");
//	}
//	
//	public static void main(String[] args) {
   
//		// 创建一个MethodDemo类的对象myMethodDemo
//		MethodDemo myMethodDemo = new MethodDemo();
//		// 使用对象名.方法名()去调用方法
//		myMethodDemo.printStar();
//		System.out.println("欢迎来到java的世界!");
//		myMethodDemo.printStar();
//
//	}
//
//}
//无参带返回值的方法
//package method;
//
//import method.Rectangle;
//
//public class Rectangle {
   
//	//求长方形面积的方法
//	public int area() {
   
//		int length=10;
//		int width =5;
//		int getArea=length*width;
//		return getArea; //返回语句
//	}
//	public static void main(String[] args) {
   
//		Rectangle rc = new Rectangle();
//		System.out.println("长方形的面积为:"+rc.area());
//
//	}
//
//}

2021.3.4 星期四

// 带参无返回值方法
//package method;
//
//import method.MaxDemo;
//
//public class MaxDemo {
   
//	
//	//求最大值的方法
//	public void max(float a, float b) {
   
//		float max;
//		if(a>b) {
   
//			max=a;
//		}else {
   
//			max=b;
//		}
//		System.out.println("两个数"+a+"和"+b+"的最大值为:"+max);
//	}
//	
//	public static void main(String[] args) {
   
//		MaxDemo maxDemo = new MaxDemo();
//		int a=5,b=3;
//		maxDemo.max(a, b);
//		float m=5.6f,n=8.9f;
//		maxDemo.max(m, n);
//		maxDemo.max(9.8f, 12.8f);
		maxDemo.max(1.2, 3.4);
//
//	}
//
//}
// 有参有返回值方法 
//package method;
//
//import method.FacDemo;
//
//public class FacDemo {
   
//	
//	//方法不能嵌套定义
//	//求阶乘的方法
//	public int fac(int n) {
   
//		int s=1;
//		for(int i=1;i<=n;i++) {
   
//			s*=i;
//		}
//		return s;
//	}
//	public static void main(String[] args) {
   
//		FacDemo facDemo = new FacDemo();
//		int fac=facDemo.fac(3);
//		System.out.println("3!="+fac);
//		int sum=0;
//		//求1!+2!+3!+4!+5!
//		for(int i=1;i<=5;i++) {
   
//			fac=facDemo.fac(i);
//			sum+=fac;
//		}
//		System.out.println(sum);
//
//	}
//
//}
// 数组作为方法参数
//package method;
//
//import method.ArrayMethod;
//
//public class ArrayMethod {
   
//	//打印输出数组元素的值
//	public void printArray(int[] arr) {
   
//		for(int i=0;i<arr.length;i++) {
   
//			System.out.print(arr[i]+" ");
//		}
//		System.out.println();
//	}
//	
//	public static void main(String[] args) {
   
//		int[] arr= {12,22,30,40,50};
//		ArrayMethod am=new ArrayMethod();
//		am.printArray(arr);
//	}
//
//}
//参数的传递问题
//package method;
//
//import method.ExchangeDemo;
//
//public class ExchangeDemo {
   
//	
//	//交换方法
//	public void swap(int a,int b) {
   
//		int temp;
//		System.out.println("交换前:a="+a+",b="+b);
//		temp=a;a=b;b=temp;
//		System.out.println("交换后:a="+a+",b="+b);
//	}
//	public void swapTest() {
   
//		int m=4,n=5;
//		System.out.println("交换前:m="+m+",n="+n);
//		swap(4,5);
//		System.out.println("交换后:m="+m+",n="+n);
//	}
//
//	public static void main(String[] args) {
   
//		ExchangeDemo ed=new ExchangeDemo();
//		ed.swapTest();
//
//	}

2021.3.5 星期五

java基础 综合案例 数组位移

package datamanage;
/*
 * 从键盘接收整形数据存放到数组中,并对数组中的数据进行管理
 */

import java.util.InputMismatchException;
import java.util.Scanner;

public class DataManage {
   
	
	public int[] insertData(){
   
		int[] a=new int[10];
		Scanner sc = new Scanner(System.in);
		//少接收一个数据,为在指定位置处插入数据做准备
		for(int i=0;i<a.length-1;i++) {
   
			System.out.println("请输入第"+(i+1)+"个数据:");
			try {
   
				a[i]=sc.nextInt();
			}catch(InputMismatchException e) {
   
				System.out.println("输入的数据格式有误,不能有非数字!");
				sc.next(); //接收字符串数据
				i--;
			}
			
		}
		return a;
	}
	/**
	 * 显示数组中元素的内容
	 * @param a:数组
	 * @param length:要显示的数组元素的个数
	 */
	public void showData(int[] a,int length) {
   
		for(int i=0;i<length;i++) {
   
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	/**
	 * 从键盘接收一个数据,插入到数组的指定位置处
	 * @param a 要插入数据的数组
	 * @param n 要插入的数据
	 * @param k 要插入的位置,从0开始
	 */
	public void insertAtArray(int[] a,int n,int k) {
   
		//注意从最后一个数据开始移动,避免数据覆盖
		for(int i=a.length-1;i>k;i--) {
   
			a[i]=a[i-1];
		}
		a[k]=n;
	}
	
	/**
	 * 被3 整除的数
	 * @param a 数组
	 */
	public void divThree(int[] a) {
   
		for(int i=0;i<a.length;i++) {
   
			if(a[i]%3==0) {
   
				System.out.print("被三整除的数为:"+a[i]);
			}
		}
		System.out.println();
	}
	/**
	 * 提示信息
	 */
	public void notic() {
   
		System.out.println("***************************");
		System.out.println("         1--插入数据");
		System.out.println("         2--显示所有数据");
		System.out.println("         3--在指定位置处插入数据");
		System.out.println("         4--查询能被3整除的数据");
		System.out.println("         0--退出");
		System.out.println("***************************");
	}

	public static void main(String[] args) {
   
		DataManage dm = new DataManage();
		Scanner sc = new Scanner(System.in);
		int input;
		int[] a =null;
		int n=0,k=0; //n表示要插入的数据,k表示插入位置
		while(true) {
   
			dm.notic();
			System.out.println("请输入对应数字进行操作:");
			try {
   
				input=sc.nextInt();
			}catch(InputMismatchException e) {
   
				System.out.println("输入的数据格式有误,不能有非数字!");
				sc.next();
				continue;
			}
			
			 
			if(input==0) {
   
				System.out.print("退出程序!");
				break;
			}
			switch(input) {
   
			case 1:
				//插入数据
				a=dm.insertData();
				//显示数据
				System.out.println("数组元素为:");
				dm.showData(a, a.length-1);
				break;
			case 2:
				if(a!=null) {
   
					System.out.println("数组元素为:");
					if(a[a.length-1]==0) {
   
						//如果数组的最后一个元素为0,说明还没有插入数据,因此不显示最后一个元素
						dm.showData(a, a.length-1);
					}else {
   
						dm.showData(a, a.length);
					}
					
				}else {
   
					System.out.println("还未在数组中插入数据,请重新选择操作!");
				}
				break;
			case 3:
				//在指定位置处插入数据
				if(a!=null) {
   
					System.out.println("请输入要插入的数据:");
					try {
   
						n=sc.nextInt();
						System.out.println("请输入要插入数据的位置:");
						k=sc.nextInt();
					}catch(InputMismatchException e) {
   
						System.out.println("输入的数据格式有误,不能有非数字!");
						sc.next();
						break;
					}
					dm.insertAtArray(a, n, k);
					System.out.println("数组元素为:");
					dm.showData(a, a.length);
					
				}else {
   
					System.out.println("还未在数组中插入数据,请重新选择操作!");
				}
				break;
			case 4:
				if(a!=null) {
   
					dm.divThree(a);
				}else {
   
					System.out.println("还未在数组中插入数据,请重新选择操作!");
				}
				break;
			}
		}
	
	}

}


2021.3.6 星期六

java 面向对象 构造方法

// TODO 3/6
// TODO 面向对象
// 类 模板 实例化 对象 属性 方法
// 单一原则  
// new 关键字  声明对象 Cat one 内存开辟栈空间 one null 存储堆空间地址的引用   new Cat() 开辟堆空间
// Cat one=new Cat() 对象实例化  one 地址指向堆空间地址 关联
//package animal;
///**
// * 宠物猫类
// * @author xi
// *
// */
//public class Cat {
   
//	// 成员属性:昵称、年龄、体重、品种
//	String name; //昵称 String类型默认值null
//	int month; // 年龄  int类型默认值0
//	double weigth;//体重 double类型默认值0.0
//	String species; //品种
//	
//	//成员方法:跑动、吃东西
//	//跑动的方法
//	public void run() {
   
//		System.out.println("小猫快跑");
//	}
//	public void run(String name) {
   
//		System.out.println(name+"快跑");
//	}
//	
//	//吃东西的方法
//	public void eat() {
   
//		System.out.println("小猫吃鱼 ");
//	}
//	
//
//}
//package animal;
//
//import animal.Cat;
//
//public class CatTest {
   
//
//	public static void main(String[] args) {
   
//		// 对象实例化
//		Cat one =new Cat();
//		Cat two = one;
//		//测试
//		one.eat();
//		one.run();
//		one.name = "花花";
//		one.month =2;
//		one.weigth=1000;
//		one.species = "英国短毛猫";
//		two.name = "凡凡";
//		one.month =1;
//		one.weigth=800;
//		one.species = "中华田园猫";
//		System.out.println("昵称:"+one.name);
//		System.out.println("年龄:"+one.month);
//		System.out.println("体重:"+one.weigth);
//		System.out.println("品种:"+one.species);
//		System.out.println("-----------------------------");
//		System.out.println("昵称:"+two.name);
//		System.out.println("年龄:"+two.month);
//		System.out.println("体重:"+two.weigth);
//		System.out.println("品种:"+two.species);
//		one.run(one.name);
//
//	}
//
//}

2021.3.7 星期日

java 封装 static

//TODO 3/7
// 构造方法 public访问修饰符 与类名相同 没有返回值类型  public 方法名(){} 实例化的时候调用
// 自动构造  无参构造方法  有参构造方法 就近原则 this 关键字 代表当前对象 调用属性 方法
// 构造方法之间的调用
// 设置 window preferances  
//package animal;
///**
// * 宠物猫类
// * @author xi
// *
// */
//public class Cat {
   
//	// 成员属性:昵称、年龄、体重、品种
//	String name; //昵称 String类型默认值null
//	int month; // 年龄  int类型默认值0
//	double weigth;//体重 double类型默认值0.0
//	String species; //品种
//	
//	public Cat() {
   
//		System.out.println("我是无参构造方法");
//	}
//	public Cat(String name) {
   
//		System.out.println("我是带参构造方法");
//	}
//	public Cat(String name,int month,double weigth,String species) {
   
//		this(); // 调用构造方法
//		this.name=name;
//		this.month=month;
//		this.weigth=weigth;
//		this.species=species;
//	}
//	//成员方法:跑动、吃东西
//	//跑动的方法
//	public void run() {
   
//		this.eat();
//		System.out.println("小猫快跑");
//	}
//	public void run(String name) {
   
//		System.out.println(name+"快跑");
//	}
//	
//	//吃东西的方法
//	public void eat() {
   
//		System.out.println("小猫吃鱼 ");
//	}
//	
//
//}
//package animal;
//
//import animal.Cat;
//
//public class CatTest {
   
//
//	public static void main(String[] args) {
   
//		// 对象实例化
//		Cat one =new Cat("花花",2,1000,"英国短毛猫");
//		
//		//测试
//		System.out.println("昵称:"+one.name);
//		one.eat();
//		one.run();
//		
//
//	}
//
//}
// 总结 类 对象成员属性 成员方法 访问修饰符  对象类型 null 实例化 构造方法 多个 重载 this关键字 调用成员属性 局部变量和成员属性冲突问题 成员方法
// 调用重载的构造方法 必须放在第一行
// 封装 修改属性的可见性 private getter/setter方法 public 逻辑隐患
// 访问修饰符 private 	只能在当前类内访问 public  
// 只有getxxx方法的属性只读属性 只写属性只有setxxx方法的属性
//package animal;
///**
// * 宠物猫类
// * @author xi
// *
// */
//public class Cat {
   
//	// 修改属性的可见性--private 限定只能在当前类内访问 成员属性:昵称、年龄、体重、品种
//	private String name; //昵称 String类型默认值null
//	private int month; // 年龄  int类型默认值0
//	private double weigth;//体重 double类型默认值0.0
//	private String species; //品种
//	
//	public Cat() {
   
//		System.out.println("我是无参构造方法");
//	}
//	public Cat(int month) {
   
//		this.setMonth(month);
//		System.out.println("我是带参构造方法");
//	}
//	public Cat(String name,int month,double weigth,String species) {
   
//		this(); // 调用构造方法
//		this.name=name;
//		this.month=month;
//		this.weigth=weigth;
//		this.species=species;
//	}
//	// 创建get/set方法
//	// 在get/set方法中添加对属性的限定
//	public void setName(String name) {
   
//		this.name=name;
//	}
//	public int getMonth() {
   
//		return month;
//	}
//	public void setMonth(int month) {
   
//		if(month<=0) {
   
//			System.out.println("输入信息有误,宠物猫的年龄必须大于0");
//		}else {
   
//			this.month = month;
//		}
//	}
//	public double getWeigth() {
   
//		return weigth;
//	}
//	public void setWeigth(double weigth) {
   
//		this.weigth = weigth;
//	}
//	public String getSpecies() {
   
//		return species;
//	}
//	public void setSpecies(String species) {
   
//		this.species = species;
//	}
//	public String getName() {
   
//		return "我是一只名叫:"+this.name+"的宠物猫";
//	}
//	
//	
//	//成员方法:跑动、吃东西
//	//跑动的方法
//	public void run() {
   
//		this.eat();
//		System.out.println("小猫快跑");
//	}
//	public void run(String name) {
   
//		System.out.println(name+"快跑");
//	}
//	
//	//吃东西的方法
//	public void eat() {
   
//		System.out.println("小猫吃鱼 ");
//	}
//	
//
//}
//package animal;
//
//import animal.Cat;
//
//public class CatTest {
   
//
//	public static void main(String[] args) {
   
//		// 对象实例化
//		Cat one =new Cat(1);
//		
//		//测试
		one.setMonth(0);
//		if(one.getMonth()==0)
//			return;
//		System.out.println("年龄:"+one.getMonth());
//		
//		
//
//	}
//
//}
// 包内不存在	同名类 域名倒叙 +模块+功能 域名小写 定义包在第一行package animal;	每个包内存储信息功能单一
// 建议采用到包名.类名  效率高 import com.imooc.animal.Cat * 
// 常用系统包 java.lang 默认导入 java.util Scanner Random java.io
// static关键字 静态方式 public static int price;  静态成员 类成员 共用同一个内存空间 类对象共享
// 访问方式 Cat.price = 2000;   静态属性 类属性
// Cat.eat(); 静态方法 public static void eat(){ }
// 局部变量不能用static 成员方法里面可以访问静态方法 静态方法不能直接访问非静态成员
// 为什么静态方法不能访问类中的成员方法
// 普通代码块 方法中 作用空间 变量作用范围 生命周期 构造代码块 类中  static静态代码块 {  }什么用?
// 总结  封装 留出访问的接口  设置private属性  留出控制属性的接口 getter setter
// 定义包 package 导入包 import 包名.类名
// static	类 局部变量  不行   静态生命周期
// TODO 学生管理系统
// 格式化 快捷键 L

java 面向对象 综合案例学生管理系统

package com.imooc.model;

public class Student {
   
	// 成员属性:学号、姓名、性别、年龄
	private String studentNo;
	private String studentName;
	private String studentSex;
	private int studentAge;
	private Subject studentSubject;  // 添加专业对象属性

	// 无参构造方法
	public Student() {
   

	}

	// 多参构造方法,实现对学号、性别、姓名、年龄 的赋值
	public Student(String studentNo, String studentName, String studentSex, int studentAge) {
   
		this.setStudentNo(studentNo);
		this.setStudentName(studentName);
		this.setStudentSex(studentSex);
		this.setStudentAge(studentAge);
		
	}

	// 多参构造方法,实现对全部属性的赋值  方案三
	public Student(String studentNo, String studentName, String studentSex, int studentAge, Subject studentSubject) {
   
		this.setStudentNo(studentNo);
		this.setStudentName(studentName);
		this.setStudentSex(studentSex);
		this.setStudentAge(studentAge);
		this.setStudentSubject(studentSubject);
	}
	/**
	 * 获取专业对象,如果没有实例化,先实例化后返回
	 * @return 专业对象信息
	 */
	public Subject getStudentSubject() {
   
		if(this.studentSubject==null)
			this.studentSubject = new Subject();
		return studentSubject;
	}

	public void setStudentSubject(Subject studentSubject) {
   
		this.studentSubject = studentSubject;
	}

	public String getStudentNo() {
   
		return studentNo;
	}

	public void setStudentNo(String studentNo) {
   
		this.studentNo = studentNo;
	}

	public String getStudentName() {
   
		return studentName;
	}

	public void setStudentName(String studentName) {
   
		this.studentName = studentName;
	}

	public String getStudentSex() {
   
		return studentSex;
	}

	public void setStudentSex(String studentSex) {
   
		// 限制性别只能是“男”或者“女。反之,强制赋值为"男"
		this.studentSex = studentSex;
	}

	public int getStudentAge() {
   
		return studentAge;
	}

	/**
	 * 给年龄赋值,限定必须在10-100之间,反之赋值为18
	 * 
	 * @param studentSex 传入的年龄
	 */
	public void setStudentAge(int studentAge) {
   
		if (studentAge < 10 || studentAge > 100)
			this.studentAge = 18;
		else
			this.studentAge = studentAge;
	}

	/**
	 * 学生自我介绍方法 方案三
	 * 
	 * @return 自我介绍的信息,姓名、学号、性别、年龄
	 */
	public String introduction() {
   
		String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
				+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所报专业的名称:" + this.getStudentSubject().getSubjectName() + "\n学制年限:"
						+ this.getStudentSubject().getSubjectLife();
		return str;
	}

	/**
	 * 学生自我介绍方法 方案一
	 * 
	 * @param subjectName 所学专业名称
	 * @param subjectLife 学制年限
	 * @return 自我介绍的信息,包括年龄、学号、性别、所学专业名称、学制年限
	 */
	public String introduction(String subjectName, int subjectLife) {
   
		String str = "学生信息如下: \n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
				+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所报专业的名称:" + subjectName + "\n学制年限:"
				+ subjectLife;
		return str;
	}

	/**
	 * 学生自我介绍的方法 方案二
	 * 
	 * @param mySubject 所选专业的对象
	 * @return 自我介绍的信息 包括姓名、学号、性别、年龄、所学专业名称、学制年限
	 */
	public String introduction(Subject mySubject) {
   
		String str = "学生信息如下: \n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
				+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所报专业的名称:" + mySubject.getSubjectName()
				+ "\n学制年限:" + mySubject.getSubjectLife() + "\n专业编号:" + mySubject.getSubjectNo();
		return str;
	}

}
package com.imooc.model;
/**
 * 专业类
 * @author xx 
 */
public class Subject {
   
	// 成员属性:学科名称、学科编号、学制年限
	private String subjectName;
	private String subjectNo;
	private int subjectLife;
	private Student[] myStudents;
	private int studentNum;

	// 无参构造方法
	public Subject() {
   
		
	}
	// 带参构造方法,实现对属性额全部赋值
	public Subject(String subjectName,String subjectNo,int subjectLife) {
   
		this.setSubjectName(subjectName);
		this.setSubjectNo(subjectNo);
		this.setSubjectLife(subjectLife);
	}
	
	public void setSubjectName(String subjectName) {
   
		this.subjectName = subjectName;
	}

	public String getSubjectName() {
   
		return subjectName;
	}

	public void setSubjectNo(String subjectNo) {
   
		this.subjectNo = subjectNo;
	}

	public String getSubjectNo() {
   
		return subjectNo;
	}

	public int getSubjectLife() {
   
		return subjectLife;
	}

	// 设置学制年限,限制必须>0
	public void setSubjectLife(int subjectLife) {
   
		if (subjectLife <= 0)
			return;
		this.subjectLife = subjectLife;
	}
	
	/**
	 * 获取选修专业的学生信息 如果保存学生信息的数组未被初始化,则,先初始化长度200
	 * @return 保存学生信息的数组
	 */
	public Student[] getMyStudents() {
   
		if(this.myStudents==null)
			this.myStudents=new Student[200];
		return myStudents;
	}

	public void setMyStudents(Student[] myStudents) {
   
		this.myStudents = myStudents;
	}

	public int getStudentNum() {
   
		return studentNum;
	}

	public void setStudentNum(int studentNum) {
   
		this.studentNum = studentNum;
	}
	
	/**
	 * 专业介绍方法
	 * @return 专业介绍的相关信息,包括名称、编号、年限
	 */
	public String info() {
   
		String str = "专业信息如下:\n专业名称:" + this.getSubjectName() + "\n专业编号:" + this.getSubjectNo() + "\n学制年限:"
				+ this.getSubjectLife() + "年";
		return str;
	}
	
	public void addStudent(Student stu){
   
		/*
		 * 1、将学生保存到数组中
		 * 2、将学生个数保存到studentNum
		 * */
		//1、将学生保存到数组中
		for(int i=0;i<this.getMyStudents().length;i++){
   
			if(this.getMyStudents()[i]==null){
   
				stu.setStudentSubject(this);  // 关联学生和学科 
				this.getMyStudents()[i]=stu;
				//2、将学生个数保存到studentNum
				this.studentNum=i+1;
				return;
			}
		}
	}

}
package com.imooc.test;

import com.imooc.model.Student;
import com.imooc.model.Subject;

public class SchoolTest {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		// 测试Subject
		Subject sub1 = new Subject("计算机科学与应用", "J0001", 4);
		System.out.println(sub1.info());
		// 测试Student
		Student stu1 =new Student("S01","账上","男",18,sub1);
		System.out.println(stu1.introduction());
		System.out.println("===================================");
		Student stu2 = new Student("S02","LISI","NV",24);
		System.out.println(stu2.introduction("计算机科学与应用专业", 4));
		System.out.println("===================================");
		Student stu3 = new Student("S03","SADA","男",19);
		System.out.println(stu3.introduction(sub1));
		//测试指定专业中到底有多少学生报名
		sub1.addStudent(stu1);
		sub1.addStudent(stu2);
		sub1.addStudent(stu3);
		System.out.println(sub1.getSubjectName()+"的专业中已有"+sub1.getStudentNum()+"学生进行了报名");
	}

}

2021 3.8 星期一

// 传入对象参数 传递的是引用 类中 对象参数 为属性
// 数组 容器 Student[] ary = new Student[200];
// 集合 新增属性完成学生信息存储
// length 空指针错误 只是声明 没有实例化 用的时候在开空间

2021 3.9 星期二

继承

// TODO 3/9

// 继承 利于代码的复用 父类 基类 子类 派生类
// public class Dog extends Animal{} 只能继承一个父类 继承非私有 父类不可以访问子类 为什么?
// 方法重写 与父类方法完全一致 访问修饰符允许有变化?与参数名无关
//方法重载 同一个类中 方法名相同 参数列表不同(顺序、个数、类型) 方法返回值、访问修饰符任意 与方法名无关
// 子类先调用重写的方法
// 调用父类的方法 super 父类对象的引用 super.eat();
// private 	本类 默认 同包 protected 访问修饰符	关键字 子类可用
// public 任意可用
// 父类的构造不能继承、重写?
// 继承初始化顺序 执行 类型加载 静态   对象实例化 执行构造 先父类 后子类
// 无参构造 必须创建
// 子类构造默认调用父类无参构造方法 用super()调用父类的其他构造方法 放在第一行
// this 对象引用 super父类对象引用  this() super() 构造方法中不能同时出现
package com.imooc.animal;

/* final class:该类没有子类  public final class \ final public class
 * final 方法:该方法不允许被子类重写,但是可以正常被子类继承使用
 * final 方法内局部变量:只要在具体被使用之前进行赋值即可,一旦赋值不允许被修改
 *       类中成员属性:赋值过程:1、定义直接初始化  2、构造方法  3、构造代码块
 */
public class Animal {
   
	/*
	 * private:只允许在本类中进行访问 public:允许在任意位置访问
	 * protected:允许在当前类、同包子类/非子类、跨包子类调用;跨包非子类不允许
	 * 默认:允许在当前类、同包子类/非子类调用;跨包子类/非子类不允许调用
	 */
	private String name = "妮妮";// 昵称
	protected int month;// 月份
	String species = "动物";// 品种
	public  final static int temp=12;

	static {
   
		System.out.println("我是父类的静态代码块");
	}

	public static int st2 = 23;
	private static int st1 = 22;

	{
   
//		temp=12;
		System.out.println("我是父类的构造代码块");
	}

	// 父类的构造不允许被继承、不允许被重写,但是会影响子类对象的实例化
	 public Animal() {
   
		month = 2;
//		temp=20;
		System.out.println("我是父类的无参构造方法");
	}

	public Animal(String name, int month) {
   
		this.name = name;
		this.month = month;
		System.out.println("我是父类的带参构造方法");
	}

	public String getName() {
   
//		this.temp=22;
		return name;
	}

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

	public int getMonth() {
   
		return month;
	}

	public void setMonth(int month) {
   
		this.month = month;
	}

	public String getSpecies() {
   
		return species;
	}

	public void setSpecies(String species) {
   
		this.species = species;
	}

	// 吃东西
	public void eat() {
   
		System.out.println(this.getName() + "在吃东西");
	}

	public void eat(String name) {
   
		final int temp;//方法内的局部变量
//		temp=12;
		System.out.println(name + "在吃东西");
		temp=12;
		System.out.println(temp);
		
		final Animal animal=new Animal("凡凡",1);
//		animal=new Animal();
		animal.month=12;
		animal.name="豆豆";
	}

	public boolean equals(Object obj){
   
//		if(obj==null)
//			return false;
		Animal temp=(Animal)obj;
		if(this.getName().equals(temp.getName()) && (this.getMonth()==temp.getMonth()))
				return true;
		else
			return false;
	}
	
	public boolean equals(Animal obj){
   
//		if(obj==null)
//			return false;
		if(this.getName().equals(obj.getName()) && (this.getMonth()==obj.getMonth()))
				return true;
		else
			return false;
	}

	public String toString() {
   
		return "昵称:" + this.getName() + ";年龄:" + this.getMonth();
	}
	
	public Animal create(){
   
		return new Animal();
	}	
	
}

package com.imooc.animal;

public class Cat extends Animal{
   
	private double weight;//体重
	//public int temp=300;
	public static int st3=44;
	
	static{
   
		System.out.println("我是子类的静态代码块");
	}
	
	{
   
		System.out.println("我是子类的构造代码块");
	}
	
	public Cat(){
   
//		Animal temp=new Animal();
//		temp.name;
//		this.temp=12;
//		this.month=23;
//		this.species="";
		System.out.println("我是子类的无参构造方法");
	}
	
	public Cat(String name,int month){
   
		/* 子类构造默认调用父类无参构造方法
		 * 可以通过super()调用父类允许被访问的其他构造方法
		 * super()必须放在子类构造方法有效代码第一行
		 */
		super(name,month); //this
		System.out.println("我是子类的带参构造方法");
	}
	
	public static void say(){
   
//		this.weight=20;
//		super.name="aa";
	}
	
	public double getWeight() {
   
		return weight;
	}

	public void setWeight(double weight) {
   
		this.weight = weight;
	}
	
	//跑动的方法
	public void run(){
   
//		super();
//		eat();
//		Animal();
		System.out.println(this.getName()+"是一只"+this.getSpecies()+",它在快乐的奔跑");
	}
}
package com.imooc.animal;

public class Dog extends Animal {
   
	private String sex;//性别
	
	public Dog(){
   
		
	}

	public String getSex() {
   
		return sex;
	}

	public void setSex(String sex) {
   
		this.sex = sex;
	}
	
	//睡觉的方法
	 public void sleep(){
   
		 super.eat();//调用的哪个eat();
		 super.species="犬科";
		 System.out.println(this.getName()+"现在"+this.getMonth()+"个月大,它在睡觉~~");
	 }
	 
	 /*
	  * 方法重载:
	  * 1、同一个类中
	  * 2、方法名相同,参数列表不同(参数顺序、个数、类型)
	  * 3、方法返回值、访问修饰符任意
	  * 4、与方法的参数名无关
	  * 
	  * 方法重写
	  * 1、有继承关系的子类中
	  * 2、方法名相同,参数列表相同(参数顺序、个数、类型),方法返回值可以允许是子类类型
	  * 3、访问修饰符,访问范围需要大于等于父类的访问范围
	  * 4、与方法的参数名无关
	  * */
	 
//	 private String sleep(String name){
   
//		 return "";
//	 }
//	 public void sleep(String name,int month){
   
//		 
//	 }
//	 public void sleep(int month,String name){
   
//		 
//	 }
//	 public void sleep(int name,String month){
   
//		 
//	 }
	 
	 //子类重写父类吃东西方法
//	 public void eat(){
   
//		 System.out.println(this.getName()+"最近没有食欲~~");
//	 }
	 
	 public void eat(String month){
   
		 System.out.println(month+"最近没有食欲~~");
	 }

	 @Override
	public void eat() {
   
		// TODO Auto-generated method stub
		super.eat();
	}
	 
	@Override
	public Dog create() {
   
		// TODO Auto-generated method stub
		return new Dog();
	}
	 
}
package com.imooc.test;

import com.imooc.animal.Animal;
import com.imooc.animal.Cat;
import com.imooc.animal.Dog;

public class Test {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
//		Cat one=new Cat();
//		one.setName("花花");
//		one.setSpecies("中华田园猫");
//		one.eat();
//		one.run();
//		System.out.println(one.temp);
//		System.out.println("===================");
		Dog two=new Dog();
		two.setName("妞妞");
		two.setMonth(1);
		two.eat();
//		two.sleep();
//		System.out.println("===================");
//		two.eat("凡凡");
//		System.out.println("===================");
//		Animal three=new Animal();
//		three.month=2;
//		three.species="猫科动物";
//		three.name;
//		three.run();
//		three.sleep();
	}

}
package com.imooc.test;

import com.imooc.animal.Cat;

public class TestTwo {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
//		Cat one=new Cat();
		Cat one=new Cat("花花",2);
		System.out.println(one.temp);
	}

}
package com.imooc.test;

import com.imooc.animal.Animal;

public class TestThree {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		Animal one=new Animal("花花",2);
//		Animal two=new Animal("花花",2);
		Animal two=null;
		/*equals测试:
		 * 1、继承Object中的equals方法时,比较的是两个引用是否指向同一个对象
		 * 2、子类可以通过重写equals方法的形式,改变比较的内容
		 */
		boolean flag=one.equals(two);
		System.out.println("one 和 two的引用比较:"+flag);
		System.out.println("one 和 two的引用比较:"+(one==two));
		System.out.println("======================================");
		String str1=new String("hello");
		String str2=new String("hello");
		flag=str1.equals(str2);
		System.out.println("str1 和 str2的引用比较:"+flag);
		System.out.println("str1 和 str2的引用比较:"+(str1==str2));
		System.out.println("======================================");
		/*toString测试:
		 * 1、输出对象名时,默认会直接调用类中的toString
		 * 2、继承Object中的toString方法时,输出对象的字符串表示形式:类型信息+@+地址信息
		 * 2、子类可以通过重写equals方法的形式,改变输出的内容以及表现形式
		 */
		System.out.println(one.toString());
		System.out.println(one);
		System.out.println("======================================");
		System.out.println(str1);
	}

}


2021 3.10 星期三

// TODO 3/10
// Object类 默认继承object类
// object one.equals(two) 比较的是两个引用是否指向同一个对象
// String 重写equals方法 比较字符串内容 类型转换  null 空指针异常
// object toString方法 输出对象默认直接调用  输出@地址信息  String类 重写toString 输出字符串内容
// 子类重写
// final 关键字 final class 该类没有子类  
//final 方法 不能被子类重写,可以继承使用
// final 方法内局部变量 赋值后不允许修改 final int temp =12; 
// public final static int temp=12; 类中成员属性 赋值过程 定义直接初始化 构造方法 构造代码块
// art+/ 提示输入
// 不允许使用在构造方法
// final Animal animal = new Animal()引用类型变量 初始化后不能指向另一个对象,对象的属性可以改变
// static final 不可修改全局变量 配置信息
// 修改快捷键
// 注解 对元素进行说明、注释 辅助编译器理解 源码注解 编译时注解 .class有 运行时注解 @Override 
// 方法重写 可以返回子类类型 访问修饰符的限定范围大于等于父类方法 向下兼容
// 

3.11 星期四

单例模式

/ TODO 3/11 设计模式 解决问题一般方案
// 单例模式 某个类只有一个实例
// 饿汉式 空间换时间 速度快 空间大 在类加载就创建实例  线程安全创建类中私有构造 不能实例化 创建私有静态实例 类外不能实例 加载类的时候就实例化  创建公有静态方法
// 懒汉式 静态 直接调用 时间换空间 第一次使用进行实例化 懒汉式线程风险
package com.imooc.singleton;

//饿汉式:创建对象实例的时候直接初始化  空间换时间
public class SingletonOne {
   
	//1、创建类中私有构造
	private SingletonOne(){
   
		
	}
	
	//2、创建该类型的私有静态实例
	private static SingletonOne instance=new SingletonOne();
	
	//3、创建公有静态方法返回静态实例对象
	public static SingletonOne getInstance(){
   
		return instance;
	}
}
package com.imooc.singleton;
//懒汉式:类内实例对象创建时并不直接初始化,直到第一次调用get方法时,才完成初始化操作
//时间换空间
public class SingletonTwo {
   
	//1、创建私有构造方法
	private SingletonTwo(){
   
		
	}
	
	//2、创建静态的该类实例对象
	private static SingletonTwo instance=null;
	
	//3、创建开放的静态方法提供实例对象
	public static SingletonTwo getInstance(){
   
		if(instance==null)
			instance=new SingletonTwo();
		
		return instance;
	}
}
package com.imooc.test;

import com.imooc.singleton.SingletonOne;
import com.imooc.singleton.SingletonTwo;

public class Test {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		SingletonOne one =SingletonOne.getInstance();
		SingletonOne two=SingletonOne.getInstance();
		System.out.println(one);
		System.out.println(two);
		System.out.println("===================================");
		SingletonTwo one1 =SingletonTwo.getInstance();
		SingletonTwo two1=SingletonTwo.getInstance();
		System.out.println(one1);
		System.out.println(two1);
	}

}


3.12 星期五

多态

// TODO 3.12 override 注解 art+/
//多态 不同类的对象对同一个信息做出不同响应 满足继承关系 父类引用指向子类对象
// 向上转型 小类转型父类 Animal one = new Cat(); 父类引用指向子类实例 可以调用子类重写的方法
// 向下转型 子类指向父类 强制类型转换 Cat temp =(Cat) one; 调用子类方法
// instanceof 运算符 one instanceof Cat   对象是否有该实例类的特征 true or false
// 静态方法不能重写  static

package com.imooc.animal;

//抽象类:不允许实例化,可以通过向上转型,指向子类实例
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值