2020-10-02

1.变量是一个代词,指代内存中的数据
  变量是可以改变的量---指代不同的数据

2.变量必须先声明,再使用
    语法: 数据类型 变量名;
          int       a;
  可以一条语句声明多个同类型变量
    eg: int a,b,c;
        int a=1,b,c=2;

3.变量的命名:
    规定:
      1)只能包含字母、数字、_、$,
        并且不能以数字开头
      2)严格区分大小写
      3)不能使用关键字(java已经用了的字)
    建议:
      1)不建议中文命名变量
      2)最好"见名知意"
      3)驼峰命名法:第一个单词首字母小写,
                   其余单词首字母大写
4.变量的初始化:
    java语法规定:变量在使用前必须初始化
    初始化两种方式:
      1)声明同时初始化
          int a=5;
      2)先声明再初始化
          int b; 
      b=8;

5.变量的使用:
    1)必须与数据类型匹配
      eg: int a=2.5; //错误,类型不匹配

6.int:
   1)整型,占4个字节,范围-21个多亿到21个多亿
   2)整数直接量(25,250,2500...),默认为int类型
   3)两个整数相除,结果为整数
     (无条件舍去小数位)
  long:
   1)长整型,占8个字节,范围特别大(足够用)
   2)long型直接量,在数字后面加l或L
      eg: long a = 35L;

小结:
1.变量:
    声明
    命名
    初始化
    使用
2.数据类型:
    int:55
    long:55L
    double:55.0
    char:'a'
    boolean:true和false
    类型间转换

明天讲:
  运算符,分支结构

声明5个变量,前3个直接给值,后2个算出来的


365*24*60*60*1000-----一年的毫秒数
31536000000-----long


10000元钱---10年
利息----几厘
10000.124578-----取钱,消卡
财务系统ERP----数字敏感----double型

BigDecimal----精确表示任何小数


我军-----敌军来袭
电报-----码

1110 1234 5878 4586-------码
敌   军   来   袭---------字符

char字符型:
两部分组成: 字符+码

char c='A';  //字符
char c=65;   //字符所对应的码

char c='\'';  //错误

System.out.println();

long a = 1L;
int b = (int)a; //错误


java规定两点规则:
1.整数直接量可以直接赋值给byte,char,short
2.byte,char,short只要参与运算,则一律转为int


面试题:哪错了,原因,怎么改
byte b1=5;
byte b2=6;
byte b3=(byte)(b1+b2); //错误


byte a = 55;
char c = 97;


int a=10000000000;    //错误
long a = 10000000000; //错误
long a = 10000000000L;//正确


----------如下为了解内容
1G=1024M(兆)
1M=1024KB(千字节)
1KB=1024B(字节)
1B=8Bit(位)

1G=???字节
1G=1024*1024*1024字节
   1000000000

int型变量只占4字节

帐户余额-----214784554(int最大值)
存1块钱------溢出变为-2145785

欠银行钱-----就不还-21475125467
             欠得多了,就变为正的了

java用之前必须给值----进化
c用之前可以不给值,系统默认给一个值(不固定)


银行系统
存款金额----5000(java)---帐户余额+5000
        ----(-10000000)(c)---------帐户余额


守法      遵守语法
道德      建议(最好遵守)
和谐      可扩展性好、维护性好、可移植性好、
          复用性好、健壮性好......(12K以上)


a="wangkejing";
b=36;
c=85;


见名知意-----------建议
name="wangkejing";
age=36;
score=85;

驼峰命名法:
name,teacherName,englishTeacherName

JavaScore
javascore
javaScore---建议


变量      数据
cardId =  123456789456123
cardPwd=  666666
name   =  wangkejing
age    =  36
address=  河北省廊坊市

I     love    you
张三          李四
李四          王五
王五          赵六
赵六          张三


1.出勤率   80%-------true
并且----&&(与)
2.考试     60分------true
-------推荐就业

&&:两边都为真,则为真
||:有一边为真,都为真
!:非真则假,非假则真


1.现金--------------false
或者----||(或)
2.刷卡--------------true
-------正常付费

北京买房:
1.社保满5年
     ||
2.纳税满5年


北京买车:
1.社保满5年
     &&
2.纳税满5年

2

age>=18
  &&
age<=50


接收用户输入:
import java.util.Scanner;  //1
Scanner scan = new Scanner(System.in);  //2

int age=scan.nextInt();  //3
boolean b1 = age>=18 && age<=50;
System.out.println(b1);  //true


闰年条件:
1.能被4整除但是不能被100整除
或者
2.能被400整除

year%4==0
   &&        
year%100!=0
   ||
year%400==0
year%4==0 && year%100!=0 || year%400==0

早上来上班:
起得特别早-----走着来
起得正常-------坐公交来
起得稍晚-------坐地铁来
再晚-----------开车来
更晚-----------不来

if...else if

int score=82;
if(score>=90){
   A优秀
}else if(score>=80){
   B良好
}else if(score>=60){
   C中等
}else{
   D差
}

总结:
1.运算符
    算术
    关系
    逻辑
    赋值:建议使用扩展赋值
         a+=5; 相当于a=a+5
    +
    三目: 语法: boolean ? 1 : 2
          运算过程:
        计算boolean
        为true,则整个为1
        为false,则整个为2
2.分支结构
    if:1条路
    if...else:2条路
    if...else if:多条路
    switch...case:多条路
                  常常和break配合使用
          break:跳出switch
          优点:清晰、效率高
          缺点:整形、只能判断相等
               ---灵活性稍差

三目/条件
int a=5,b=6;
int max;
if(a>b){
  max = a;
  ...
  ...
  ...
}else{
  max = b;
}

if(score>=90){
   A优秀
}else{
   if(score>=80 && score<90){
      B良好
   }else{
      if(score>=60 && score<80){
         C中等
      }else{
         D差
      }
   }
}

int a=88,b=26,c=66;
//从小到大----排序
a=26,b=66,c=88
System.out.println("a="+b);
System.out.println("b="+c);
System.out.println("c="+a);

int a=33,b=55,c=22; //从小到大
a>b 换 保证a小于b
a>c 换 保证a小于c
b>c 换 保证b小于c

main(){
  int a=4;
  if(a>0){
    int b=5;
  }else{
    int c=6;
  }
  .........只认a
}

循环三要素:
1.循环变量初始化
2.循环条件(以循环变量为条件)
3.循环变量的改变(向着循环结束变)
循环变量:在循环过程中改变的那个量

int count=1;   //跑的圈数---计数
count<=3        //条件------------跑
count--;       //改变计数

count=1  true  跑
count=2  true  跑
count=3  true  跑
count=4  false

计算1到9乘以9的结果
1*9=9
2*9=18
3*9=27
...
9*9=81
循环变量--------
int num=1;
num<10
num++;

语法:----背下来
while
do...while
for

1)跑3圈
2)打印10次"行动是成功的阶梯"
3)1到9乘以9的结果----后面会用到


循环来解决---反复的猜,反复的提示
三要素:
1.初始化----用户第一次猜
2.条件------猜的不等于num
3.改变------用户再猜


---------------正常

猜数字while
1.藏起来一个-----num(250)
2.猜吧-----------guess(循环变量初始化)
3.while(guess!=num){
     判断:
         若为0----退出
         否则若guess>num----太大了
         否则---------------太小了
     猜吧-----------guess(循环变量改变)
  }
4.if(guess==0){
    下次再来
  }else{
    猜对了
  }

        int num = (int)(Math.random()*1000+1);
        System.out.println("猜吧!");
        int guess = scan.nextInt(); //要素1
        while(guess != num){  //要素2
            if(guess == 0){
                break;  //提前终止
            }else if(guess>num){
                System.out.println("太大了");
            }else{
                System.out.println("太小了");
            }
            System.out.println("猜吧!");
            guess = scan.nextInt(); //要素3
        }
        if(guess == num){
            System.out.println("恭喜,猜对了");
        }else{
            System.out.println("下次再来");
        }

第一要素和第三要素一样时-------do...while更好

for (表达式1; 表达式2; 表达式3)  {   
   语句块(循环体)
}


5分钟---10分钟:
---------------------for来实现
1)1到100的累加和----5050
2)跑圈
  行动是成功的阶梯
  1到9乘以9---------任选其一

