





Object01.java
public class Object01 {
public static void main(String[] args) {
//一.单独变量解决问题,不利于数据管理
//第1只猫信息
// String cat1Name = "小白";
// int cat1Age = 3;
// String cat1Color = "白色";
// //第2只猫信息
// String cat2Name = "小花";
// int cat2Age = 100;
// String cat2Color = "花色";
// //二.数组
// //1.数据类型体现不出来
// //2.只能通过下标获取信息,造成变量名字和内容的对应关系不明确
// //3.不能体现猫的行为
// String[] cat1 = {"小白","3","白色"};
// String[] cat1 = {"小花","3","花色"};
//三.使用面向对象得到方式来解决养猫问题
//使用OOP面向对象解决
//实例化一只猫
//1.new Cat() 创建一只猫
//2.Cat cat1 = new Cat();把创建的猫赋值给cat1
//3.cat1就是一个对象
Cat cat1 = new Cat();
cat1.name = "小白";
cat1.age = 3;
cat1.color = "白色";
cat1.weight = 10;
//创建第二只猫,并赋值给cat2
//cat2也是一个对象
Cat cat2 = new Cat();
cat2.name = "小花";
cat2.age = 100;
cat2.color = "花色";
cat2.weight = 20;
//怎么访问对象的属性
System.out.println("第一只猫的信息" + cat1.name
+ " " + cat1.age + " " + cat1.color + " " + cat1.weight);
System.out.println("第一只猫的信息" + cat2.name
+ " " + cat2.age + " " + cat2.color + " " + cat2.weight);
}
}
//定义一个猫类 Cat ->自定义数据类型
class Cat {
//属性(成员变量)
String name;
int age;
String color;
double weight;
//行为
}



Object02.java
public class Object02 {
public static void main(String[] args) {
}
}
class Car {
String name;//属性,成员变量,字段field
double price;
String color;
String[] master;//属性可以是基本数据类型,也可以是引用类型(数组,对象)
}

PropertiesDetail.java
public class PropertiesDetail {
public static void main(String[] args) {
//创建Person对象
//p1是对象名(对象引用)
//new Person() 创建的对象空间(数据)才是真正的对象
Person p1 = new Person();
//对象的属性默认值,遵守数组规则:
System.out.println("\n当前这个人的信息");
System.out.println("age=" + p1.age + " name=" + p1.name
+ " sal=" + p1.sal + " isPass=" + p1.isPass);
}
}
class Person {
//四个属性
int age;
String name;
double sal;
boolean isPass;
}
//当前这个人的信息:age=0 name=null sal=0.0 isPass=false




Object03.java
public class Object03 {
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "小明";
p1.age = 10;
Person p2 = p1;//把p1赋给p2,让p2指向p1
System.out.println(p2.age);
}
}
class Person {
String name;
int age;
}
//结果为
//10




