day06
- 变量三要素: 变量类型 变量名 作用范围/声明周期
局部变量 成员变量
变量所处的位置的不同,分为局部变量和成员变量(全局变量、实例变量)
局部变量:
声明在方法中或者是代码块中的变量称之为局部变量。
只在当前声明的方法中有效 出了方法之后啥也不是
变量要声明之后 初始化之后才能被使用
代码:
-
public class Test01 { public static void main(String[] args) { // 声明了一个代码块 { //声明一个变量 int num = 10; System.out.println(num); } System.out.println(); } public static void info() { } }
成员变量:
声明位置: 类中 方法外
在整个类当中都有效,要注意下面代码报错的原因是由于static的问题而不是成员变量的问题。
在main方法中方法非static修饰的成员变量,需要在成员变量前加static修饰
成员变量都存在默认值:
整形->0
浮点型->0.0
逻辑->false
字符-> 空格 ‘\u0000’
引用类型->null
- 代码块:
以一组大括号组成的一个代码段 代码块
public class Test02 {
static int num;
static byte b;
static short s;
static long l;
static float f;
static double d;
static char ch;
static boolean flag;
static String str;
public static void main(String[] args) {
System.out.println(num);
//int result ;
//System.out.println(result);
System.out.println(b);
System.out.println(s);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(flag);
System.out.println(str);
System.out.println("+++"+ch+"+++");
}
}
递归
一种编程技巧,程序中自己调用自己
递归头 终止环节
递归体
优势: 能够更快的写出代码 复杂度特别低
劣势: 特别消耗内存
凡是可以使用递归程序的都可以使用循环,但是能够使用循环的不一定能够使用递归
使用场景:数据结构和算法
能够做到栈帧复用的递归程序称之为尾递归
计算阶乘:
1! = 1
2! = 1*2 2! = 1!*2
3! = 123 3! = 2!*3
…
n! = (n-1)!*n
格式:
if(){
return 1;
}else{
return fun(n-1)*n
}
递归图:
尾递归图:
代码:
public class Test03 {
public static void main(String[] args) {
System.out.println("通过循环计算一个数的阶乘");
//声明变量存放阶乘的结果
int result = 1;
for(int i =1;i<=5;i++) {
result = result*i;
}
System.out.println(result);
result = fun(30);
System.out.println(result);
}
//定义一个方法计算一个数的阶乘
public static int fun(int num) {
if(num==1) {
return 1;
}else {
return fun(num-1)*num;
}
}
}
- 尾递归
/**
* 斐波那契数列:
* 1 1 2 3 5 8 13 21 。。。
*
* 第一项 : 1
* 第二项 : 1
* 第三项 : 第一项+第二项
* 。。。。。。
* f(n) = f(n-1)+f(n-2)
*
*
* 4 1 1
* 计算的第n项 前一项 前两项
*
*
* @author wawjy
*
*/
public class Test04 {
public static void main(String[] args) {
//定义计算的项数
int num = 50;
System.out.println("使用循环完成斐波那契数列");
//记录时间 获取到的是当前时间的毫秒数
long start = System.currentTimeMillis();
long num1 = 1;
long num2 = 1;
long result = 0;
for(int i = 3;i<=num;i++) {
result = num1+num2;
num1 = num2;
num2 = result;
}
long end = System.currentTimeMillis();
System.out.println("循环之后第30项的值是:"+result+",总耗时:"+(end-start));
/*
* start = System.currentTimeMillis(); result = method(num); end =
* System.currentTimeMillis();
*
* System.out.println("递归之后第30项的值是:"+result+",总耗时:"+(end-start));
*/
start = System.currentTimeMillis();
result = method(num,1,1);
end = System.currentTimeMillis();
System.out.println("递归之后第50项的值是:"+result+",总耗时:"+(end-start));
}
public static long method(int num) {
if(num==1||num==2) {
return 1;
}
return method(num-1)+method(num-2);
}
public static long method(long num,long ret1,long ret2) {
if(num==1) {
return ret1;
}
return method(num-1,ret2,ret1+ret2);
}
public class test05 {
public static void main(String[] args) {
print07();
}
public static void print07() {
char ch = 'I';
for(int x = -5;x<=5;x++) {
for(int y = -5;y<=5;y++) {
if((x>0?x:-x)+(y>0?y:-y)<=5) {
System.out.print((char)(ch+y));
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
/**
* *******
*/
public static void print01() {
System.out.println("*******");
//每次打印一个星星
for(int i = 1;i<=7;i++) {
System.out.print("*");
}
System.out.println();
}
/**
* 矩形
*/
public static void print02() {
System.out.println("*******");
System.out.println("*******");
System.out.println("*******");
System.out.println("*******");
System.out.println("循环打印: 4行");
for(int i = 1;i<=4;i++) {
System.out.println("*******");
}
System.out.println("循环每次打印一个星星");
for(int i = 1;i<=4;i++) {//行数
for(int j = 1;j<=7;j++) {//星星个数
System.out.print("*");
}
System.out.println();
}
}
/**
*
* *******
*******
*******
*******
*
*
* *
* ***
* *****
* *******
*
* 1 1
* 2 3
* 3 5
* 4 7
* n 2*n-1
*
*
*/
public static void print03() {
for(int i = 1;i<=4;i++) {//控制行数
for(int j = 1;j<=2*i-1;j++) {//控制星星个数的时候和行有关
System.out.print("*");
}
System.out.println();
}
}
/**
*
*
* *
* ***
* *****
* *******
* 行数 1 2 3 4 n
* 空格 3 2 1 0 总行数-n
* 星星 1 3 5 7 2*n-1
*
*
*/
public static void print04() {
for(int i = 1;i<=4;i++) {//控制行数
for(int k = 1;k<=4-i;k++) {//空格的个数
System.out.print(" ");
}
for(int j = 1;j<=2*i-1;j++) {//控制星星个数的时候和行有关
System.out.print("*");
}
System.out.println();
}
}
/**
*
*
* *
* ***
* *****
* *******
* 行数 1 2 3 4 n
* 空格 3 2 1 0 总行数-n
* 星星 1 3 5 7 2*n-1
*
*
* *****
* ***
* *
* 行数 3 2 1 n
* 空格 1 2 3 总行数-n
* 星星 5 3 1 2*n-1
*
*
*
*
*/
public static void print05() {
for(int i = 1;i<=4;i++) {//控制行数
for(int k = 1;k<=4-i;k++) {//空格的个数
System.out.print(" ");
}
for(int j = 1;j<=2*i-1;j++) {//控制星星个数的时候和行有关
System.out.print("*");
}
System.out.println();
}
for(int i = 3;i>=1;i--) {//控制行数
for(int k = 1;k<=4-i;k++) {//空格的个数
System.out.print(" ");
}
for(int j = 1;j<=2*i-1;j++) {//控制星星个数的时候和行有关
System.out.print("*");
}
System.out.println();
}
}
public static void print06() {
for(int i = 1;i<=4;i++) {//控制行数
for(int k = 1;k<=4-i;k++) {//空格的个数
System.out.print(" ");
}
for(int j = 1;j<=2*i-1;j++) {//控制星星个数的时候和行有关
if(j==1||j==2*i-1) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.println();
}
for(int i = 3;i>=1;i--) {//控制行数
for(int k = 1;k<=4-i;k++) {//空格的个数
System.out.print(" ");
}
for(int j = 1;j<=2*i-1;j++) {//控制星星个数的时候和行有关
if(j==1||j==2*i-1) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
面向对象和过程
计算水仙花:
一个N位数 每个数字的N次幂之和 等于当前数字本身
153 1*1*1+5*5*5+3*3*3 = 153
1:分离每一位 * 位数次幂
2:计算当前数字的位数
3: 求和
4:判定 将就和的值和当前数字进行比较
倒叙输出一个数字 12345 54321 (0011 1100 100000001)
1:分离每一位
2:计算当前数字的位数
3:将每一位分离出来的数字乘以10的位数-1次方 --
4:求和
面向对象:
语言在发展过程中,
人开车
面向过程:
1:买辆车
2:考驾照
3:打开车门
4:踩离合,打火
5:挂挡、松离合、踩油门
。。。。
将一个问题,分成N多个小步骤,按步骤 按照顺序依次完成 整个功能就完成
面向对象:
对象: 对象可以帮助你完成一些功能或者是愿望
完成该功能中遇到的所有问题都和你无关 -> 更加轻松
驾驶员、人
车
面向对象和面向过程不是相对的,而是相辅相成。
面向对象和面向过程的对比:
面对对象:宏观上把控整个事务的完成度以及事务走向。
是对象帮我们处理具体的问题,而对象是如何做的,和调用者无关,调用者只需要负责结果即可。
调用的复杂度降低。底层具体的实现思路还是过程式的。
面向过程:微观上更精细化的处理具体问题
- 类中编写的内容有哪些?
成员变量:从属于当前类的
方法:对象完成 功能
-
java中如何创建对象?
创建的对象的方法:
类名 变量名=new 类名();
public class Test01 {
public static void main(String[] args) {
// 创建一个Person的对象
Person p1 = new Person();
//调用对象的功能
p1.eat();
Person p2 = new Person();
//查看当前p1对象的成员变量
System.out.println(p1.name);
//修改p1对象的name值
p1.name = "zhangsan";
System.out.println(p1.name);
//通过方法名直接调用person类中的方法
//eat();
}
}
- 创建对象要做的三件事:
1.开空间(在内存中开空间)
2.初始化(成员变量初始化)
3.指向引用
- class类和对象的关系?
编写类:
Person类: 将张三、李四。。。的相同的部分抽离出来:
外在的表现形式: 姓名、年龄。。。。 -> 成员变量
内在的功能: 能够做什么 -> 方法
类:
分门别类。抽象的概念。祖先为了能够让后来的人更好的去认识这个世界,将各个具象的内容抽离出来一个一个的类。
对象:
将类中所有抽象的内容进行了具象化 。 现实生活中真实存在的。
类和对象:
类是对于对象的抽象,抽象就是抽取像的部分
对象就是对于类的具象化的体现。
问题:
先有类还是先有对象?
如果是编写过程: 先有类 再有对象
如果是构建过程: 先有对象,将对象的共性抽离出来,外在、内在->类
内存分析:
static用法
- static的用法
static叫做 静态的 共享的 所属于类的 类被加载的时候会优先加载静态内容
static修饰的内容只有一份,被所有的对象以及类共享,对于static修饰的内容改变,也被所有对象可见
- static修饰的内容两种调用方式:
通过类名直接点 类名. (建议)
通过对象调用 对象.
- 如何在一个静态内容中调用非静态内容:
1:在非静态内容中加static
2:创建非静态内容的所属对象 通过对象调用
- 在非静态内容中调用静态方法:
1:可以直接调用,
非静态内容隶属于对象,如果非静态内容能够被调用,对象一定存在,对象存在证明类一定能被加载。
而类被加载,静态内容一定被加载。
问题:
- 什么时候类会被加载?
1:调用当前类中的静态方法
2:创建当前类的实例对象的时候
public class Test02 {
//声明一个变量
int num;//加载Test02类的时候,首先会加载当前类中的所有的static修饰的内容
public static void main(String[] args) {
//创建当前非静态变量所属的对象
Test02 t = new Test02();
System.out.println(t.num);
//直接方法person中的loc
Person p = new Person();
System.out.println(Person.loc);
System.out.println(p.loc);
}
}