分析:
1.int score = 0; //分
2.for(int i=1;i<=10;i++){
    1)随机生成a,b(0-99之间)
    2)int result=a+b;---预存正确答案
    3)出题---问?+?=?
    4)算吧!----answer
    5)判断(answer==-1){
        break;
      }else if(answer==result){
         输出---正确
         score += 10;
      }else{
         输出---错误
      }
  }
3.输出分数

0----0.999999
  *100
0----99.999999


while语法:
  while(boolean表达式){
     循环体
  }

do...while语法:
  do{
     循环体
  }while(boolean表达式);

for语法:
  for(表达式1 ; 表达式2 ; 表达式3){
     循环体
  }

while和do...while的区别:

break:
continue:应用率低,常常可以用if...else代替


1----1000之间的随机数

Math.random()
-------生成0到1之间的(包含0,不包含1)
-------[0,1)

0--------0.999999999
     *1000
0--------999.9999999
     +1
1--------1000.9999999
     强转为int
1--------1000

猜数字:
直到猜对了结束-----break

break跳出循环


输出100句:行动是成功的阶梯
1.用循环
2.三要素
int count=1;
count<=100   输出
count++;

count=1 true 输出
count=2 true 输出
count=3 true 输出
...
count=100 true 输出
count=101 false


System.out.println("行动是成功的阶梯");


int sum=0,num=0;   //存和
for(int i=1;i<=10;i++){
   num = num*10+9;
   sum = sum+num;
}
System.out.println("sum="+sum);
sum=0,num=0
i=1  num=9    sum=9
i=2  num=99   sum=9+99
i=3  num=999  sum=9+99+999
 
num----每次加的那个数
i=1  num=9
i=2  num=99
i=3  num=999
i=4  num=9999

分析: 9到99
      9*10+9

      99到999
      99*10+9

      999到9999
      999*10+9


for(int n=1;n<=9;n++){
   for(int i=1;i<=n;i++){
      System.out.print(i+"*"+n+"="+i*n+"\t");
   }
}

int n = 8;
for(int i=1;i<=n;i++){
   System.out.print(i+"*"+n+"="+i*n+"\t");
}

int n = 7;
for(int i=1;i<=n;i++){
   System.out.print(i+"*"+n+"="+i*n+"\t");
}

int n = 6;
for(int i=1;i<=n;i++){
   System.out.print(i+"*"+n+"="+i*n+"\t");
}

int n = 5;
for(int i=1;i<=n;i++){
   System.out.print(i+"*"+n+"="+i*n+"\t");
}

for(int n=1;n<=9;n++){   //外层循环
   for(int i=1;i<=n;i++){  //内层循环
      System.out.print(i+"*"+n+"="+i*n+"\t");
   }
   System.out.println(); //换行
}

嵌套循环执行过程:
  外层循环走一次,内层循环走所有次
建议:
  循环层数越少越好
break:
  只能跳出一层循环


//外层一次,内层所有次
for(int i=1;i<=5;i++){
  for(int j=1;j<=10;j++){
     System.out.println(i+",,,"+j);
  }
}

1.数组可以装一组数,必须类型相同
2.数组按线性顺序,一个跟一个
3.数组也是一种数据类型
4.数组new之后,数组中的每个元素都有一个默认值
    整数:0  浮点数:0.0  boolean:false

 int    a = 5;

1)数组的定义:
    int[] arr = new int[4]; //每个数都是0
2)数组的初始化
    int[] arr;  //声明数组
    arr = new int[4];  //初始化
    int[] arr = {1,5,8,3}; //只能声明同时初始化
    int[] arr = new int[]{1,5,8,3};

    int[] arr;
    arr = {1,5,8,3}; //错误
3)数组的访问:通过下标
    下标从0开始
    int[] arr = new int[4];
    System.out.println(arr.length);//数组的长度4
    arr[0] = 55;//给arr中的第1个数赋值为55
    arr[4] = 99; //错误,数组越界,最大到3
    System.out.println(arr[2]);//输出第3个数

    //遍历数组
    int[] arr = {1,8,5,3};
    for(int i=0;i<arr.length;i++){  //正序
       System.out.println(arr[i]);
    }
    for(int i=arr.length-1;i>=0;i--){  //倒序
       System.out.println(arr[i]);
    }


    //数组的遍历
    int[] arr = {2,9,6,1,56,67,234,45,7657,234};
    for(int i=0;i<arr.length;i++){
       System.out.println(arr[i]);
    }

    System.out.println(arr[0]);
    System.out.println(arr[1]);
    System.out.println(arr[2]);
    System.out.println(arr[3]);

最大值算法:
1.假设数组中第1个元素为最大值
  int max = arr[0];  //max装最大值
2.循环遍历数组中剩余的元素
  for(int i=1;i<arr.length;i++){
     3.将数组元素与max比大小,
       若数组元素大于max,将max设置为数组元素
     if(arr[i]>max){
        max = arr[i];
     }
  }


int max = arr[0];
for(int i=1;i<arr.length;i++){
   if(arr[i] > max){
      max = arr[i];
   }
}
System.out.println("max="+max);

import java.util.Random;
main(){
  Random rand = new Random();
  int num = rand.nextInt(100); //0到99之间的
}

  
声明一个整型数组名为a,可以存储4个整数
int[]  a = new int[4];
double[] s = new double[4]; 
String[] ns = new String[10];

int[] arr = new int[5];
int i = arr.length;  //5

arr[2] = 88;
System.out.println(arr[4]);


  能用一层循环就不用两层
  能用两层循环就不用三层
  后期写个程序需要4层循环-----设计有问题
break:只能跳出1层循环

for(int i=1;i<=500;i++){
  for(int j=1;j<=10000;j++){
     for(int k=1;k<=10000;k++){
        System.out.println("aaa");
    if(条件){
      break; ----j++
    }
     }
  }
}

---500万*10000


i=1
  j=1/2/3/4/5/6.../10
  j=11
i=2
  j=
  


n=3
  i=1 1*3=3
  i=2 2*3=6
  i=3 3*3=9
  i=4
  换行

n=1
  i=1 1*1=1
  i=2
  换行
n=2
  i=1 1*2=2
  i=2 2*2=4
  i=3
  换行


分析:
1)5个数需要冒4轮
  for(int i=0;i<arr.length-1;i++){ }
2)每一轮比多少次
  for(int j=0;j<arr.length-1-i;j++){ }

99,25,45,2,23
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 t = arr[j];
     arr[j] = arr[j+1];
     arr[j+1] = t;
      }
   }
}
25,45,2,23,99
i=1
  j=0 25和45比 不换
  j=1 45和2比  换  25,2,45,23,99
  j=2 45和23比 换  25,2,23,45,99
i=0
  j=0  99和25比 换 25,99,45,2,23
  j=1  99和45比 换 25,45,99,2,23
  j=2  99和2比  换 25,45,2,99,23
  j=3  99和23比 换 25,45,2,23,99
  j=4

第一轮:
  99和25比 换 25,99,45,2,23
  99和45比 换 25,45,99,2,23
  99和2比  换 25,45,2,99,23
  99和23比 换 25,45,2,23,99-----冒出了99
第二轮:
  25和45比 不换
  45和2比  换 25,2,45,23,99
  45和23比 换 25,2,23,45,99-----冒出了45
第三轮:
  25和2比  换 2,25,23,45,99
  25和23比 换 2,23,25,45,99-----冒出了25
第四轮:
  2和23比  不换-----------------冒出了23
      

小结:
1.嵌套循环
2.数组
  1)定义
      数据类型[] 数组名 = new 数据类型[长度];
      int[] arr = new int[5];
  2)初始化
      int[] arr = new int[3]; //默认值为0
      int[] arr = {2,4,7};
      int[] arr = new int[]{2,4,7};
  3)访问:通过下标/索引
      int len = arr.length; //长度
      int num = arr[0]; //取第1个元素
      int num = arr[arr.length-1];//取最后1个元素
      arr[0] = 88;  //赋值
      System.out.println(arr[0]); //取值
  4)复制
      //从a的第1个元素开始
      //复制到a1中,从第3个元素开始
      //一共复制4个元素
      System.arraycopy(a,0,a1,2,4);
      //将a数组复制到b数组,b有6个元素
      int[] b = Arrays.copyOf(a,6);
      //给a数组扩容
      a = Arrays.copyOf(a,a.length+1);
  5)排序
      Arrays.sort(arr); //升序
      冒泡

 