Method01.java
import java.util.Scanner;
public class Method01 {
public static void main(String[] args) {
//方法使用
//1.方法写好不调用,不会输出
//2.先创建对象,然后调用方法即可
//
Person p1 = new Person();
p1.speak();//调用speak方法
p1.cal01();//调用cal01方法
p1.cal02(5);//调用cal02方法,同时给n=5
p1.cal02(10);//调用cal02方法,同时给n=10
//调用getSum方法,同时num1=10,num2=20
//把方法getSum返回的值,赋给变量returnSum
int returnSum = p1.getSum(80,20);
System.out.println("getSum方法返回的值=" + returnSum);
}
}
class Person {
String name;
int age;
//方法(成员方法)
//添加speak成员方法,输出“我是一个好人”
//1.public 表示方法是公开的
//2.void 表示方法没有返回值
//3.speak() speak是方法名,()形参列表
//4.{}表示方法体,要执行的代码
//5.System.out.println("我是一个好人");表示方法就是要输出一句话
public void speak() {
System.out.println("我是一个好人");
}
//添加cal01成员方法,可以计算从1+..+1000的结果
public void cal01() {
//循环完成
int sum = 0;
for(int i = 1;i <= 1000;i++){
sum +=i;
}
System.out.println("cal01的sum=" + sum);
}
//添加cal02成员方法,该方法可以接收一个数n,计算1+..+n的结果
//1.(int n)形参列表,表示当前有一个形参n,可以接收用户的输入
public void cal02(int n) {
// Scanner myScanner = new Scanner(System.in);
// System.out.println("计算1+..+n,请输入n等于?");
// int n = myScanner.nextInt();
int sum = 0;
for(int i = 1;i <= n;i++){
sum += i;
}
System.out.println("cal02的sum=" + sum);
}
//添加getSum成员方法,可以计算两个数的和
//1.public 表示方法是公开的
//2.int 表示方法执行后,返回一个int值
//3.getSum 方法名
//4.(int num1,int num2)形参列表,2个形参,可以接收用户传入的两个数
//5.return sum;表示把sum的值返回
public int getSum(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
}


Method02.java
public class Method02 {
public static void main(String[] args) {
//请遍历一个数组,输出数组的各个元素值
int[][] map = {{0,0,0},{1,1,1},{1,1,3}};
//使用方法完成输出,创建MyTools对象
MyTools tool = new MyTools();
//遍历map数组
//传统的解决方法就是直接遍历
// for(int i = 0;i < map.length;i++){
// for(int j =0;j < map[i].length;j++){
// System.out.print(map[i][j] + " ");
// }
// System.out.println();
// }
// 使用方法
tool.printArr(map);
//要求再次遍历map数组
tool.printArr(map);
}
}
//把输出的功能,写到一个类的方法中,然后调用该方法即可
class MyTools {
//方法接收一个二维数组
public void printArr(int[][] map) {
System.out.println("========");
//对传入的map数组进行遍历输出
for(int i = 0;i < map.length;i++){
for(int j =0;j < map[i].length;j++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}



MethodDetail.java
public class MethodDetail {
public static void main(String[] args) {
AA a = new AA();
int[] res = a.getSumAndSub(1,4);
System.out.println("和=" + res[0]);
System.out.println("差=" + res[1]);
}
}
//细节:调用带参数的方法时,一定对应着参数列表传入相同类型或兼容类型的参数
// byte b1 = 1;
// byte b2 = 2;
// a.getSumAndSub(b1,b2);//byte -> int
// a.getSumAndSub(1.1,1.8);//double -> int(x)
//细节:实参和形参的类型要一致或兼容、个数、顺序必须一致
// a.getSumAndSub(100);//x 个数不一致
//a.f3("tom",10);//ok
// a.f3(100,"jack");//实际参数和形式参数顺序不对
class AA {
public void f3(String str,int n){
}
// //方法不能嵌套定义
// public void f4(){
// public void f5(){
// }
// }
//1.一个方法最多有一个返回值[思考如何返回多个结果,返回数组]
public int[] getSumAndSub(int n1,int n2) {
int[] resArr = new int[2];//创建一个数组
resArr[0] = n1 + n2;
resArr[1] = n1 - n2;
return resArr;
}
// 2.返回类型可以为任意类型,包含基本类型或引用类型(数组,对象)
//看getSumAndSub
//3.如果方法要求有返回数据类型,则方法体中最后的执行语句必须为return值;
//而且要求返回值类型必须和return的值类型一致或兼容
public double f1() {
double d1 = 1.1 * 3;
return d1;
// return 1.1;
//int n = 100;
//return n;//int -> double,正确
}
//4.如果方法是void,则方法体中可以没有return语句,或者只写return;
public void f2() {
System.out.println("hello");
}
}

MethodDetail02.java
public class MethodDetail02 {
public static void main(String[] args) {
A a = new A();
a.sayOk();
a.m1();
}
}
class A {
//1.同一个类中的方法调用:直接调用即可
public void print(int n) {
System.out.println("print()方法被调用 n=" + n);
}
public void sayOk() {//sayOk调用 print(直接调用即可)
print(10);
System.out.println("继续执行sayOk()");
}
//2.跨类中的方法A类调用B类方法:需要通过对象名调用
public void m1() {
//先创建B对象,然后再调用方法即可
System.out.println("============");
System.out.println("m1()方法被调用");
B b = new B();
b.hi();
System.out.println("m1()方法继续执行");
}
}
class B {
public void hi() {
System.out.println("B类中的hi()被执行");
}
}


MethodExercise01.java
public class MethodExercise01 {
public static void main(String[] args) {
AA a = new AA();
if(a.isOdd(9)){
System.out.println("是奇数");
}else{
System.out.println("是偶数");
}
a.print(4,6,'#');
}
}
class AA {
//1.编写类AA,有一个方法:
//判断一个数是奇数odd还是偶数,返回boolean
//思路:
//1.方法的返回类型 boolean
//2.方法的名称 isOdd
//3.方法的形参 (int num)
//4.方法体,判断
public boolean isOdd(int num) {
// ============
// if(num % 2 != 0){
// return true;
// }else{
// return false;
// }
// ============
// return num % 2 != 0 ? true ; false;
// ============
return num % 2 != 0;
}
//2.根据行、列、字符打印对应行数和列数的字符
//比如:行:4,列:4,字符#,则打印相应的效果
//思路:
//1.方法的返回类型 void
//2.方法的名称 print
//3.方法的形参 (int row,int col,char c)
//4.方法体,循环
public void print(int row,int col,char c) {
for(int i = 1;i <= row;i++){
for(int j = 1;j <= col;j++){
System.out.print(c);
}
System.out.println();
}
}
}

MethodParameter01.java
public class MethodParameter01 {
public static void main(String[] args) {
//创建AA对象名字为obj
AA obj = new AA();
int a = 10;
int b = 20;
obj.swap(a,b);//调用swap方法
System.out.println("\nmain方法里\na=" + a + "\tb=" + b);
//a=10 b=20
}
}
class AA {
public void swap(int a,int b) {
System.out.println("\na和b交换前的值\na=" + a + "\tb=" + b);
//a=10 b=20
//完成了a 和 b的交换
int tmp = a;
a = b;
b = tmp;
System.out.println("\na和b交换后的值\na=" + a + "\tb=" + b);
//a=20 b=10
}
}

MethodParameter02.java
public class MethodParameter02 {
public static void main(String[] args) {
B b = new B();
int[] arr = {1,2,3};
b.test100(arr);//调用方法
//遍历数组
System.out.println("main的arr数组");
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println();//200 2 3
System.out.println("================");
Person p = new Person();
p.name = "jack";
p.age = 10;
b.test200(p);
// 测试题如果test200执行的是p.age = 10000;
// System.out.println("main的p.age =" + p.age);//10000
// System.out.println("================");
// 测试题如果test200执行的是p=null
// System.out.println("main的p.age =" + p.age);//10
// 测试题如果test200执行的是p = new person()...结果是
System.out.println("main的p.age =" + p.age);//10
}
}
class Person {
String name;
int age;
}
class B {
public void test200(Person p) {
// p.age = 10000;//修改对象属性
// p = null;
p = new Person();
p.name = "tom";
p.age = 99;
}
//B类中 编写一个方法test100
//可以接收一个数组,在方法中修改该数组,看看原来的数组是否变化
public void test100(int[] arr){
arr[0] = 200;
//遍历数据
System.out.println("test100 的arr数组");
for(int i = 0 ;i < arr.length;i++){
System.out.print(arr[i] + " ");
}
System.out.println();//200 2 3
}
}

MethodExercise02.java
public class MethodExercise02 {
public static void main(String[] args) {
Person p = new Person();
p.name = "milan";
p.age = 100;
MyTools tools = new MyTools();
Person p2 = tools.copyPerson(p);
// p和p2是Person对象,但是是两个独立的对象,属性相同
System.out.println("p的属性 age=" + p.age + " name=" + p.name);
System.out.println("p2的属性 age=" + p2.age + " name=" + p2.name);
//通过同对象比较看看,是否为同一个对象
System.out.println(p == p2);
}
}
class Person {
String name;
int age;
}
class MyTools {
//编写一个方法copyPerson,可以复制一个Person对象,
//返回复制的对象。克隆对象,注意要求得到新对象和原来
//的对象是两个独立的对象,只是他们的属性相同
//
//编写方法的思路
//1.方法的返回类型 Person
//2.方法的对象copyPerson
//3.方法的形参(Person p)
//4.方法体,创建一个新对象,并复制属性返回即可
//
public Person copyPerson(Person p) {
//创建一个新的对象
Person p2 = new Person();
p2.name = p.name;//把原来对象的名字赋值给p2.age
p2.age = p.age;//把原来对象的年龄赋值给p2.age
return p2;
}
}






recursion01.java
public class recursion01 {
public static void main(String[] args) {
T t1 = new T();
t1.test(4);//结果 n=2 n=3 n=4
int res = t1.factorial(5);
System.out.println("res=" + res);//结果为120
}
}
class T {
public void test(int n) {
if(n > 2){
test(n-1);
}//else{
// System.out.println("n=" + n);
//}//如果有else,则结果n=2,不输出后面的
System.out.println("n=" + n);
}
public int factorial(int n) {
if(n == 1) {
return 1;
}else{
return factorial(n-1) * n;
}
}
}


RecursionExercise01.java
public class RecursionExercise01 {
public static void main(String[] args) {
T t1 = new T();
int n = 8;
int res = t1.fibonacci(n);
if(res != -1){
System.out.println("当n="+ n +"对应的斐波那契数=" + res);
}
System.out.println("=========");
int day = 1;
int peachNum = t1.peach(day);
if(peachNum != -1){
System.out.println("第"+ day +"天有" + peachNum + "个桃子");
}
}
}
class T {
/*
请使用递归方式求出斐波那契数1,1,2,3,5,8,13...
给你一个整数n,求出它的值是多少
思路分析
1.当n=1,斐波那契数是1
2.当n=2,斐波那契数是1
3.当n >= 3,斐波那契数是前两个数的和
4.这就是一个递归的思路
*/
public int fibonacci(int n){
if(n >= 1){
if(n == 1 || n == 2){
return 1;
}else{
return fibonacci(n-1) + fibonacci(n-2);
}
}else{
System.out.println("输入的数需要n>=1的整数");
return -1;
}
}
/*
猴子吃桃子问题:有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个!
以后每天猴子都吃其中的一半,然后再多吃一个。当到第10天时,想再吃时
(还没吃),发现只有一个桃子了,问:最初共多少个桃子?
思路分析(逆推)
1.第10天,还剩一个桃子
2.第9天,有(day10 + 1) * 2 = 4
3.第8天,有(day9 + 1) * 2 = 10
4.规律:前一天的桃子=(后一天的桃子+1) * 2
*/
public int peach(int days) {
if(days == 10){
return 1;
}else if(days >= 1 && days <= 9){
return (peach(days + 1) + 1) * 2;
}else{
System.out.println("天数在1~10天,输入的不对");
return -1;
}
}
}

MiGong.java
public class MiGong {
public static void main(String[] args) {
//思路
//1.先创建一个迷宫,用二维数组表示int[][] map = new int[8][7];
//2.先规定map数组的元素值:0表示可以走,1表示障碍物
int[][] map = new int[8][7];
//3.将最上面的一行和最下面的一行全部设置为1
for(int i = 0;i < 7;i++){
map[0][i] = 1;
map[7][i] = 1;
}
// 4.将最右面的一列和最左边的一列,全部设置为1
for(int i = 0;i < 8;i++){
map[i][0] = 1;
map[i][6] = 1;
}
map[3][1] = 1;
map[3][2] = 1;
// ==测试回溯==
// map[2][2] = 1;
//
//输出当前的地图
System.out.println("==当前地图==");
for(int i = 0;i < map.length;i++){
for(int j = 0;j < map[i].length;j++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
//使用findWay给老鼠找路
T t1 = new T();
t1.findWay(map,1,1);
System.out.println("\n==使用findWay找路的情况如下==");
for(int i = 0;i < map.length;i++){
for(int j = 0;j < map[i].length;j++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
//使用findWay2给老鼠找路
// T t1 = new T();
// t1.findWay2(map,1,1);
// System.out.println("\n==使用findWay2找路的情况如下==");
// for(int i = 0;i < map.length;i++){
// for(int j = 0;j < map[i].length;j++){
// System.out.print(map[i][j] + " ");
// }
// System.out.println();
// }
}
}
class T {
//使用递归回溯的思想解决老鼠出迷宫
//1.findway方法是专门找出迷宫的路径
//2.如果找到,就返回true,否则就返回false
//3.map就是二维数组,即表示迷宫
//4.i,j就是老鼠的位置,初始化的位置为map[1][1]
//5.递归找路,所以规定map数组各个值的含义
// 0表示可以走,1表示障碍物,2表示可以走,3表示走过但是走不通
//6.当map[6][5] = 2,就说明找到通路,就可以结束,否则就继续找。
//7.先确定老鼠找路的策略,下->右->上->左
public boolean findWay(int[][] map,int i,int j){
if(map[6][5] == 2){//说明已经找到
return true;
}else{
if(map[i][j] == 0){//当前这个位置0,说明可以走}
//假定可以走通
map[i][j] = 2;
//使用找路策略来确定该位置是否真的可以走通
//下->右->上->左
if(findWay(map,i + 1,j)){//先走下
return true;
}else if(findWay(map,i,j + 1)){//右
return true;
}else if(findWay(map,i-1,j)){//上
return true;
}else if(findWay(map,i,j - 1)){//左
return true;
}else{
map[i][j] = 3;
return false;
}
}else{//map[i][j] = 1,2,3
return false;
}
}
}
//修改找路策略,看看路径是否变化
//上右下左
public boolean findWay2(int[][] map,int i,int j){
if(map[6][5] == 2){//说明已经找到
return true;
}else{
if(map[i][j] == 0){//当前这个位置0,说明可以走}
//假定可以走通
map[i][j] = 2;
//使用找路策略来确定该位置是否真的可以走通
//下->右->上->左
if(findWay2(map,i-1,j)){//先走上
return true;
}else if(findWay2(map,i,j + 1)){//右
return true;
}else if(findWay2(map,i + 1,j)){//下
return true;
}else if(findWay2(map,i,j - 1)){//左
return true;
}else{
map[i][j] = 3;
return false;
}
}else{//map[i][j] = 1,2,3
return false;
}
}
}
}
// ==当前地图==
// 1 1 1 1 1 1 1
// 1 0 0 0 0 0 1
// 1 0 0 0 0 0 1
// 1 1 1 0 0 0 1
// 1 0 0 0 0 0 1
// 1 0 0 0 0 0 1
// 1 0 0 0 0 0 1
// 1 1 1 1 1 1 1
// ==使用findWay找路的情况如下==
// 1 1 1 1 1 1 1
// 1 2 0 0 0 0 1
// 1 2 2 2 0 0 1
// 1 1 1 2 0 0 1
// 1 0 0 2 0 0 1
// 1 0 0 2 0 0 1
// 1 0 0 2 2 2 1
// 1 1 1 1 1 1 1
//
// ==回溯==
// 1 1 1 1 1 1 1
// 1 2 2 2 0 0 1
// 1 3 1 2 0 0 1
// 1 1 1 2 0 0 1
// 1 0 0 2 0 0 1
// 1 0 0 2 0 0 1
// 1 0 0 2 2 2 1
// 1 1 1 1 1 1 1
//
// ==使用findWay2找路的情况如下==
// 1 1 1 1 1 1 1
// 1 2 2 2 2 2 1
// 1 0 0 0 0 2 1
// 1 1 1 0 0 2 1
// 1 0 0 0 0 2 1
// 1 0 0 0 0 2 1
// 1 0 0 0 0 2 1
// 1 1 1 1 1 1 1


HanoiTower.java
public class HanoiTower {
public static void main(String[] args) {
Tower tower = new Tower();
tower.move(5,'A','B','C');
}
}
class Tower {
//方法
//num表示要移动的个数,a,b,c分别表示A塔,B塔,C塔
public void move(int num,char a,char b,char c) {
//如果只有一个盘 num=1
if(num == 1) {
System.out.println(a + "-" + c);
}else{
//如果有多个盘,可以看成两个,最下面和上面的所有盘(num-1)
//1.先移动上面的所有的盘到b,借助c
move(num - 1,a,c,b);
//2.把最下面的盘移动到c
System.out.println(a + "-" + c);
//3.再把b塔的所有盘,移动到c,借助a塔
move(num - 1,b,a,c);
}
}
}





OverLoad01.java
public class OverLoad01 {
public static void main(String[] args) {
//重载的介绍
// System.out.println(100);
// System.out.println("hello,world");
// System.out.println('h');
// System.out.println(1.1);
// System.out.println(true);
MyCalculator overload = new MyCalculator();
System.out.println(overload.calculate(1,2));
System.out.println(overload.calculate(1,2.1));
System.out.println(overload.calculate(1.1,2));
}
}
class MyCalculator {
//下面四个calculate方法构成了重载
//两个整数的和
public int calculate(int n1,int n2){
System.out.println("calculate(int n1,int n2)被调用");
return n1 + n2;
}
// 没有构成方法重构,方法重复定义
// public void calculate(int n1,int n2){
// System.out.println("calculate(int n1,int n2)被调用");
// int res = n1 + n2;
// }
// 没有构成方法重构,方法重复定义
// public void calculate(int a1,int a2){
// System.out.println("calculate(int a1,int a2)被调用");
// int res = a1 + a2;
// }
public double calculate(int n1,double n2){
System.out.println("calculate(int n1,double n2)被调用");
return n1 + n2;
}
public double calculate(double n1,int n2){
System.out.println("calculate(double n1,int n2)被调用");
return n1 + n2;
}
public int calculate(int n1,int n2,int n3){
System.out.println("calculate(int n1,int n2,int n3)被调用");
return n1 + n2 + n3;
}
}


OverLoadExercise.java
public class OverLoadExercise {
public static void main(String[] args){
Methods m1 = new Methods();
System.out.println(m1.m(2));
System.out.println(m1.m(2,3));
System.out.println(m1.m("hello"));
System.out.println("=====");
System.out.println(m1.max(10,22));
System.out.println(m1.max(11.8,22.5));
System.out.println(m1.max(11.8,72.5,69.0));
}
}
class Methods {
public int m(int n){
System.out.println("一个int参数");
return n * n;
}
// public void m(int n){
// System.out.println("一个int参数");
// System.out.println("相乘=" + (n1 * n2));
// }
public int m(int n1,int n2){
System.out.println("两个int参数");
return n1 * n2;
}
public String m(String n){
System.out.println("一个字符串参数");
return n;
}
//==============================================
public int max(int n1,int n2){
return n1 > n2 ? n1 : n2;
}
public double max(double n1,double n2){
return n1 > n2 ? n1 : n2;
}
public double max(double n1,double n2,double n3){
// double max1 = n1 > n2 ? n1 : n2;
// double max2 = max1 > n3 ? max1 : n3;
// return max2;
return (n1 > n2 ? n1 : n2) > n3 ? (n1 > n2 ? n1 : n2) : n3;
}
//下面方法和上面方法构成重载,优先级是,调用什么就先使用哪个方法。
public double max(double n1,double n2,int n3){
// double max1 = n1 > n2 ? n1 : n2;
// double max2 = max1 > n3 ? max1 : n3;
// return max2;
return (n1 > n2 ? n1 : n2) > n3 ? (n1 > n2 ? n1 : n2) : n3;
}
}

VarParameter01.java
public class VarParameter01 {
public static void main(String[] args) {
HspMthod m = new HspMthod();
System.out.println(m.sum(21,3,5));
}
}
class HspMthod {
//可以计算2个数的和,3个数的和,4,5...的和
// public int sum(int n1,int n2) {
// return n1 + n2;
// }
// public int sum(int n1,int n2,int n3) {
// return n1 + n2 + n3;
// }
// public int sum(int n1,int n2,int n3,int n4) {
// return n1 + n2 + n4;
// }
//...
//上面的三个方法名称相同,功能相同,参数个数不同->使用可变参数优化
//1.int...表示接收的是可变参数,类型是int,可以接受多个int
//2.使用可变参数时,可以当做数组来使用,即nums可以当做数组
//3.遍历nums求和
public int sum(int... nums) {
System.out.println("接收的参数个数=" + nums.length);
int res = 0;
for(int i = 0;i < nums.length;i++){
res += nums[i];
}
return res;
}
}

VarParameterDetail.java
public class VarParameterDetail {
public static void main(String[] args) {
//细节:可变参数的实参可以是数组
int[] arr = {1,2,3};
T t1 = new T();
t1.f1(arr);
}
}
class T {
public void f1(int... nums) {
System.out.println("长度=" + nums.length);
}
//细节:可变参数可以和普通类型的参数一起放在形参列表
//但必须保证可变参数在最后
public void f2(String str,double... nums) {
}
// 细节:一个形参列表中只能出现一个可变参数
// public void f3(int... nums1,double... nums2) {
// }
}

VarParameterExercise.java
public class VarParameterExercise {
public static void main(String[] args) {
HspMethod h1 = new HspMethod();
System.out.println(h1.showScore("xiaom",90.1,80.2));
System.out.println(h1.showScore("terry",10,30.3,69,77));
}
}
class HspMethod {
// 分析
// 1.方法名showScore
// 2.形参(String,double...)
// 3.返回String
public String showScore(String name,double... score){
double sum = 0;
for(int i =0;i < score.length;i++){
sum += score[i];
}
return name + score.length + "门课成绩总分为" + sum;
}
}

VarScope.java
public class VarScope {
public static void main(String[] args){
}
}
class cat{
//全局变量:也就是属性,作用域为整个类体,Cat类:cry eat等方法使用属性
//属性在定义时,可以直接赋值
int age = 10;//指定的值是10
//全局变量(属性)可以不赋值,直接使用,因为有默认值
double weight;//有默认值0.0
public void hi() {
//局部变量必须赋值后,才能使用,因为没有默认值
// int num;//错误
// System.out.println("num=" + num);
}
//
public void cry(){
//1.局部变量一般是指在成员方法中定义的变量
//2.n和name就是局部变量
//3.n和name作用域在cry方法中
int n = 10;
String name = "jack";
System.out.println("在cry中使用属性 age=" + age);
}
public void eat() {
System.out.println("在eat中使用属性 age=" + age);
// System.out.println("在eat中不可以使用属性cry的变量 name=" + name);
}
}


VarScopeDetail.java
public class VarScopeDetail {
public static void main(String[] args) {
//
Person p1 = new Person();
//细节3:属性生命周期较长,伴随着对象的创建而创建,伴随着对象的销毁而销毁
//局部变量,声明周期较短,伴随着它的代码块的执行而创建
//伴随着代码块的结束而销毁。即在一次方法调用过程中
p1.say();
/*结果:king
当执行say方法时,say方法的局部变量比如name,会创建,当
say方法执行完毕后,name局部变量就销毁了,但是属性(全局变量)
仍然可以使用
*/
T t1 = new T();
t1.test();//jack//第一种跨类访问对象属性的方式
t1.test2(p1);//jack//第二种跨类访问对象属性的方式
}
}
class T {
public void test(){
//细节4:全局变量/属性:可以被本类使用,或其他类的使用(通过对象调用)
Person p1 = new Person();
System.out.println(p1.name);
}
public void test2(Person p) {
System.out.println(p.name);
}
}
class Person {
//细节5:属性可以加修饰符(public protected private..)
//局部变量就不可以
private int age = 20;
String name = "jack";
public void say(){
//细节1:属性和局部变量可以重名,访问时遵循就近原则
String name = "king";
System.out.println("say() name=" + name);
}
public void hi(){
//细节2:在同一个作用域中,比如在同一个成员方法中,两个局部变量,不能重名.
String address = "北京";
// String address = "上海";//错误,重复定义变量
String name = "hsp";//可以定义
}
}



Constructor01.java
public class Constructor01 {
public static void main(String[] args) {
//当我们new一个对象时,直接通过构造器指定名字和年龄
Person p1 = new Person("smith",80);
System.out.println("p1的信息如下");
System.out.println("p1对象name=" + p1.name);//smith
System.out.println("p1对象age=" + p1.age);//80
}
}
//在创建人类的对象时,直接指定这个对象的年龄和姓名
//
class Person {
String name;
int age;
//构造器
//1.构造器没有返回值,也不能写void
//2.构造器的名称和类名Person一样
//3.(String pName,int pAge)是构造器形参列表,规则和成员方法一样
public Person(String pName,int pAge) {
System.out.println("构造器被调用~,完成对象属性的初始化");
name = pName;
age = pAge;
}
}


ConstructorDetail.java
public class ConstructorDetail {
public static void main(String[] args){
Person p1 = new Person("king",40);//第一个构造器
Person p2 = new Person("tom");//第二个构造器
// p1.Person("king",40);错误
Dog d1 = new Dog();//()调用默认构造器
}
}
class Person{
String name;
int age;
//第一个构造器
public Person(String pName,int pAge){
name = pName;
age = pAge;
}
//第二个构造器,只指定人名,不需要指定年龄
public Person(String pName) {
name = pName;
}
}
class Dog {
//6.如果程序员没有定义构造器,系统会自动给类生成一个默认无参构造器
//又称为(默认构造器),使用javap反编译查看
/*
默认构造器
Dog() {
}
*/
//7.一旦定义了自己的构造器,默认的构造器就覆盖了,就不能再使
//用默认的无参构造器了,除非显式的定义一下,即DOg(){}
public Dog(String dName){
//...
}
Dog() {//显式的定义一下 无参构造器
}
}


ConstructorExercise.java
public class ConstructorExercise {
public static void main(String[] args) {
Person p1 = new Person();//无参构造器
//下面结果为name=null age=18
System.out.println("p1的信息 name=" + p1.name + " age= " + p1.age);
Person p2 = new Person("scott",50);
System.out.println("p1的信息 name=" + p2.name + " age= " + p2.age);
}
}
class Person {
String name;//默认值null
int age;//默认0
public Person() {
age = 18;
}
public Person(String pName,int pAge) {
name = pName;
age = pAge;
}
}



This01.java
public class This01 {
public static void main(String[] args) {
Dog d1 = new Dog("大壮",3);
d1.info();
}
}
class Dog{//类
//属性
String name;
int age;
//构造器
// public Dog(String dName,int aAge){
// name = dName;
// age = aAge;
// }
//如果构造器的形参,能够直接写成属性名,就更好
//根据变量的作用域原则,构造器name就是局部变量,而不是属性了
//根据变量的作用域原则,构造器age就是局部变量,而不是属性了
//引出this关键字解决
public Dog(String name,int age){
name = name;
age = age;
}
//成员方法
public void info() {//输出属性信息
System.out.println(name + "\t" + age + "\t");
}
}



This01.java
public class This01 {
public static void main(String[] args) {
Dog dog1 = new Dog("大壮",3);
System.out.println("dog1的hash code=" + dog1.hashCode());
dog1.info();
Dog dog2 = new Dog("大黄",3);
System.out.println("dog2的hash code=" + dog2.hashCode());
dog2.info();
}
}
class Dog{//类
//属性
String name;
int age;
//构造器
// public Dog(String dName,int aAge){
// name = dName;
// age = aAge;
// }
//如果构造器的形参,能够直接写成属性名,就更好
//根据变量的作用域原则,构造器name就是局部变量,而不是属性了
//根据变量的作用域原则,构造器age就是局部变量,而不是属性了
//引出this关键字解决
public Dog(String name,int age){
//this.name就是当前对象得到name
this.name = name;
//this.age就是当前对象得到age
this.age = age;
System.out.println("this.hashCode=" +this.hashCode());
}
//成员方法
public void info() {//输出属性信息
System.out.println(name + "\t" + age + "\t");
}
}
// 结果:
// this.hashCode=366712642
// dog1的hash code=366712642
// 大壮 3
// this.hashCode=1829164700
// dog2的hash code=1829164700
// 大黄 3

ThisDetail.java
public class ThisDetail {
public static void main(String[] args) {
// T t1 = new T();
// t1.f2();
T t2 = new T();
t2.f3();
}
}
class T {
String name = "jack";
int age = 100;
//细节4:访问构造器语法:this(参数列表);
//注意只能在构造器中使用(即只能在构造器中访问另外一个构造器)
//
//访问构造器语法:有this(参数列表);必须放在第一条语句
public T(){
//这里访问T(String name,int age)构造器
this("jack",100);
System.out.println("T()构造器");
}
public T(String name,int age){
System.out.println("T(String name,int age)构造器");
}
//细节1:this关键字可以用来访问本类的属性、方法、构造器
public void f3() {
//传统方式,就近原则,局部变量会影响
String name = "Smith";
System.out.println("name=" + name + " age = " + age);//smith 100
// 也可以通过this来访问属性
System.out.println("name=" + this.name + " age = " + this.age);//jack 100
}
//细节3:访问成员方法的语法:this.方法名(参数列表);
public void f1(){
System.out.println("f1()方法..");
}
public void f2(){
System.out.println("f2()方法..");
//调用本类的f1
//第一种方式
f1();
//第二种方式
this.f1();
}
}

TestPerson.java
public class TestPerson {
public static void main(String[] args) {
Person p1 = new Person("mary",20);
Person p2 = new Person("mary",20);
System.out.println("p1和p2比较的结果=" + p1.compareTo(p2));
}
}
class Person{
String name;
int age;
//构造器
public Person(String name,int age){
this.name = name;
this.age = age;
}
//compareTo比较方法
public boolean compareTo(Person p){
// if(this.name.equals(p.name) && this.age == p.age){
// return true;
// }else{
// return false;
// }
return this.name.equals(p.name) && this.age == p.age;
}
}

Homework01.java
public class Homework01 {
public static void main(String[] args) {
A01 a1 = new A01();
double[] arr = {1.3,23.3,342.12,642.2,323};
Double res = a1.max(arr);
if(res != null){
System.out.println("arr的最大值=" + res);
}else{
System.out.println("arr输入有误,数组不能为null,或者{}");
}
}
}
// 思路分析
// 1.类名A01
// 2.方法名max
// 3.形参(double[])
// 4.返回值double
// 先完成正常业务,然后再考虑代码健壮性
class A01 {
//double改为Double包装类,不然不能return null
public Double max(double[] arr){
//先判断arr是否为空,然后再判断length是否>0
if(arr != null && arr.length > 0){
//考虑代码健壮性,保证arr至少有一个元素
double max = arr[0];//假定第一个元素就是最大值
for(int i =1;i < arr.length;i++){
if(max < arr[i]){
max = arr[i];
}
}
return max; //double值
}else{
return null;
}
}
}
Homework02.java
public class Homework02 {
public static void main(String[] args){
String[] strs = {"jack","tom","mary"};
A02 a02 = new A02();
int index = a02.find("tom",strs);
System.out.println("查找的index=" + index);
}
}
//编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,
//并返回索引,如果找不到,返回-1
//分析
//1.类名 A02
//2.方法名 find
//3.返回值 int
//4.形参(String,String[])
//5.补充代码对的健壮性
class A02 {
public int find(String findstr,String[] strs){
//直接遍历字符串数组,如果找到,则返回索引
for(int i = 0;i < strs.length;i++){
if(findstr.equals(strs[i])){
return i;
}
}
//如果找不到,返回-1
return -1;
}
}
Homework03.java
public class Homework03 {
public static void main(String[] args){
Book b1 = new Book("红楼梦",20);
b1.info();
b1.updatePrice();
b1.info();
}
}
//编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:
//如果价格>150,则更改为150,如果价格>100,更改为100,否则不变
//分析
//1.类名 Book
//2.属性price,name
//3.方法名 updatePrice
//4.返回值 void
//5.形参()
//6.提供一个构造器
class Book {
double price;
String name;
public Book(String name,double price){
this.name = name;
this.price = price;
}
public void updatePrice() {
//如果方法中没有price局部变量,this.price等价price
if(price > 150){
price = 120;
}else if(price > 100){
price = 100;
}
}
//显示书籍情况
public void info(){
System.out.println("书名" + this.name + "价格=" + this.price);
}
}

Homework04.java
public class Homework04 {
public static void main(String[] args) {
int[] oldArr = {10,23,23,222,44,2};
A03 a03 = new A03();
int[] newArr = a03.copyArr(oldArr);
//遍历newArr,验证
System.out.println("==返回的新数组==");
for(int i = 0;i < newArr.length;i++){
System.out.print(newArr[i] + " ");
}
}
}
//编写类A03,实现数组的复制功能copyArr,输入旧数组,
//返回一个新数组,元素和旧数组一样
//分析
//1.类名 A03
//2.方法名copyArr
//3.返回值int
//5.形参(int[] oldArr)
class A03{
public int[] copyArr(int[] oldArr){
//在堆中,创建一个长度为oldArr.length数组
int[] newArr = new int[oldArr.length];
//遍历oldArr,将元素拷贝到newArr
for(int i = 0;i < oldArr.length;i++){
newArr[i] = oldArr[i];
}
return newArr;
}
}
Homework05.java
public class Homework05 {
public static void main(String[] args) {
Circle c1 = new Circle(3);
System.out.println("周长等于" + c1.len());
System.out.println("面积等于" + c1.area());
}
}
//定义一个圆类Circle,定义属性:半径,提供显示圆周长,圆面积功能的方法
class Circle {
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double len(){//周长
return radius * 2 * Math.PI;
}
public double area(){//面积
return radius * radius * Math.PI;
}
}
Homework06.java
public class Homework06{
public static void main(String[] args){
Cale cale = new Cale(2,1);
System.out.println("和=" + cale.sum());
System.out.println("差=" + cale.minus());
System.out.println("乘=" + cale.mul());
Double divRes = cale.div();
if(divRes != null){
System.out.println("除=" + divRes);
}
}
}
/*
创建一个Cale计算类,在其中定义2个变量表示两个操作数,
定义四个方法实现求和、差、乘、商(要求除数为0的话,要提示)
并创建两个对象,分别测试
*/
class Cale {
double num1;
double num2;
public Cale(double num1,double num2){
this.num1 = num1;
this.num2 = num2;
}
//和
public double sum(){
return num1 +num2;
}
//差
public double minus(){
return num1 -num2;
}
//乘
public double mul(){
return num1 * num2;
}
//除
public Double div(){
if(num2 != 0){
return num1 / num2;
}else{
System.out.println("num2 不能为0");
return null;
}
}
}

Homework07.java
public class Homework07 {
public static void main(String[] args){
Dog d1 = new Dog("lucy","blue",23);
d1.show();
}
}
class Dog {
String name;
String color;
int age;
public Dog(String name,String color,int age){
this.name = name;
this.color = color;
this.age = age;
}
public void show() {
System.out.println(name);
System.out.println(color);
System.out.println(age);
}
}
Test.java
public class Test { //公有类
int count = 9;//属性
public void count1(){//Test类的成员方法
count = 10;
System.out.println("count1=" + count);
}
public void count2() {//Test类的成员方法
System.out.println("count1=" + count++);
}
//这是Test类的main方法,任何一个类,都可由main
public static void main(String args[]) {
// 1.new Test()是匿名对象,在堆里,只能用一次
// 2.new Test().count1();创建好匿名对象后,就调用了count1()
new Test().count1();//10
Test t1 = new Test();
t1.count2();//9
t1.count2();//10
}
}

Homework09.java
public class Homework09 {
public static void main(String[] args) {
Music music = new Music("笑傲江湖",300);
music.play();
System.out.println(music.getInfo());
}
}
/*
定义Music类,里面有音乐名name,音乐时长times属性,并有播放play功能
和返回本身属性信息的功能方法getInfo
*/
class Music{
String name;
int times;
public Music(String name,int times){
this.name = name;
this.times = times;
}
//播放功能
public void play() {
System.out.println("音乐" + name + "正在播放中...时长" + times);
}
//返回本身属性的方法
public String getInfo(){
return "音乐" + name + "播放时长为" + times;
}
}
Homework10.java
public class Homework10 {
public static void main(String[] args){
}
}
class Demo{
int i = 100;
public void m(){
int j = i++;
System.out.println("i=" + i);
System.out.println("j=" + j);
}
}
class Test{
public static void main(String[] args){
Demo d1 = new Demo();
Demo d2 = d1;
d2.m();//i = 101 j = 100
System.out.println(d1.i);//101
System.out.println(d2.i);//101
}
}

Homework12.java
public class Homework12 {
public static void main(String[] args) {
}
}
/*
创建一个Employee类,属性有(名字,性别,年龄,职位,薪水),
提供3个构造方法,可以初始化
1.(名字,性别,年龄,职位,薪水)
2.(名字,性别,年龄)
3.(职位,薪水),要充分复用构造器
*/
class Employee {
String name;
char gender;
int age;
String job;
double sal;
//因为要求可以复用构造器,先写属性少的构造器
//职位,薪水
public Employee(String job,double sal){
this.job = job;
this.sal = sal;
}
//名字.性别,年龄
public Employee(String name,char gender,int age){
this.name = name;
this.gender = gender;
this.age = age;
}
//名字.性别,年龄,职位,薪水
public Employee(String name,char gender,int age,String job,double sal){
this(name,gender,age);
this.job = job;
this.sal = sal;
}
}

Homework13.java
public class Homework13 {
public static void main(String[] args) {
Circle c = new Circle();
PassObject op = new PassObject();
op.printAreas(c,5);
}
}
class Circle {//类
double radius;//半径
// public Circle(){
// }
// public Circle(radius){
// this.radius = radius;
// }
public double findArea(){
return radius * radius * Math.PI;
}
//添加方法setRadius,修改对象的半径值
public void setRadius(double radius){
this.radius = radius;
}
}
class PassObject {
public void printAreas(Circle c,int times){
System.out.println("radius\tarea");
for(int i = 1;i <= times;i++){
c.setRadius(i);//修改对象的半径值
System.out.println((double)i + "\t" +c.findArea());
}
}
}

MoraGame.java
import java.util.Random;
import java.util.Scanner;
public class MoraGame {
public static void main(String[] args) {
//创建一个玩家对象
Tom t = new Tom();
//用来记录输赢的次数
int isWinCount = 0;
//创建一个二维数组,用来接收局数,To出拳情况以及电脑出拳情况
int[][] arr1 = new int[3][3];
int j = 0;
//创建一个一维数组,用来接收输赢情况
String[] arr2 = new String[3];
Scanner scanner = new Scanner(System.in);
for(int i = 0;i < 3;i++){//比赛 3次
//获取玩家出的拳
System.out.println("请输入你要出的拳(0拳头,1剪刀,2布)");
int num = scanner.nextInt();
t.setTomGuessNum(num);
int tomGuess = t.getTomGuessNum();
arr1[i][j + 1] = tomGuess;
//获取电脑的出拳
int comGuess = t.computerNum();
arr1[i][j + 2] = tomGuess;
//将玩家猜的拳与电脑做比较
String isWin = t.vsComputer();
arr2[i] = isWin;
arr1[i][j] = t.count;
//对每一局情况输出
System.out.println("===============");
System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
System.out.println(t.count +"\t" + tomGuess + "\t\t" + comGuess + "\t\t");
System.out.println("===============");
System.out.println("\n\n");
isWinCount = t.winCount(isWin);
}
//对游戏的最终结果进行输出
System.out.println("局数\t玩家的出拳\t电脑的出拳\t\t输赢情况");
for(int a = 0;a < arr1.length;a++){
for(int b = 0;b < arr1[a].length;b++){
System.out.print(arr1[a][b] + "\t\t\t");
}
System.out.print(arr2[a]);
System.out.println();
}
System.out.println("你赢了" + isWinCount + "次");
}
}
//有个人Tom设计他的成员变量.成员方法,可以电脑猜拳
//电脑每次都会随机生成0,1,2
//0表示石头,1表示剪刀,2表示布
//并要可以显示Tom的输赢次数(清单)
class Tom { //核心代码
//玩家出拳的类型
int tomGuessNum;//0,1,2
//电脑出拳的类型
int comGuessNum;//0,1,2
//玩家赢得次数
int winCountNum;
//比赛的次数
int count = 1;//一共比赛3次
public void showInfo() {
}
//电脑随机生成猜拳数字的方法
public int computerNum() {
Random r = new Random();
comGuessNum = r.nextInt(3);//返回0-2的随机数
return comGuessNum;
}
//设置玩家猜拳的数字的方法
public void setTomGuessNum(int tomGuessNum) {
if(tomGuessNum > 2 || tomGuessNum < 0){
//抛出一个异常
throw new IllegalArgumentException("数字输入 有误");
}
this.tomGuessNum = tomGuessNum;
}
public int getTomGuessNum() {
return tomGuessNum;
}
//比较猜拳的结果
public String vsComputer() {
if(tomGuessNum == 0 && comGuessNum == 1){
return "你赢了";
}else if (tomGuessNum == 1 && comGuessNum ==2){
return "你赢了";
}else if(tomGuessNum ==2 && comGuessNum == 0){
return "你赢了";
}else if(tomGuessNum == comGuessNum){
return "平手";
}else{
return "你输了";
}
}
//记录玩家赢得次数
public int winCount(String s) {
count++;
if(s.equals("你赢了")) {
winCountNum++;
}
return winCountNum;
}
}
MySQL学习笔记