1.方法
2.猜字符游戏----------


int min = arr[0];
for(int i=1;i<arr.length;i++){
   if(arr[i]<min){
      min = arr[i];
   }
}
arr = Arrays.copyOf(arr,arr.length+1); 

0  1  2--------arr
11,22,0

for(int i=arr.length-1;i>=0;i--){
   arr[i] = arr[i-1];
}
i=2 arr[2]=arr[1]; arr[2]=22
i=1 arr[1]=arr[0]; arr[1]=11
i=0 arr[0]=arr[-1]


for(int i=0;i<arr.length;i++){
   arr[i+1]=arr[i];
}
i=0  arr[1]=arr[0]---arr[1]=11
i=1  arr[2]=arr[1]---arr[2]=11

System.arraycopy(arr,0,arr1,1,arr.length);

补充题:---可做可不做
1.将最小值放在第1个位置---自己写算法
2.删除数组中的第3个元素
  int[] arr = {2,6,8,4,5,7};
  最后数组元素: 2,6,4,5,7
  提示:1)将要删除的元素放在最后一个位置
       2)缩容

8:
  8 % 2/3/4/5/6/7
  只要有一个为0,说明不是质数
5:
  5 % 2/3/4
  所有都不为0,说明是质数

99:
  99 % 2/3/4/5/6/7/.../97/98
  99 % 2/3/4/5/6/7/.../49
       51/52/53


25 % 2/3/4/5
     6/7/8/9/10/11/12


4平方根2
9平方根3
16平方根4
25平方根5
36平方根6


//调用方法----调java定义好的

方法没有返回值
System.out.println("HelloWorld");
System.out.println();
System.arraycopy(a,0,a1,1,4);
Arrays.sort(a);

方法有返回值
int num = scan.nextInt();
int num = rand.nextInt(100);
int[] arr = Arrays.copyOf(a,a.length+1);
double dou = Math.random();
double d = Math.sqrt(25);

方法没有返回值

方法无参数
System.out.println();
int num = scan.nextInt();
double dou = Math.random(); 固定在[0,1)

方法有参数
System.out.println("HelloWorld");
System.arraycopy(a,0,a1,1,4);
Arrays.sort(a);
int num = rand.nextInt(100); 0-99
int num = rand.nextInt(50);  0-49
int num = rand.nextInt(20); 
int num = rand.nextInt(15);
int num = rand.nextInt(25);  0-24


int[] arr = Arrays.copyOf(a,a.length+1);


double d = Math.sqrt(25);


方法是用于封装一段特定的逻辑功能
方法尽可能独立---只干一件事


sort(int[]){  //排序方法
  //冒泡6行代码 >改为<
}

sort(a);
sort(b);
sort(c);

arr------6行
arr1-----6行
arr2
arr3
arr4-----6行

取钱(){
}
存钱(){
}
改密(){
}

A用户: 存钱();
B用户: 存钱(); 改密();


操作(){
  //存钱
  //取钱
  //改密
}

A用户: 只想存钱


A用户: 存钱(); 改密();


开关:

int n = 9;
boolean flag = true;  //假设是质数
for(int i=2;i<n;i++){
  if(n % i == 0){
     System.out.println(不是质数);
     flag = false;   //不是质数
     break;
  }
}
若有一个取余为0,就不是质数
所有取余都不为0,就是质数
只要进到if,就不是质数
if(flag == true){
   System.out.println("是质数");
}

  
 


main(){
   say();     
   sayHi("wkj");
   double d = sum();
   int n = plus(5,6);
}无参无返回值,有参无返回值,
 无参有返回值,有参有返回值
public static void say() { }
public static void sayHi(String name){ }
public static double sum(){
   ....业务操作
   return 55.5;
}
public static int plus(int a,int b){
   return a+b;
}


修饰词 返回值类型 方法名(参数列表){
   方法体
}

当方法需要返回结果时,设计特定返回值类型
不需要结果时,返回值类型为void


返回结果通过return关键字


方法嵌套调用:
111,333,555,aaa,666,444,222
main(){
  111
  a();
  222
}
a(){
  333
  b();
  444
}
b(){
  555
  System.out.println("aaaaaa");
  666
}


一.设计数据结构-----数据、变量、类型
1.int score;    //分数
  int count;    //猜错的次数
2.char[] chs;   //5个随机字符组成的数组
3.char[] input; //用户猜的数的数组
4.int[] result; //猜测结果

二.设计程序结构-----方法
1.随机生成5个字符数组
  public static char[] generate(){
     char[] chs = new char[5];
     return chs;
  }
2.实现用户输入的与随机生成的数据间的比较
  public static int[] check(char[] chs,char[] input){
     int[] result = new int[2];
     return result;
  }
3.main()

三.设计实现---------方法的实现

A---负责一块
B---负责一块

A告诉B,给我一个方法,???再???得到???
  B:
  public static int[] check(char[] chs,char[] input){
     int[] result = new int[2];
     return result;
  }
  A:不用等B全部代码写完

  该怎么写代码怎么代码

chs:    ABCDE
input:  BDCMN

//result[0]字符对,result[1]位置对
int[] result = new int[2];
for(int i=0;i<chs.length;i++){
   for(int j=0;j<input.length;j++){
      if(chs[i]==input[j]){
         result[0]++;
     if(i==j){
        result[1]++;
     }
     break;
      }
   }
}


char[] letters = {'A','B','C','D','E','F''G','H','I''J','K','L''M','N','O''P','Q','R''S','T','U','V','W','X','Y','Z'};
boolean[] flags = new boolean[letters.length];
for(int i=0;i<chs.length;i++){
   int index;
   do{
      index = rand.nextInt(26); //0到25
   }while(flags[index]==true);
   chs[i] = letters[index];
   flags[index] = true;
}
i=0
  index=0  chs[0]='A'  flags[0]=true
i=1
  index=2  chs[1]='C'  flags[2]=true
i=2
  index=3  chs[2]='D'  flags[3]=true


开关默认为false
生成过改为true
何时存:当为false时
    当对应开关为true时,重新生成随机数


String字符串---------
0.int count=0;
1.生成随机数组----chs
while(true){-------------自造死循环
  2.用户猜------input
  int[] result = 调check()比较
  if(result[1]==chs.length){
     猜对了
     int score = chs.length*100-count*10;
     输出score
     break;
  }else{
     猜错了,字符对result[0],位置对result[1]
     count++;
  }
}


char:1个字符
  'A'
  'B'
  'C'
String:1串字符
  "ABC"


A-----B
 -----D
 -----C
 -----M
 -----N
B-----B
 -----D
 -----C
 -----M
 -----N
C-----B
 -----D
 -----C
 -----M
 -----N
 

main(){
    ...
    ...
    generate(?);
    ...
    ...
    check(?);
  }
  generate(?){
  }
  check(?){
  }

面向过程编程:一堆方法,调来调去
面向对象编程:以对象为核心,围绕着对象做操作
面向接口编程:面向对象的基础之上,抽接口
    复用性好、可维护性好、可扩展性好、
    移植性好......

面向过程:实在
面向对象:抽象
  A,B,C
  只放在一个地方更合适---抽象所在

晕是正常的------多练、多想

不晕两种情况:
1.什么都不懂
2.以为自己不晕---语法掌握

6天:掌握语法,知道何时用

讲完打飞机小游戏
------面向对象6天内容实际的应用


缺陷一:缺乏对数据的封装
缺陷二:数据和方法分离状态

class Emp{
   String name;
   int age;
   char gender;
   double salary;
}

将name,age,gender,salary----包到一种类型(Emp)中

public static void print(Emp emp){
  输出emp.name,emp.age,emp.salary,emp.gender
}
public static void print(String a,int b
                         char c,double d){
}

public static void print(String name,int age,
                         char gender,double salary){
  输出4个变量的值
}
main(){
  String name = "zhangsan";
  int age = 25;
  char gender='男';
  double salary = 5000;
  print(name,age,gender,salary);

  String a = "abc";
  int b = -1000000000;
  char c='国';
  double d = -10000; 
  print(a,b,c,d);
}
笔记:
1.现实世界是由很多对象组成的
2.现实世界是先有对象,再抽出类
  代码中先创建类,再创建对象
3.一个类可以创建多个对象
  同一个类的多个对象,结构相同,数据不同
4.类是一种数据类型
  只能包含:
    1)描述对象所共有的特征:------变量
          属性-----静的
    2)对象所共有的行为:----------方法
          行为-----动的
5.new后,成员变量有默认值
6.创建对象语法:
    类名 引用 = new 类名();
  其中:new 类名()是在创建对象
       因对象为数据,所有声明引用来指代数据
7.访问成员变量、访问方法
  ----通过点来操作,语法:
     引用.成员变量
     引用.方法名();
8.基本类型之间画等号----在赋值
    ------身份证复印件
  引用类型之间画等号----指向同一个对象
    ------房子钥匙
9.null:空,表示没有指向对象
  若引用的值为null,则不能再进行点操作,
  否则会出现NullPointerException异常

class Teacher{
   String name;
   int age;
   String address;
   double salary;
   
   void teach(){
   }
   void eat(){
   }
   void sleep(){
   }
}
类中只能包含成员变量和方法
class Student{   //学生类
   String name;
   int age;
   String address;  //成员变量

   int eat(int num){ }
   void sleep(){ }
   void study(){ }  //方法
}
class StudentTest{   //测试类都包含main
   main(){
      Student zs = new Student();
      zs.name = "zhangsan";
      zs.age = 25;
      zs.address = "河北廊坊";
      zs.eat();
      zs.sleep();
      zs.study();

      Student ls = new Student();
      ls.name = "lisi";
      ls.age = 18;
      ls.address = "黑龙江佳木斯";
      ls.eat();
      ls.sleep();
      ls.study();

      Student ww = new Student();
   }
}


int[]   arr = new int[4];
   ----数组元素有默认值
Student zs  = new Student();
   ----成员变量就有默认值

步骤:
1.找对象------好多好多格子
2.抽类------class Cell{ }
3.设计类:
    1)成员变量---row行号,col列号
    2)方法

方法是用于操作数据的
class Cell{      //格子类
   int row;   //行号
   int col;   //列号

   void drop(){  //下落1个
      row++;
   }
   void moveLeft(int step){  //左移step个
      col -= step;
   }
   String getCellInfo(){  //获取格子坐标
      return row+",,,"+col;
   }

}

class Cell{
   drop();
   moveLeft();
   moveRight();
   getCellInfo();

   print(); -----打墙+格
}

格子对象没有打墙的行为

1.打印雇员信息
2.Cell类+成员变量
3.Cell类的方法
4.创建Cell类对象+打墙+打格

房子-----------对象
房子钥匙-------引用
配了/复制一把钥匙给他

new Student(); //创建学生类对象
new Teacher(); //创建老师类对象

对象是一个数据

创建对象


类型       引用类型变量  对象
           引用---简称
Student     zs =          new   Student();
Teacher     wkj =         new   Teacher();
ClassRoom   cr =          new   ClassRoom();
Cell        c =           new   Cell();

Student zs = new Student();
zs.name = "zhangsan";

int num = 5;
int n = num;

Cell c = new Cell();
c.row = 5;
c.col = 4;

for(int i=1;i<=20;i++){     //行号
   for(int j=1;j<=10;j++){  //列号
      if(i==c.row && j==c.col){
         System.out.println("* ");
      }else{
         System.out.println("- ");
      }
   }
   System.out.println();
}


main(){
  Cell c = new Cell();
  c.row = 5;
  c.col = 4;
  print(c); //Cell cc = c;

  c.drop();
  print(c);

  c.moveLeft();
  print(c);
}
public static void print(Cell cc){
  //双层for循环+if判断
}


i=1
  j=1/2/3/.../11
i=2
  j=1/2/3/.../11

- - - - - - - - - - 
- - - - - - - - - - 


//练习:
1.创建Student类,包含:
    特征: name,age,address
    行为: study(),sayHi()---输出3个变量的值
2.创建StudentTest类,包含main()方法
    main方法中:
      1)创建Student对象zs
        给变量赋值,调用方法
      2)创建Student对象ls
        给变量赋值,调用方法

Student zs = new Student(); //创建对象


英雄机:
  特征:
    width,height,x,y,image
  行为:
    move(),shoot()

小蜜蜂:
  特征:
    width, height, x, y, image
  行为:
    move()

类         对象
月饼模子   月饼
图纸       高楼

class Student{  //学生类
}

Student zs = new Student();
Student ls = new Student();
Student ww = new Student();

学员管理系统:
对象:
  学员
  老师
  教室
  课程
抽出类:
  class Student{   //学生类
  }
  class Teacher{   //老师类
  }
  class ClassRoom{ //教室类
  }
  class Course{    //课程类
  }

  //创建对象
  //Scanner和Random就是类
  Scanner scan = new Scanner(System.in);
  Random rand = new Random();

  创建对象语法:
  类名  名 = new  类名();
 

1.方法的重载
class Aoo{
   void pay(){}
   void pay(double d){}
   void pay(String s1,String s2){}
   void pay(int a,double d){}
   void pay(double d,int a){}

   void pay(int num,double dou){} //错误
   int pay(){} //错误
}

用户:

void println(){}
void printlnString(String str){}
void printlnInt(int n){}
void printlnDouble(double d){}
void printlnBoolean(boolean b){}


System.out.println();
System.out.println("HelloWorld");
System.out.println(111);
System.out.println(5.55);


java建议:
  1个文件只包含1个类

java规定:
  java中一个文件可以包含多个类,
  但是,public的类只能有1个,
  并且,public的类必须与文件名相同


语法:
1.class---成员变量、方法
2.测试类---main(){创建对象}


构造方法:
1.构造方法常常用于给成员变量初始化
2.与类同名,没有返回值类型
3.构造方法是在创建对象时被自动调用
4.若自己不写构造方法,
  则编译器默认给一个无参构造,
  若自己写了,则不再默认提供无参构造
5.构造方法可以重载

this:
1.this指代当前对象,谁调指的就是谁
2.用法:
    this.成员变量---访问成员变量
    this.方法()-----访问方法
    this()--------调构造方法


class Cell{
   int row;
   int col;
   Cell(int row,int col){
      this.row = row;
      this.col = col;
   }
   Cell(int n){
      this(n,n);
   }
   Cell(){
      this(0,0); //调两个int型参数的构造
   }
}


class Cell{
   int row;
   int col;
   Cell(int row,int col){
      this.row = row;
      this.col = col;
   }
   void drop(){
      row++;
      //this.row++;  //相当于c.row++
   }
}
main(){
   Cell cc = new Cell(2,8);
   cc.drop();

   Cell c = new Cell(5,4);//c.row=5,c.col=4
   c.drop();
   System.out.println(c.row); //6

   Cell c = new Cell(5,4);
   Cell cc = new Cell(8,2);
}

class Cell{
   int row;
   int col;
   void drop(){}
   void moveLeft(){}
   String getCellInfo(){}
   
   Cell(){
   }
   Cell(int n){
      row = n;
      col = n;
   }
   Cell(int row,int col){
      this.row = row;
      this.col = col;
   }
}

class CellTest{
   main(){
      Cell c = new Cell();
      Cell c = new Cell(5);
      Cell c = new Cell(5,4);
      Cell c = new Cell();
      c.row = 5;
      c.col = 4;
   }
}


class Student{  //学生类
   String name;
   int age;
   String address;
   void study(){}


   void sayHi(){
      study();
      this.study();

      System.out.println(name+",,,"+age+",,,"+address);
   }

   Student zs = new Student("zhangsan",25,"河北");
   zs.sayHi();

   Student ls = new Student("lisi",26,"黑龙江");
   ls.sayHi();


   Student(){
   }
   Student(String name,int age,String address){
      this.name = name;
      this.age = age;
      this.address = address;
      //this.name---指的是成员变量
      //name--------指的是局部变量/参数
   }
}
class StudentTest{
   main(){
      Student zs = new Student();
      Student zs = new Student("zhangsan",25,"河北");

      Student ls = new Student("lisi",26,"黑龙江");

      Student zs = new Student();
      zs.setInfo("zhangsan",25,"河北");

      Student ls = new Student();
      ls.setInfo("lisi",18,"黑龙江");


      Student zs = new Student();
      zs.name = "zhangsan";
      zs.age = 25;
      zs.address = "河北";

      Student ls = new Student();
      ls.name = "lisi";
      ls.age = 23;
      ls.address = "黑龙江";
   }
}

声明int型数组,名为arr,包含4个元素
arr中的每一个元素都是int类型
int   [] arr = new int[4];---基本类型数组

声明Cell型数组,名为arr,包含4个元素
arr中的每一个元素都是Cell类型
Cell  [] arr = new Cell[4];---引用类型数组

int是数据类型
Cell是数据类型


int    [] arr = new int[4];
boolean[] bs = new boolean[6];
Student[] stus = new Student[100];
Random [] rs = new Random[4];
Scanner[] ss = new Scanner[8];

Hero hero = new Hero(); //英雄机

1.引用类型数组的定义
  Cell[] cs = new Cell[4];
2.引用类型数组的初始化
  1) Cell[] cells = new Cell[4]; //new数组
     cells[0] = new Cell();
     cells[1] = new Cell(2);
     cells[2] = new Cell(5,4);
     cells[3] = new Cell(2,6);
  2) Cell[] cells = {
        new Cell(),
    new Cell(1),
    new Cell(5,4),
    new Cell(2,7)
     };
  3) Cell[] cells = new Cell[]{
        new Cell(),
    new Cell(1),
    new Cell(5,4),
    new Cell(2,7)
     };
     

数组也是一种数据类型

int   []  arr  = new int [4];
Cell  []  cs   = new Cell[4];


声明int[]类型的数组,名为as,包含4个元素
as为数组的数组,里面每一个元素都是int[]类型
每一个元素,默认值为null
as[0]默认值为null
as[0]是int[]类型

int[] []  as = new int[4][];
as[0] = new int[2];
as[1] = new int[4];
as[2] = new int[2];
as[3] = new int[5];


int[][] as = new int[3][];
as[0] = new int[4];
as[1] = new int[4];
as[2] = new int[4];
简写:
int[][] as = new int[3][4];
for(int i=0;i<3;i++){   //行
   for(int j=0;j<4;j++){  //列
      as[i][j] = 100;
   }
}

练习:------全部熟练做下来
1.创建Cell型数组,包含4个元素
  分别对每一个数组元素赋值
2.创建Cell型数组,直接对每一个元素赋值--{ }
3.创建int[]型数组arr,包含3个元素
  第1个元素又包含2个元素
  第2个元素又包含3个元素
  第3个元素又包含2个元素
  将arr的第2个元素中的第1个元素赋值为100
4.创建int[]型数组,为3行4列
  双层循环输出每一个元素的值


1.找对象-----7种图形对象
2.抽类
    class T{}
    class J{}
    class O{}
    class S{}
    class Z{}
    class I{}
    class L{}
3.设计类中的成员变量(特征)、方法(行为)
    class T{
       Cell[] cells;
       T(int row,int col){
          cells = new Cell[4];
      cells[0] = new Cell(row,col);
      cells[1] = new Cell(row,col+1);
      cells[2] = new Cell(row,col+2);
      cells[3] = new Cell(row+1,col+1);
       }

       void drop(){   //下落一个
          for(int i=0;i<cells.length;i++){
         cells[i].row++;
      }
       }
       void moveLeft(){ //左移一个
          for(int i=0;i<cells.length;i++){
         cells[i].col--;
      }
       }
       void moveRight(){ //左移一个
          for(int i=0;i<cells.length;i++){
         cells[i].col++;
      }
       }
       void print(){  //输出4个格子的坐标
          for(int i=0;i<cells.length;i++){
         String s = cells[i].getCellInfo();
         System.out.println(s);
         
         System.out.println(cells[i].getCellInfo());
      }
       }
    }


    class J{
       Cell[] cells;
       J(int row,int col){
          cells = new Cell[4];
      cells[0] = new Cell(row,col);
      cells[1] = new Cell(row,col+1);
      cells[2] = new Cell(row,col+2);
      cells[3] = new Cell(row+1,col+2);
       }


    }

    main(){
       T t = new T(1,5);
    }


int[][] as = new int[3][];
as[0] = new int[4];
System.out.println(as[0][0]);//0


System.out.println(as[0][0]);//错误


as[1][2] = 100;


给as中第2个元素的第3个元素赋值为100

as[0]---int类型
int[] as = new int[4];

//1给arr[0]赋值,2给arr[1]赋值......
int[] arr = {
   1,
   2,
   3,
   4
};
//1给cs[0]赋值,2给cs[1]赋值
Cell[] cs = {
   new Cell(),
   new Cell(5),
   new Cell(5,4),
   new Cell(3,7)
};
System.out.println(cs[1].row);


Cell[] cs = new Cell[4];
cs[0] = new Cell();
cs[1] = new Cell(5,4);
cs[2] = new Cell(3,2);
cs[3] = new Cell(8);


cs[4] = new Cell();  //越界

System.out.println(cs[0].row);  //0


cs[0]=null cs[1]=null ......
Cell[] cs = new Cell[4];
cs[0].row = 5;  //空指针异常

int[] arr = new int[4];
arr[0] = 5;

Cell[] cs = new Cell[4]; //new是在创建数组
cs[0]为Cell类型
cs[0] = new Cell();  //new是在创建Cell对象


new之后,arr中每个元素都是0
int[] arr = new int[4];
boolean[] bs = new boolean[4];

new之后,cs中每个元素都是null
Cell[] cs = new Cell[4];
Student[] ss = new Student[4];
 

1.继承财产:
    钱不用自己挣,也能用(花)
  继承皇位:
    江山不用自己打,也是你的
  继承工作:
    工作不用自己找,也是你的
  程序中的继承:
    代码不用写,也能用


继承:避免代码重复
     父类中包含所有子类公有的数据
     子类中包含子类所特有的数据

Student zs = new Student();
zs.name/age/address/eat()/sleep()---父类
zs.className/study()----子类

Teacher wkj = new Teacher();
wkj.name/age/address/eat()/sleep()---父类
wkj.salary/teach()
wkj.className/study()----不可以

class Person{     //父类
   String name;
   int age;
   String address;
   char sex;
   void eat(){}
   void sleep(){}
}

class Student extends Person{   //子类
   String className;
   void study(){}   //学习
}
class Teacher extends Person{   //子类
   int salary;
   void teach(){}   //授课
}
class Doctor extends Person{    //子类
   String level;   
   void cut(){}    //开刀
}


class Tetromino{   //图形类---父类
   //公有的数据
   Cell[] cells;
   void drop(){}
   void moveLeft(){}
   void moveRight(){}
   void print(){}
}

class T extends Tetromino{
   //特有的数据
   T(int row,int col){
      
   }
}
class J extends Tetromino{
}
class O extends Tetromino{
}


class 飞行物{
   int width;
   int height;
   int x;
   int y;
   void move(){}
}
class 英雄机 extends 飞行物,A{   //错误的
}
class 小蜜蜂 extends 飞行物{
}
class 小飞机 extends 飞行物{
}
class 子弹 extends 飞行物{
}
class A{
}


java继承有传递性:
class A{
  int aa;
}
class B extends A{
  int bb;
}
class C extends B{
  int cc;
}
C c = new C();
c.cc/bb/aa

B b = new B();
b.aa/bb

A a = new A();
a.aa

class Tetromino{
   Cell[] cells;
   Tetromino(){
      cells = new Cell[4];
   }
   void drop(){}
   void moveLeft(){}
   void moveRight(){}
   void print(){}
}
class T extends Tetromino{
   T(){
      this(0,0);
   }
   T(int row,int col){
      cells[0] = new Cell(row,col);
      cells[1] = new Cell(row,col+1);
      cells[2] = new Cell(row,col+2);
      cells[3] = new Cell(row+1,col+1);
   }
}
class J extends Tetromino{
}


class A{
   A(){
   }
   A(int num){
      输出222
   }
}
class B extends A{
   B(){
      super(5);
      输出111
   }
}

打墙+打星方法:
  Tetromino父类---
     T
     J


  TetrominoTest测试类---放在此处

class Cell{
   
}
class CellTest{
   main(){
      Cell c = new Cell(5,4);
      printCell(c);//Cell cc = c;
   }
   printCell(Cell cc){  //打墙+打格
      for(){
         for(){
        if(i==cc.row && j==cc.col){
           *
        }else{
           -
        }
     }
      }
   }
}


main(){
   T t = new T(0,0);
   printCell(t);
}
public static void printCell(T tt){ //打墙+格
   for(int i=0;i<20;i++){
      for(int j=0;j<10;j++){
         boolean flag = true; //默认打-
     for(int k=0;k<4;k++){
        if(i==tt.cells[k].row && j==tt.cells[k].col){
           flag = false;  //不打-
           System.out.print("* ");
           break;
        }
     }
     if(flag){
        System.out.print("- ");
     }
      }
      System.out.println();
   }
}
以前打1个Cell(星),

如果不打*,肯定打-吗
if(i==? && j==?){
   System.out.print("* ");
}else{
   System.out.print("- ");
}

-----------
----***----
-----*-----

1.JVM内存管理
2.继承

java中,调一个方法,就给该方法分配一个"栈桢"
栈桢中存的就是局部变量
方法调用结束,栈桢消失,局部变量跟着消失
class Cell{

  int add(int num1,int num2){  //求和
   int num = num1+num2;
   return num;
  }

}


for(int i=0;i<20;i++){
   for(int j=0;j<10;j++){
      boolean flag = true;
      for(int k=0;k<4;k++){
        if(i==tt.cells[k].row&&j==tt.cells[k].col){
       System.out.print("* ");
       flag = false;
       break;
    }
      }
      if(flag){
         System.out.print("- ");
      }
   }
   System.out.println();
}

T是图形
class T extends Tetromino{
}
学生是人
class Student extends Person{
}


老虎是动物
class Animal{   //动物类
}
class Tiger extends Animal{    //老虎类
}

动物是动物
Animal a = new Animal();
老虎是老虎
Tiger t = new Tiger();
老虎是动物
Animal a = new Tiger();

父类类型引用指向子类的对象---向上造型

动物是老虎--------------语义不通
Tiger t = new Animal();  //错误

Person p = new Student(); //向上造型
Person p = new Teacher();
Person p = new Doctor();

Tetromino tt = new T();
Tetromino tt = new J();
Tetromino tt = new O();
Tetromino tt = new I();
Tetromino tt = new L();
Tetromino tt = new S();
Tetromino tt = new Z();  //向上造型

一切以对象为中心

main(){
   T t = new T(0,0);
   printTetromino(t.cells); //不建议
   printTetromino(t); //建议
}
printTetromino(Tetromino t){ //传的对象
   t.cells
   t.drop()
   t.moveLeft()
   t.moveRight()
   t.print()
}
printTetromino(Cell[] cs){  //传的对象的数据
}

练习:
1.创建父类Aoo,包含a,show()
2.创建子类Boo继承Aoo,包含b,say()
3.创建测试类Test,在main()中:
  1)创建父类对象,访问成员---?
  2)创建子类对象,访问成员---?
  3)向上造型,访问成员-------?


向上造型:父类引用指向子类对象
         只能点出来父类的成员
意义何在?

生产水果:

Apple createApple(){
  return new Apple();
}
Banana createBanana(){
  return new Banana();
}
Fruit createFruit(){   //生成水果

  Fruit f = new Apple();
  return f;

  return new Apple();

  return new Banana();
  return new Pair();
}

main(){
  Tetromino t = new T(2,5);
  printTetromino(t);  //Tetromino tt = t;
}
void printTetromino(Tetromino tt){
  tt.cells-----相当于t.cells
  Cell[] cells = tt.cells; //获取4个格子
}
class Tetromino{
  Cell[] cells;
  Tetromino(){
    this.cells = new Cell[4];  //this指t
  }
  void drop(){}....
}
class T extends Tetromino{
  T(int row,int col){
    super();
    this.cells[0] = new Cell(row,col);
    this.cells[1] = new Cell(row,col+1);
    this.cells[2] = new Cell(row,col+2);
    this.cells[3] = new Cell(row+1,col+1);
  }
}

1.堆、栈、方法区-----了解
2.继承
  构造子类之前先构造父类----super()
  向上造型

class J extends Tetromino{
}

main(){
   Student zs = new Student(); 
   zs.name = "zhangsan";
   zs.age = 25;
   zs.address = "黑龙江";
   zs.className = "JSD1407";
   zs.sayHi();----name,,age,,address,className

   Teacher wkj = new Teacher();
   wkj.name = "wangkejing";
   wkj.age = 18;
   wkj.address = "河北廊坊";
   wkj.salary = 5000;
   wkj.sayHi();----name,age,address,salary


   Student zs = new Student();
   print(zs);
   Teacher wkj = new Teacher();
   print(wkj);
   
   Person p = new Student();
   print(p);

   Person pp = new Person();
   print(pp);
}

public static print(Person per){
   per.sayHi();  //正确的---Student类的
                
}

方法的重写:发生两个类中,并且是子父的关系
           子类方法与父类方法签名相同时,
       我们说,子类重写了父类的方法

当方法被重写时,调哪个方法看对象

class Person{
   String name;
   int age;
   String address; 
   void sayHi(){
      System.out.println
       (name+",,"+age+",,"+address);
   }
}
class Student extends Person{
   String className;
   void sayHi(){
      System.out.println
       (name+",,"+age+",,"+address+",,"+className);
   }
}
class Teacher extends Person{
   int salary;
   void sayHi(){
      System.out.println
       (name+",,"+age+",,"+address+",,"+salary);
   }
}

class Aoo{
  int a;
  void show(){ 
     111 
  }
}
class Boo extends Aoo{
  int b;
  void show(){ --在Aoo的show()方法基础之上再做事
     super.show();---调父类的show()方法
     222 
  }
}

main(){
   T t = new T();
   t.print();-------打印4个格子坐标
}
class Tetromino{
   void print(){----打印4个格子的坐标(问好)
   }
}
class T extends Tetromino{
   void print(){
      System.out.println("i am a t");
      super.print();
   }
}
class J extends Tetromino{
}

this:------------本类
 this.成员变量
 this.方法()
 this()
super:-----------父类
 super.成员变量
 super.方法()
 super()


重写(override)和重载(overload)的区别------常见面试题
重载:
  在一个类中,方法名相同,参数列表不同
重写:
  在两个类中,并且是子类和父类的关系,签名相同

重载:编译时----.java到.class的过程
               内存没东西---只看语法对不对

重写:运行时----jvm加载.class并运行.class的过程
               内存才有东西

堆、栈、方法区------jvm分配的内存

重载时看类型,重写时看对象
1.创建Aoo类,包含show(),输出111
2.创建Boo类继承Aoo类,重写show(),输出222
3.创建Coo类,包含:
    t(Aoo o){ 输出333   o.show(); }
    t(Boo o){ 输出444   o.show(); }
4.测试类,main()方法中:
    Coo c = new Coo();
    Aoo o = new Boo();
    c.t(o);------问输出结果????
                   333 222

建议
域名反写   项目名称       模块名称
com.tarena.studentmanager.course.类名

package a.b.c.d.e; //声明包
public class Test{
}


//a.Test----全局限定名
a.b.c.d.e.Test o = new a.b.c.d.e.Test();

import a.b.c.d.e.Test; //声明类、引入类
Test t = new Test();   //创建对象


import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

import java.util.*;---------不建议


Scanner scan = new Scanner(System.in);

java.util.Scanner scan = new java.util.Scanner(System.in);

同一个包中的类之间,需要import吗?

全局限定名=包名+类名

main(){
  Aoo o = new Boo();
  o.show();-----------Boo

  Boo o = new Boo();
  o.show();-----------Boo

  Aoo o = new Aoo(); 
  o.show();-----------Aoo
}

1.重写------------------
2.package,import
3.访问修饰符public......
4.static,final

去银行取钱:
密码----------密码正确与否---
              密码是多少-----不知道
class Bank{
   private int password;  //密码----藏起来
   public boolean checkPwd(int pwd){----放开
      if(pwd == password){
         return true;
      }
      return false;
   }
}


//单例模式
class Aoo{
  private Aoo(){
  }
  void show(){
     Aoo o = new Aoo(); //正确
  }
}
class Boo{
  void show(){
     Aoo o = new Aoo();  //错误
  }
}


实例化:创建对象的过程
实例:实际的例子-----------对象

class Aoo{
   int a;-------------属于对象
   static int b;------属于类
   void show(){
     b++;
   }
}

成员变量:
  1)实例变量-----不用static修饰的 
  2)静态变量-----static修饰的

何时用静态变量,何时用实例变量
class Customer{ //帐户类
   String customerName;  //帐户名称
   String customerPwd;   //帐户密码
   static double poir;          //利率
}
实例变量:属于对象,一个对象有一份
静态变量:属于类,所有对象公用这一份

Customer zs = new Customer();
zs.customerPwd = 123456;

Customer ls = new Customer();
ls.customerPwd = 654321;

类的方法中,常常需要对对象的实例变量操作
类的非静态方法,默认有个隐式的this
类的静态方法,没有隐式的this的
class Cell{
   int row;  //属于对象---实例变量
   int col;  //属于对象---实例变量
   static int num;
   static void show(){
      row++;   //错误的
      num++;   //正确的

      Cell c = new Cell();
      c.row = 5;
   }
   void drop(){
      this.row++;
   }
   void moveLeft(){
      this.col--;
   }

}
main(){
  Cell c1 = new Cell(2,5);
  c1.drop();  //3,5

  Cell c2 = new Cell(6,7);
  c2.drop();  //7,7
}


非静态方法:---有隐式this
  可以直接访问静态变量和实例变量
  需要访问实例变量时用

静态方法:-----没有隐式this
  只能直接访问静态变量,不能直接访问实例变量
  不需要访问实例变量时,只需对参数操作即可

何时用静态方法,何时用非静态方法:
  

静态方法,只与参数相关,与实例变量有关
Arrays.sort(arr);
Math.random();
Math.sqrt(25);


何时用静态代码块:
  一般用于加载静态资源(图片、音频、视频)

 

1.抽象类、抽象方法
2.接口


class Test{
   public static void main(String[] args){
      Cell c = new Cell(5,2);
      Test.printCell(c);

      Test t = new Test();
      t.show();
   }
   public void show(){
   }
   public static void printCell(Cell c){
   }
}


java是不建议空方法的

抽象类能创建对象吗??????

Shape s = new Shape(); ------错误
abstract class Shape{    //抽象类(不完整)
   int c;  //周长
   abstract double area(); //抽象方法(不完整)
}
abstract class Square extends Shape{   //方形类
}
class Circle extends Shape{   //圆形类
   double area(){  //重写--变不完整为完整
      return 0.0796*c*c;
   }
}
Circle cir = new Circle();

abstract class 汽车{
   abstract void 制造发动机();
}

class 汽车{
   void 制造发动机(){
   }
}
new汽车对象


1.抽象方法:由abstract修饰
           只有方法的定义,没有方法体的
2.抽象类:由abstract修饰
         可以包含抽象方法,也可以包含普通方法
3.包含抽象方法的类,必须是抽象类
  类中没有抽象方法,也可以将类声明为抽象类
4.抽象类不能被实例化 Shape s = new Shape();//错误
5.抽象类一般需要被继承:
  1)子类也声明为抽象类
  2)子类重写抽象类中所有抽象方法---首选
6.抽象类的意义:
  1)封装子类公用的成员
    为子类提供一个公共的类型
  2)定义抽象方法,由子类来做不同的实现,
    但入口(方法名)是一样的


//声明一个数组,包含各种图形对象
//获取最大最积

数组-1个圆形对象,1个方形对象,1个三角形对象
通过算法,求这3个对象的最大面积

Shape s = new Shape(); //new Shape对象--错误
Shape s = new Circle();
Shape s = new Square();

Shape[] shapes = new Shape[3];//创建Shape数组
shapes[0] = new Circle(1);
shapes[1] = new Square(1);
shapes[2] = new Circle(1.2);
int maxArea = shapes[0].area();
for(int i=1;i<shapes.length;i++){
   double area = shapes[i].area();
   if(area > maxArea){
      maxArea = area;
   }
}
System.out.println("maxArea=" + maxArea);

abstract class Shape{   //抽象类(不完整)
   int c;   //周长
   abstract double area();  //抽象方法(不完整)
}
class Square extends Shape{   //方形类
   Square(double c){
      this.c = c;
   }
   double area(){  //重写
      return 0.0625*c*c;
   }
}
class Circle extends Shape{   //圆形
   Circle(double c){
      this.c = c;
   }
   double area(){  //重写
      return 0.0796*c*c;
   }
}

问:Tetromino类是为了创建对象吗?
俄罗斯方块游戏中:
  Tetromino tetri = new Tetromino(); //没有意义

abstract class Tetromino{
   Cell[] cells;
   void drop(){}
   void moveLeft(){}
}
class T extends Tetromino{
}
class J extends Tetromino{
}
class O extends Tetromino{
}

abstract 飞行物类:
  width,height
  x,y
  img
  abstract move();

英雄机----继承飞行物
小蜜蜂----继承飞行物
小飞机----继承飞行物
子弹------继承飞行物


只要遵守了USB规范,就能将设备插入到电脑上
只要遵守了国家盖楼房的标准,这个楼就能卖

只要遵守了这个规范,就能干某件事

重写:
  方法名同,参数列表同
  子类的访问权限大于或等于父类的


1.接口就是一个标准、一个规范
2.接口中只能包含常量和抽象方法
3.接口不能被实例化
    接口 引用 = new 实现类(); //向上造型
4.类实现接口,必须将所有抽象方法都实现
5.类可以实现多个接口,用逗号分隔
  若类又继承父类又实现接口,需先继承后实现
6.接口与接口之间可以继承

1.创建接口Inter,包含:
    常量: PI(完整写法),NUM(简便写法)
    方法: Show(完整写法),Say(简便写法)
2.创建接口Inter1,包含a()
  创建接口Inter2,包含b()
  创建接口Inter3,继承Inter1,并包含c()
  创建抽象类Aoo,包含普通方法d(),抽象方法e()
3.创建类Boo,实现Inter3,包含m()方法,实现抽象方法
  创建类Coo,实现Inter1,Inter2,实现抽象方法
  创建类Doo,继承Aoo并实现Inter1和Inter2
4.在main()方法中:
  1)创建Inter对象o1-------???
  2)输出Inter中的PI的值-------Inter.PI
  2)Inter3引用指向Boo对象---??能调什么方法
      Inter3 o = new Boo();
      o.a();
      o.c();

想开一个工商银行(对象)
工商银行类---------遵守标准(工行接口)

1.制定标准---接口
  interface UnionPay{   //银联接口
     void 存钱();
     void 取钱();
     void 改密码();
     void 查余额();
  }
  interface ICBC extends UnionPay{   //工行接口
     void 在线支付();
  }
  interface ABC extends UnionPay{    //农行接口
     void 支付电话费();
  }
2.遵守标准----类
  class ICBCImpl implements ICBC{  //工行类
     public void 存钱(){}
     public void 取钱(){}
     public void 改密码(){}
     public void 查余额(){}
     public void 在线支付(){}
     
  }
  class ABCImpl implements ABC{    //农行类
     public void 存钱(){}
     public void 取钱(){}
     public void 改密码(){}
     public void 查余额(){}
     public void 支付电话费(){}
  }
3.main(){
     ICBCImpl icbc1 = new ICBCImpl();//开了1个工行
     icbc1.存钱()/取钱()/改密码()/查余额()/在线支付()

     ABCImpl abc1 = new ABCImpl();

     ICBC i = new ICBCImpl(); //向上造型
  }


interface IInter{
  public static final int NUM = 5;
  int NUMNUM; //默认public static final的

  public abstract void show();
  void sayHi();//默认public abstract
}
interface IInter2{
  void sayHello();
}
class Abc{
}
class Coo extends Abc implements IInter1,IInter2{
}

class Boo implements IInter,IInter2{
  public void show(){}
  public void sayHi(){}
  public void sayHello(){}
}


          实现
class Aoo implements IInter{  //遵守标准
  public void show(){
  }
  public void sayHi(){
  }
}

接口:
1.电脑厂商,做一个USB接口(规范、标准)
  外部设备厂商,遵守USB接口(规范、标准)
   (鼠标、摄像头、键盘......)
2.国家制定盖楼房的标准
  开发商遵守这个标准
3.国家制定家具的标准
  家具厂商遵守这个标准
  

1.多态
2.内部类
3.面向对象的汇总

main(){
   Shape[] shapes = new Shape[30000];
   shapes[0] = new Circle(2);   //向上造型
   shapes[1] = new Square(2);
   shapes[2] = new Six(2);
   maxArea(shapes);

   Shape s1 = new Circle();
   Shape s2 = new Square();
   Shape s3 = new Six();

}
public static void maxArea(Shape[] shapes){
   shapes[0].area();
}

class Six extends Shape{
   Six(double c){
      this.c=c;
   }
   double area(){
      return 0.0721*c*c;
   }
}

interface CCB extends UnionPay{
   void 支付燃气费();
}
class CCBImpl implements CCB{
   public void 支付燃气费(){}
   ....
   ....
   ....
   ....
}
UnionPay up = new CCBImpl(); //向上造型

int[] arr = {3,345,34,5,23,75};
Arrays.sort(arr);

Student[] stus = new Student[4];
....分别给每个元素赋值
Arrays.sort(stus); //语法可以的
                   //运行后报错
接口的概念-----只要遵守了标准,就能干某件事


cut()方法----多态的
cut是多态的
一个类型的引用在指向不同的对象时有不同的功能
1.理发师-------cut剪发
  外科医生-----cut开刀
  演员---------cut停止表演

人 a = new 理发师();
人 b = new 外科医生();
人 c = new 演员();     //向上造型
a.cut();-----剪发
b.cut();-----开刀
c.cut();-----停止表演

abstract class 人{
   abstract void cut();
}
class 理发师 extends 人{
   void cut(){ 剪发 }
}
class 外科医生 extends 人{
   void cut(){ 开刀 }
}
class 演员 extends 人{
   void cut(){ 停止表演 }
}


同一个对象,造型成不同的类型时,具有不同的功能
我的多态的
2.讲师--------------授课
  儿子他妈----------打他
  我妈的女儿--------发火
  老公我老婆--------打他、发火

讲师 a       = new 我();
儿子妈妈 b   = new 我();
老公的老婆 c = new 我();  //向上造型

a.授课();
b.打他();
c.揍他();
c.发火();

class 我 implements 讲师,儿子妈妈,老公的老婆{
   public void 授课(){}
   public void 打他(){}
   public void 揍他(){}
   public void 发火(){}
}
interface 讲师{
   void 授课();
}
interface 儿子妈妈{
   void 打他();
}
interface 老公的老婆{
   void 揍他();
   void 发火();
}

编译器认为: 子类小,父类大
            实现类小,接口大
达内职员 a = new 讲师();
技术顾问 b = new 讲师();

大到小,需要强转
讲师 aa = (讲师)a;
讲师 bb = (讲师)b;

aa.所有讲师类具有的成员


达内职员 a = new 项目经理();
讲师 aa = (讲师)a;          //异常的
项目经理 aa = (项目经理)a;  //正确的


技术顾问 a = new 讲师();
讲师 aa = (讲师)a;     //正确

达内职员 a = new 讲师();
技术顾问 aa = (技术顾问)a;


达内职员 a = new 讲师();
if(a instanceof 技术顾问){  -----true
   技术顾问 aa = (技术顾问)a;
}
if(a instanceof 项目经理){  -----false
   项目经理 aa = (项目经理)a;
}

语法:
  引用 instanceof 数据类型 ------boolean

小结:
  强转成功与否,看对象

练习:
1.创建接口Inter,包含a()
  创建抽象类Abs,包含b()
  创建类Aoo,继承Abs,实现Inter,包含num变量
  创建类Boo,继承Abs,包含num2变量
2.main()方法中:
  1)Abs引用指向Aoo对象,Inter引用指向Aoo对象
    两个引用分别点后,看点到什么东西
  2)Abs引用指向Aoo对象,
    将引用强转为Aoo类型-------???
    将引用强转为Inter类型-----???
    将引用强转为Boo类型-------???---错误
  3)如上2)中的强转错误的是哪句?
    通过instanceof避免那个错误,即判断后再转
    if(引用 instanceof Boo){
       Boo b = (Boo)引用;
    }

interface UnionPay{  //银联接口
   void 存();
   void 取();
}
interface ICBC extends UnionPay{  //工行接口
   在线支付();
}
interface ABC extends UnionPay{  //农行接口
   支付电话费();
}
class ICBCImpl implements ICBC{  //工行实现类
   public void 存(){}
   public void 取(){}
   public void 在线支付(){}
}
class ABCImpl implements ABC{  //农行实现类
   public void 存(){}
   public void 取(){}
   public void 支付电话费(){}
}

需求: 农行ATM系统,要求只有农行卡才能支付话费
分析:
1.ATM对象
2.抽ATM类
3.设计ATM类的数据和行为

class ABCATM{   //农行ATM机系统
   UnionPay card;   //银联卡
   void insertCard(UnionPay card){  //插卡
      this.card = card;
   }
   void payPhone(){   //支付话费功能
      //判断卡是否是农行卡
      if(card instanceof ABCImpl){
         ABCImpl abc = (ABCImpl)card;
     abc.支付电话费();
      }else{
         System.out.println
         ("卡类型不对,不能支付话费");
      }
   }
}
main(){
   ABCATM atm = new ABCATM();  //农行ATM机对象
   UnionPay card = new ABCImpl(); //银联卡-农行卡
   //UnionPay card = new ICBCImpl();//工行卡
   atm.insertCard(card);
   atm.payPhone();

   if(card instanceof ABCImpl){}
}

1.内部类:一个类只被一个类使用,对外不可见
         eg:宝宝是由妈妈来创造的
2.内部类对象通常只在外部类中被创建
  内部类中可以直接访问外部类的所有成员


class Mama{     //外部类
   String name;
   Mama(String name){
      this.name = name;
   }
   Baby create(){   //创建Baby对象
      return new Baby();
   }
   class Baby{  //内部类
      void mamaName(){
     //this指当前对象,下面错
     System.out.println(this.name);
     //Mama.this指当前外部类对象,下面对
     System.out.println(Mama.this.name);
     //默认有Mama.this,下面对
         System.out.println(name);
      }
   }

}


匿名内部类:
1.何时用:
    有一个类(子类或实现类),
    只需要创建一个对象,
    对象创建完,这个类就没有意义了


class Test{
   main(){
      //创建实现类的对象
      Inter1 o = new Inter1(){
         实现类的成员
      }; //正确

      Inter2 o = new Inter2(){
      }; //错误,{}中尚未重写show()方法
      
      //创建实现类对象,名为o
      Inter2 o = new Inter2(){
         public void show(){
        System.out.println("HiHiHi");
     }
      };
      o.show();

   }
}
interface Inter2{
   void show();
}
interface Inter1{
}

小结:
面向对象3大特征:
1.封装:保证安全
    类-------封装数据、行为
        作为一个整体操作
    方法-----封装功能的实现
        隐藏实现的细节
    访问修饰符----控制访问权限
        保证数据的安全
2.继承:实现代码的重用
    extends
3.多态:多种形态,在继承的基础之上
       提高可维护性、可扩展性
    1)一个类型指向不同对象,有不同的实现
    2)同一个对象造型成不同类型时,有不同的功能

作业:
          swing的侦听中用
1.内部类,匿名内部类----实际应用中应用率不高
  class Mama{
     class Baby{
     }
  }
  问:编译后生成几个.class
2.课后作业
3.运行Shoot游戏,了解需求
  看看数据模型(类、接口)

interface Inter{
}
main(){
   //创建接口对象,错误的
   Inter o = new Inter();  //接口不能被实例化

   //创建接口的实现类的对象---实现类省略了
   Inter o = new Inter(){
      实现类的成员
   };
}

//如下为不使用匿名内部类的方式
interface Inter{
}
class Aoo implements Inter{
   
}
main(){
   Aoo o = new Aoo();
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值