黑马java代码06-08

01_数组(二维数组)

  1 /*
  2     二维数组:就是元素为一维数组的一个数组。
  3     
  4     格式1:
  5         数据类型[][] 数组名 = new 数据类型[m][n];
  6         
  7         m:表示这个二维数组有多少个一维数组。
  8         n:表示每一个一维数组的元素有多少个。
  9         
 10     注意:
 11         A:以下格式也可以表示二维数组
 12             a:数据类型 数组名[][] = new 数据类型[m][n];
 13             b:数据类型[] 数组名[] = new 数据类型[m][n];
 14         B:注意下面定义的区别
 15             int x;
 16             int y;
 17             int x,y;
 18             
 19             int[] x;
 20             int[] y[];
 21             
 22             int[] x,y[];
 23 */
 24 class Array2Demo {
 25     public static void main(String[] args) {
 26          //定义一个二维数组
 27          int[][] arr = new int[3][2];
 28          //定义了一个二维数组arr
 29          //这个二维数组有3个一维数组的元素
 30          //每一个一维数组有2个元素
 31          //输出二维数组名称
 32          System.out.println(arr); //地址值    [[I@175078b
 33          //输出二维数组的第一个元素一维数组的名称
 34          System.out.println(arr[0]); //地址值    [I@42552c
 35          System.out.println(arr[1]); //地址值    [I@e5bbd6
 36          System.out.println(arr[2]); //地址值    [I@8ee016
 37          //输出二维数组的元素
 38          System.out.println(arr[0][0]); //0
 39          System.out.println(arr[0][1]); //0
 40     }
 41 }
 42 
 43 /*
 44     格式2:
 45         数据类型[][] 数组名 = new 数据类型[m][];
 46         
 47         m:表示这个二维数组有多少个一维数组。
 48         列数没有给出,可以动态的给。这一次是一个变化的列数。
 49 */
 50 class Array2Demo2 {
 51     public static void main(String[] args) {
 52         //定义数组
 53         int[][] arr = new int[3][];
 54         
 55         System.out.println(arr);    //[[I@175078b
 56         System.out.println(arr[0]); //null
 57         System.out.println(arr[1]); //null
 58         System.out.println(arr[2]); //null
 59         
 60         //动态的为每一个一维数组分配空间
 61         arr[0] = new int[2];
 62         arr[1] = new int[3];
 63         arr[2] = new int[1];
 64         
 65         System.out.println(arr[0]); //[I@42552c
 66         System.out.println(arr[1]); //[I@e5bbd6
 67         System.out.println(arr[2]); //[I@8ee016
 68         
 69         System.out.println(arr[0][0]); //0
 70         System.out.println(arr[0][1]); //0
 71         //ArrayIndexOutOfBoundsException
 72         //System.out.println(arr[0][2]); //错误
 73         
 74         arr[1][0] = 100;
 75         arr[1][2] = 200;
 76     }
 77 }
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 /*
 88     格式3:
 89         基本格式:
 90             数据类型[][] 数组名 = new 数据类型[][]{{元素1,元素2...},{元素1,元素2...},{元素1,元素2...}};
 91         简化版格式:
 92             数据类型[][] 数组名 = {{元素1,元素2...},{元素1,元素2...},{元素1,元素2...}};
 93             
 94         举例:
 95             int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
 96             int[][] arr = {{1,2,3},{4,5},{6}};
 97 */
 98 class Array2Demo3 {
 99     public static void main(String[] args) {
100         //定义数组
101         int[][] arr = {{1,2,3},{4,5},{6}};
102         
103         System.out.println(arr);
104         System.out.println(arr[0]);
105         System.out.println(arr[1]);
106         System.out.println(arr[2]);
107         
108         System.out.println(arr[0][0]); //1
109         System.out.println(arr[1][0]); //4
110         System.out.println(arr[2][0]); //6
111         
112         System.out.println(arr[0][1]); //2
113         System.out.println(arr[1][1]); //5
114         //越界
115         System.out.println(arr[2][1]); //错误
116     }
117 }
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 /*
131     需求:二维数组遍历
132     
133     外循环控制的是二维数组的长度,其实就是一维数组的个数。
134     内循环控制的是一维数组的长度。
135 */
136 class Array2Test {
137     public static void main(String[] args) {
138         //定义一个二维数组
139         int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
140         
141         //请问谁代表{1,2,3}
142         //arr[0]就是第一个数组
143         //arr[0] = {1,2,3};
144         for(int x=0; x<arr[0].length; x++) {
145             System.out.println(arr[0][x]);
146         }
147         System.out.println("--------------");
148         
149         for(int x=0; x<arr[1].length; x++) {
150             System.out.println(arr[1][x]);
151         }
152         System.out.println("--------------");
153         
154         for(int x=0; x<arr[2].length; x++) {
155             System.out.println(arr[2][x]);
156         }
157         System.out.println("--------------");
158         
159         //用循环改进
160         for(int x=0; x<3; x++) {
161             for(int y=0; y<arr[x].length; y++) {
162                 System.out.print(arr[x][y]+" ");
163             }
164             System.out.println();
165         }
166         System.out.println("--------------");
167         
168         //这个时候,注意了,3是我们根据上面的代码得出来的
169         //但是,它不能针对任何的数组都可以这样
170         //所以,我们应该想办法改进
171         //其实,外面的这个循环的长度就是二维数组的长度
172         
173         for(int x=0; x<arr.length; x++) {
174             for(int y=0; y<arr[x].length; y++) {
175                 System.out.print(arr[x][y]+" ");
176             }
177             System.out.println();
178         }
179         System.out.println("--------------");
180         
181         //用方法改进
182         //调用方法
183         printArray2(arr);
184         System.out.println("--------------");
185         
186         //我们再来一个列数是变化的
187         int[][] arr2 = {{1,2,3},{4,5},{6}};
188         printArray2(arr2);
189     }
190     
191     /*
192         需求:遍历二维数组
193         两个明确:
194             返回值类型:void
195             参数列表:int[][] arr
196     */
197     public static void printArray2(int[][] arr) {
198         for(int x=0; x<arr.length; x++) {
199             for(int y=0; y<arr[x].length; y++) {
200                 System.out.print(arr[x][y]+" ");
201             }
202             System.out.println();
203         }
204     }
205 }
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 /*
219     公司年销售额求和
220     某公司按照季度和月份统计的数据如下:单位(万元)
221     第一季度:22,66,44
222     第二季度:77,33,88
223     第三季度:25,45,65
224     第四季度:11,66,99
225     
226     分析:
227         A:把题目的数据用二维数组来表示
228             int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
229         B:如何求和呢?
230             求和其实就是获取到每一个元素,然后累加即可。
231         C:定义一个求和变量sum,初始化值是0。
232         D:通过遍历就可以得到每一个二维数组的元素。
233         E:把元素累加即可。
234         F:最后输出sum,就是结果。
235 */
236 class Array2Test2 {
237     public static void main(String[] args) {
238         //把题目的数据用二维数组来表示
239         int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
240         
241         //定义一个求和变量sum,初始化值是0。
242         int sum = 0;
243         
244         //通过遍历就可以得到每一个二维数组的元素。
245         for(int x=0; x<arr.length; x++) {
246             for(int y=0; y<arr[x].length; y++) {
247                 //把元素累加即可。
248                 sum += arr[x][y];
249             }
250         }
251         
252         //最后输出sum,就是结果。
253         System.out.println("一年的销售额为:"+sum+"万元");
254     }
255 }
256 
257 
258 
259 
260 
261 
262 /*
263 
264     需求:打印杨辉三角形(行数可以键盘录入)
265     
266     1
267     1 1    
268     1 2 1
269     1 3 3 1
270     1 4 6 4 1 
271     1 5 10 10 5 1
272 
273     分析:看这种图像的规律
274         A:任何一行的第一列和最后一列都是1
275         B:从第三行开始,每一个数据是它上一行的前一列和它上一行的本列之和。
276     
277     步骤:
278         A:首先定义一个二维数组。行数如果是n,我们把列数也先定义为n。
279           这个n的数据来自于键盘录入。
280         B:给这个二维数组任何一行的第一列和最后一列赋值为1
281         C:按照规律给其他元素赋值
282             从第三行开始,每一个数据是它上一行的前一列和它上一行的本列之和。
283         D:遍历这个二维数组。
284 */
285 import java.util.Scanner;
286 
287 class Array2Test3 {
288     public static void main(String[] args) {
289         //创建键盘录入对象
290         Scanner sc = new Scanner(System.in);
291         
292         //这个n的数据来自于键盘录入。
293         System.out.println("请输入一个数据:");
294         int n = sc.nextInt();
295         
296         //定义二维数组
297         int[][] arr = new int[n][n];
298         
299         //给这个二维数组任何一行的第一列和最后一列赋值为1
300         for(int x=0; x<arr.length; x++) {
301             arr[x][0] = 1; //任何一行第1列
302             arr[x][x] = 1; //任何一行的最后1列
303         }
304         
305         //按照规律给其他元素赋值
306         //从第三行开始,每一个数据是它上一行的前一列和它上一行的本列之和。
307         for(int x=2; x<arr.length; x++) {
308             //这里如果y<=x是有个小问题的,就是最后一列的问题
309             //所以这里要减去1
310             //并且y也应该从1开始,因为第一列也是有值了
311             for(int y=1; y<=x-1; y++) {
312                 //每一个数据是它上一行的前一列和它上一行的本列之和。
313                 arr[x][y] = arr[x-1][y-1] + arr[x-1][y];
314             }
315         }
316         
317         //遍历这个二维数组。
318         /*
319         for(int x=0; x<arr.length; x++) {
320             for(int y=0; y<arr[x].length; y++) {
321                 System.out.print(arr[x][y]+"\t");
322             }
323             System.out.println();
324         }
325         */
326         //这个时候,要注意了,内循环的变化必须和曾经讲过的九九乘法表类似
327         for(int x=0; x<arr.length; x++) {
328             for(int y=0; y<=x; y++) {
329                 System.out.print(arr[x][y]+"\t");
330             }
331             System.out.println();
332         }
333     }
334 }
View Code

02_两个思考题

  1 /*
  2     思考题1:看程序写结果,然后分析为什么是这个样子的。并画图讲解。最后总结Java中参数传递规律。
  3     
  4     Java中的参数传递问题:
  5         基本类型:形式参数的改变对实际参数没有影响。
  6         引用类型:形式参数的改变直接影响实际参数。
  7 */
  8 class ArgsDemo {
  9     public static void main(String[] args) {
 10         int a = 10;
 11         int b = 20;
 12         System.out.println("a:"+a+",b:"+b); //a:10,b:20
 13         change(a,b);
 14         System.out.println("a:"+a+",b:"+b); //???    a:10,b:20
 15 
 16         int[] arr = {1,2,3,4,5}; 
 17         change(arr);
 18         System.out.println(arr[1]); //???    4
 19     }
 20 
 21     public static void change(int a,int b) { //a=10,b=20
 22         System.out.println("a:"+a+",b:"+b); //a:10,b:20
 23         a = b;    //a=20
 24         b = a + b; //b=40
 25         System.out.println("a:"+a+",b:"+b); //a:20,b:40
 26     }
 27 
 28     public static void change(int[] arr) { //arr={1,2,3,4,5};
 29         for(int x=0; x<arr.length; x++) {
 30             if(arr[x]%2==0) {
 31                 arr[x]*=2;
 32             }
 33         }
 34         //arr={1,4,3,8,5};
 35     }
 36 }
 37 
 38 
 39 
 40 
 41 
 42 /*
 43     某个公司采用公用电话传递数据信息,数据是小于8位的整数,为了确保安全,
 44     在传递过程中需要加密,加密规则如下:
 45         首先将数据倒序,然后将每位数字都加上5,再用和除以10的余数代替该数字,
 46         最后将第一位和最后一位数字交换。 请任意给定一个小于8位的整数,
 47         然后,把加密后的结果在控制台打印出来。 
 48     题目要求:
 49         A:数据是小于8位的整数
 50             定义一个int类型的数据
 51             int number = 123456;
 52         B:加密规则
 53             a:首先将数据倒序
 54                 结果 654321
 55             b:然后将每位数字都加上5,再用和除以10的余数代替该数字
 56                 结果 109876
 57             c:最后将第一位和最后一位数字交换
 58                 结果 609871
 59         C:把加密后的结果输出在控制台
 60         通过简单的分析,我们知道如果我们有办法把这个数据变成数组就好了。
 61         不是直接写成这个样子的:
 62             int[] arr = {1,2,3,4,5,6};    
 63         如何把数据转成数组呢?
 64             A:定义一个数据
 65                 int number = 123456;
 66             B:定义一个数组,这个时候问题就来了,数组的长度是多少呢?
 67                 int[] arr = new int[8]; //不可能超过8
 68                 在赋值的时候,我用一个变量记录索引的变化。
 69                 定义一个索引值是0
 70                 int index = 0;
 71             C:获取每一个数据
 72                 int ge = number%10
 73                 int shi = number/10%10
 74                 int bai = number/10/10%10
 75                 
 76                 arr[index] = ge;
 77                 index++;
 78                 arr[index] = shi;
 79                 index++;
 80                 arr[index] = bai;
 81                 ...
 82 */
 83 class JiaMiDemo {
 84     public static void main(String[] args) {
 85         //定义一个数据
 86         int number = 123456;
 87         //定义一个数组
 88         int[] arr = new int[8];
 89         //把数据中每一位上的数据获取到后存储到数组中
 90         /*
 91         int index = 0;
 92         arr[index] = number%10; //arr[0]=6;
 93         index++;
 94         arr[index] = number/10%10; //arr[1]=5;
 95         index++;
 96         arr[index] = mumber/10/10%10; //arr[2]=4;
 97         */
 98         //通过观察这个代码,我们发现应该是可以通过循环改进的
 99         int index = 0;
100 while(number>0){     //number=123456,number=12345,number=1234,
101 //number=123,number=12,number=1,number=0
102             arr[index] = number%10; //arr[0]=6,arr[1]=5,arr[2]=4,arr[3]=3,arr[4]=2,arr[5]=1
103             index++;//index=1,index=2,index=3,index=4,index=5,index=6
104             number/=10;    //number=12345,number=1234,
105 //number=123,number=12,number=1,number=0
106         }
107         //然后将每位数字都加上5,再用和除以10的余数代替该数字
108         for(int x=0; x<index; x++) {
109             arr[x] += 5;
110             arr[x] %= 10;
111         }
112         //最后将第一位和最后一位数字交换
113         int temp = arr[0];
114         arr[0] = arr[index-1];
115         arr[index-1] = temp;
116         //输出数据
117         for(int x=0; x<index; x++) {
118             System.out.print(arr[x]);
119         }
120         System.out.println();
121     }
122 }
123 
124 
125 
126 
127 
128 
129 
130 /*
131     把刚才的代码改进一下:
132         A:把数据改进为键盘录入
133         B:把代码改进为方法实现
134         
135         
136         另一个数据的测试:
137         number:1234567
138         第一步:7654321
139         第二步:2109876
140         第三步:6109872
141         
142     知识点:
143         变量
144         数据类型
145         运算符
146         键盘录入
147         语句
148         方法
149         数组
150 */
151 import java.util.Scanner;
152 
153 class JiaMiDemo2 {
154     public static void main(String[] args) {
155         //创建键盘录入对象
156         Scanner sc = new Scanner(System.in);
157         
158         //请输入一个数据
159         System.out.println("请输入一个数据(小于8位):");
160         int number = sc.nextInt();
161         
162         //写功能实现把number进行加密
163         //调用
164         String result = jiaMi(number);
165         System.out.println("加密后的结果是:"+result);
166     }
167     
168     /*
169         需求:写一个功能,把数据number实现加密。
170         两个明确:
171             返回值类型:String 做一个字符串的拼接。
172             参数列表:int number
173     */
174     public static String jiaMi(int number) {
175         //定义数组
176         int[] arr = new int[8];
177         
178         //定义索引
179         int index = 0;
180         
181         //把number中的数据想办法放到数组中
182         while(number > 0) {
183             arr[index] = number%10;
184             index++;
185             number /= 10;
186         }
187         
188         //把每个数据加5,然后对10取得余数
189         for(int x=0; x<index; x++) {
190             arr[x] += 5;
191             arr[x] %= 10;
192         }
193         
194         //把第一位和最后一位交换
195         int temp = arr[0];
196         arr[0] = arr[index-1];
197         arr[index-1] = temp;
198         
199         //把数组的元素拼接成一个字符串返回
200         //定义一个空内容字符串
201         String s = "";
202         
203         for(int x=0; x<index; x++) {
204             s += arr[x];
205         }
206         
207         return s;
208     }
209 }
View Code

03_面向对象

  1 /*
  2     手机事物:
  3         属性:品牌,价格,颜色...
  4         行为:打电话,发短信,玩游戏...
  5         
  6     手机类:
  7         成员变量:品牌,价格,颜色
  8         成员方法:打电话,发短信,玩游戏
  9 */
 10 class Phone {
 11     //品牌
 12     String brand;
 13     //价格
 14     int price;
 15     //颜色
 16     String color;
 17     
 18     //打电话的方法
 19     public void call(String name) {
 20         System.out.println("给"+name+"打电话");
 21     }
 22     
 23     //发短信的方法
 24     public void sendMessage() {
 25         System.out.println("群发短信");
 26     }
 27     
 28     //玩游戏的方法
 29     public void playGame() {
 30         System.out.println("玩游戏");
 31     }
 32 }
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 /*
 44     手机类的测试
 45 */
 46 class Phone {
 47     //品牌
 48     String brand;
 49     //价格
 50     int price;
 51     //颜色
 52     String color;
 53     
 54     //打电话的方法
 55     public void call(String name) {
 56         System.out.println("给"+name+"打电话");
 57     }
 58     
 59     //发短信的方法
 60     public void sendMessage() {
 61         System.out.println("群发短信");
 62     }
 63     
 64     //玩游戏的方法
 65     public void playGame() {
 66         System.out.println("玩游戏");
 67     }
 68 }
 69 
 70 class PhoneDemo {
 71     public static void main(String[] args) {
 72         //创建手机对象
 73         //类名 对象名 = new 类名();
 74         Phone p = new Phone();
 75         
 76         //直接输出成员变量值
 77         System.out.println(p.brand+"---"+p.price+"---"+p.color);
 78         
 79         //给成员变量赋值
 80         p.brand = "诺基亚";
 81         p.price = 100;
 82         p.color = "灰色";
 83         //再次输出
 84         System.out.println(p.brand+"---"+p.price+"---"+p.color);
 85         
 86         //调用方法
 87         p.call("林青霞");
 88         p.sendMessage();
 89         p.playGame();
 90     }
 91 }
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 /*
100     事物:
101         属性    事物的信息描述
102         行为    事物的功能
103     
104     类:
105         成员变量    事物的属性
106         成员方法    事物的行为
107         
108     定义一个类,其实就是定义该类的成员变量和成员方法。
109     
110     案例:我们来完成一个学生类的定义。
111     
112     学生事物:
113         属性:姓名,年龄,地址...
114         行为:学习,吃饭,睡觉...
115         
116     把事物要转换为对应的类:
117     
118     学生类:
119         成员变量:姓名,年龄,地址...
120         成员方法:学习,吃饭,睡觉...
121         
122     成员变量:和以前变量的定义是一样的格式,但是位置不同,在类中方法外。
123     成员方法:和以前的方法定义是一样的格式,但是今天把static先去掉。
124     
125     首先我们应该定义一个类,然后完成类的成员。
126 */
127 //这是我的学生类
128 class Student {
129     //定义变量
130     //姓名
131     String name;
132     //年龄
133     int age;
134     //地址
135     String address;
136     
137     //定义方法
138     //学习的方法
139     public void study() {
140         System.out.println("学生爱学习");
141     }
142     
143     //吃饭的方法
144     public void eat() {
145         System.out.println("学习饿了,要吃饭");
146     }
147     
148     //睡觉的方法
149     public void sleep() {
150         System.out.println("学习累了,要睡觉");
151     }
152 }
153 
154 
155 
156 
157 
158 
159 
160 /*
161     在一个java文件中写两个类:一个基本的类,一个测试类。
162     注意:文件名称和测试类名称一致。
163     
164     如何使用呢?
165         创建对象使用。
166         
167     如何创建对象呢?
168         格式:类名 对象名 = new 类名();
169         
170     如何使用成员变量呢?
171         对象名.变量名
172     如何使用成员方法呢?
173         对象名.方法名(...)
174 */
175 //这是学生类
176 class Student {
177     //姓名
178     String name; //null
179     //年龄
180     int age; //0
181     //地址
182     String address; //null
183     
184     //学习
185     public void study() {
186         System.out.println("学生爱学习");
187     }
188     
189     //吃饭
190     public void eat() {
191         System.out.println("学习饿了,要吃饭");
192     }
193     
194     //睡觉
195     public void sleep() {
196         System.out.println("学习累了,要睡觉");
197     }
198 }
199 
200 //这是学生测试类
201 class StudentDemo {
202     public static void main(String[] args) {
203         //类名 对象名 = new 类名();
204         Student s = new Student();
205         
206         //输出成员变量值
207         //System.out.println(s.name);
208         //System.out.println(s.age);
209         //System.out.println(s.address);
210         //改进写法
211         System.out.println(s.name+"---"+s.age+"---"+s.address);
212         
213         
214         //给成员变量赋值
215         s.name = "林青霞";
216         s.age = 27;
217         s.address = "北京";
218         //赋值后的输出
219         System.out.println(s.name+"---"+s.age+"---"+s.address);
220         
221         //调用方法
222         s.study();
223         s.eat();
224         s.sleep();
225     }
226 }
View Code

小结:

  1 类与对象的概述:
  2 现实世界中是如何描述一个事物的呢?
  3     举例:学生
  4             姓名,年龄,性别...
  5             学习,吃饭,睡觉
  6             
  7     属性:该事物的描述信息
  8     行为:该事物能够做什么
  9     
 10 我们学习编程语言,是为了模拟现实世界的事物的。
 11 而我们学习的编程语言Java中最基本的单位是:类。
 12 所以,我们就应该把事物通过类来体现出来:
 13 由此,我们就得到了现实世界事物和类的对应关系:
 14 
 15 事物:                        类:    
 16     属性                        成员变量
 17     行为                        成员方法
 18     
 19     
 20 类:是一组相关的属性和行为的集合。是一个抽象的概念。
 21 对象:是该类事物的具体表现形式。具体存在的个体。
 22 
 23 举例:
 24     学生:类
 25     班长:对象
 26 面向对象思想概述:
 27 1:面向对象思想
 28     面向对象是基于面向过程的编程思想。
 29     
 30     面向过程:强调的是每一个功能的步骤
 31     面向对象:强调的是对象,然后由对象去调用功能
 32     
 33 2:面向对象的思想特点
 34     A:是一种更符合我们思想习惯的思想
 35     B:可以将复杂的事情简单化
 36     C:将我们从执行者变成了指挥者
 37         
 38     举例:
 39         买电脑:
 40             面向过程:我的了解电脑--了解我自己的需求--找对应的参数信息--去中关村买电脑--讨价还价--买回电脑
 41             面向对象:我知道我要买电脑 -- 班长去给我买 -- 班长就买回来了
 42         洗衣服:
 43             面向过程:把衣服脱下--找一个盆--放点洗衣粉--加点水--把衣服扔进去--搓一搓--清洗衣服--拧干--晾起来
 44             面向对象:把衣服脱下--打开全自动洗衣机--扔进去--一键即可--晾起来
 45         吃饭:
 46             面向过程:去超市买菜--摘菜--洗菜--切菜--炒菜--盛起来-- 47             面向对象:上饭店吃饭,你--服务员(点菜)--厨师(做菜)--服务员(端菜)-- 48             
 49             家常事物,买洗衣机和去饭店太不划算了,所以,找个对象。
 50             但是,你不跟我好好学习,你将来4000,你对象8000。
 51             
 52 3:把大象装进冰箱
 53     面向过程:
 54         动作有哪些呢?
 55             A:打开冰箱门
 56             B:装进大象
 57             C:关闭冰箱门
 58             
 59         代码体现;
 60             class Demo {
 61                 public static void main(String[] args) {
 62                     /*
 63                     System.out.println("打开冰箱门");
 64                     //打开冰箱门的东西,我现在仅仅是为了演示,就写了一个输出语句
 65                     //其实,它可能需要做很多操作。
 66                     //这个时候代码就比较多一些了
 67                     //假设我要多次打开冰箱门,
 68                     //代码一多,每次都写一遍,麻烦不
 69                     //我们就应该用方法改进
 70                     
 71                     System.out.println("装进大象");
 72                     System.out.println("关闭冰箱门");
 73                     */
 74                     
 75                     //写了方法以后,调用就改变了
 76                     open();
 77                     in();
 78                     close();
 79                 }
 80                 
 81                 public static void open() {
 82                     System.out.println("打开冰箱门");
 83                 }
 84                 
 85                 public static void in() {
 86                     System.out.println("装进大象");
 87                 }
 88                 
 89                 public static void close() {
 90                     System.out.println("关闭冰箱门");
 91                 }
 92             }
 93     
 94     面向对象:
 95         我们怎么才能更符合面向对象思想呢?
 96             A:有哪些类呢?
 97             B:每个类有哪些东西呢?
 98             C:类与类直接的关系是什么呢?
 99             
100         把大象装进冰箱的分析? (如何分析有哪些类呢?UML。名词提取法。)
101             A:有哪些类呢?
102                 大象
103                 冰箱
104                 Demo
105             B:每个类有哪些东西呢?
106                 大象:
107                     进去
108                 冰箱:
109                     开门
110                     关门
111                 Demo:
112                     main方法
113             C:类与类直接的关系是什么呢?
114                 Demo中使用大象和冰箱类的功能。
115                 
116         代码体现:
117             class 大象 {
118                 public static void in() {
119                     System.out.println("装进大象");
120                 }
121             }
122             
123             class 冰箱 {
124                 public static void open() {
125                     System.out.println("打开冰箱门");
126                 }
127                 
128                 public static void close() {
129                     System.out.println("关闭冰箱门");
130                 }
131             }
132             
133             class Demo {
134                 public static void main(String[] args) {
135                     冰箱调用开门
136                     大象调用进去
137                     冰箱调用关门
138                 }
139             }
140             
141 4:开发,设计,特征
142 面向对象开发
143     就是不断的创建对象,使用对象,指挥对象做事情。
144     
145 面向对象设计
146     其实就是在管理和维护对象之间的关系。
147 
148 面向对象特征
149     封装(encapsulation)
150     继承(inheritance)
151     多态(polymorphism)
152 
153 知识点:
154 1:二维数组(理解)
155     (1)元素是一维数组的数组。
156     (2)格式:
157         A:数据类型[][] 数组名 = new 数据类型[m][n];
158         B:数据类型[][] 数组名 = new 数据类型[m][];
159         C:数据类型[][] 数组名 = new 数据类型[][]{{...},{...},{...}};
160         D:数据类型[][] 数组名 = {{...},{...},{...}};
161     (3)案例(掌握):
162         A:二维数组的遍历
163         B:二维数组的求和
164         C:杨辉三角形
165 
166 2:两个思考题(理解)
167     (1)Java中的参数传递问题
168         Java中只有值传递。
169         
170         基本类型:形式参数的改变不影响实际参数
171         引用类型:形式参数的改变直接影响实际参数
172     (2)数据加密问题
173         综合的小案例。
174     
175 3:面向对象(掌握)
176     (1)面向对象
177         面向对象是基于面向过程的编程思想
178     (2)面向对象的思想特点
179         A:是一种更符合我们思考习惯的思想
180         B:把复杂的事情简单化
181         C:让我们从执行者变成了指挥者
182         
183         举例:
184             买电脑
185             洗衣服
186             做饭
187             ...
188             万事万物皆对象
189     (3)把大象装进冰箱(理解)
190         A:面向过程实现
191         B:面向对象实现
192         
193         注意:如何让我们的操作更符合面向对象思想呢?
194         A:有哪些类
195         B:每个类有哪些成员
196         C:类与类的关系
197     (4)类与对象
198         A:现实世界的事物
199             属性    事物的基本描述
200             行为    事物的功能
201         B:Java语言中最基本的单位是类。所以,我们要用类来体现事物
202         C:类
203             成员变量    事物属性
204             成员方法    事物行为
205         D:类:是一组相关的属性和行为的集合。是一个抽象的概念。
206           对象:是该类事物的具体存在,是一个具体的实例。(对象)
207           
208           举例:
209             学生:类
210             班长:对象
211     (5)类的定义及使用
212         A:类的定义
213             成员变量    定义格式和以前一样,就是位置不同,在类中,方法外。
214             成员方法    定义格式和以前一样,就是去掉了static。
215         B:使用类的内容
216             a:创建对象? 格式
217                 类名 对象名 =  new 类名();
218             b:如何使用成员变量和成员方法呢
219                 对象名.成员变量
220                 对象名.成员方法()
221     (6)案例:
222         A:学生类的定义和使用
223         B:手机类的定义和使用
224     (7)内存图
225         A:一个对象的内存图
226         B:二个对象的内存图
227         C:三个对象的内存图
228     (8)Java程序的开发,设计和特征
229         A:开发:就是不断的创建对象,通过对象调用功能
230         B:设计:就是管理和维护对象间的关系
231         C:特征
232             a:封装
233             b:继承
234             c:多态
View Code

01_成员变量和局部变量的区别

 1 /*
 2     成员变量和局部变量的区别?
 3         A:在类中的位置不同
 4             成员变量:在类中方法外
 5             局部变量:在方法定义中或者方法声明上
 6         B:在内存中的位置不同
 7             成员变量:在堆内存
 8             局部变量:在栈内存
 9         C:生命周期不同
10             成员变量:随着对象的创建而存在,随着对象的消失而消失
11             局部变量:随着方法的调用而存在,随着方法的调用完毕而消失
12         D:初始化值不同
13             成员变量:有默认初始化值
14             局部变量:没有默认初始化值,必须定义,赋值,然后才能使用。
15         注意事项:
16             局部变量名称可以和成员变量名称一样,在方法中使用的时候,采用的是就近原则。
17 */
18 class Varialbe {
19     //成员变量
20     //int num = 10;
21     int num; //0
22     public void show() {
23         //int num2 = 20; //局部变量
24         //可能尚未初始化变量num2
25         //int num2; //没有默认值
26         int num2 = 20;
27         System.out.println(num2);
28         //int num = 100;
29         System.out.println(num);
30     }
31 }
32 class VariableDemo {
33     public static void main(String[] args) {
34         Varialbe v = new Varialbe();
35         System.out.println(v.num); //访问成员变量
36         v.show();        
37     }
38 }
View Code

02_形式参数是类名的问题

 1 /*
 2     形式参数的问题:
 3         基本类型:形式参数的改变不影响实际参数
 4         引用类型:形式参数的改变直接影响实际参数
 5 */
 6 //形式参数是基本类型
 7 class Demo {
 8     public int sum(int a,int b) {
 9         return a + b;
10     }
11 }
12 //形式参数是引用类型
13 class Student {
14     public void show() {
15         System.out.println("我爱学习");
16     }
17 }
18 class StudentDemo {
19     //如果你看到了一个方法的形式参数是一个类类型(引用类型),这里其实需要的是该类的对象。
20     public void method(Student s) { //调用的时候,把main方法中的s的地址传递到了这里 Student s = new Student();
21         s.show();
22     }
23 }
24 class ArgsTest {
25     public static void main(String[] args) {
26         //形式参数是基本类型的调用
27         Demo d = new Demo();
28         int result = d.sum(10,20);
29         System.out.println("result:"+result);
30         System.out.println("--------------");
31         //形式参数是引用类型的调用
32         //需求:我要调用StudentDemo类中的method()方法
33         StudentDemo sd = new StudentDemo();
34         //创建学生对象
35         Student s = new Student();
36         sd.method(s); //把s的地址给到了这里
37     }
38 }
View Code

03_匿名对象

 1 /*
 2     匿名对象:就是没有名字的对象。
 3     匿名对象的应用场景:
 4         A:调用方法,仅仅只调用一次的时候。
 5             注意:调用多次的时候,不适合。
 6             那么,这种匿名调用有什么好处吗?
 7                 有,匿名对象调用完毕就是垃圾。可以被垃圾回收器回收。
 8         B:匿名对象可以作为实际参数传递
 9 */
10 class Student {
11     public void show() {
12         System.out.println("我爱学习");
13     }
14 }
15 class StudentDemo {
16     public void method(Student s) {
17         s.show();
18     }
19 }
20 class NoNameDemo {
21     public static void main(String[] args) {
22         //带名字的调用
23         Student s = new Student();
24         s.show();
25         s.show();
26         System.out.println("--------------");
27         //匿名对象
28         //new Student();
29         //匿名对象调用方法
30         new Student().show();
31         new Student().show(); //这里其实是重新创建了一个新的对象
32         System.out.println("--------------");
33         //匿名对象作为实际参数传递
34         StudentDemo sd = new StudentDemo();
35         //Student ss = new Student();
36         //sd.method(ss); //这里的s是一个实际参数
37         //匿名对象
38         sd.method(new Student());
39         //在来一个
40         new StudentDemo().method(new Student());
41      }
42 }
View Code

04_封装和private关键字

  1 /*
  2     private:
  3         是一个权限修饰符
  4         可以修饰成员变量和成员方法
  5         被其修饰的成员只能在本类中被访问
  6 */
  7 class Demo {
  8     //int num = 10;
  9     //用private修饰
 10     private int num = 10;
 11     
 12     public void show() {
 13         System.out.println(num);
 14     }
 15     
 16     private void method() {
 17         System.out.println("method");
 18     }
 19     
 20     public void function() {
 21         method();
 22     }
 23 }
 24 
 25 class PrivateDemo {
 26     public static void main(String[] args) {
 27         Demo d = new Demo();
 28         //不能方法私有的成员变量
 29         //System.out.println(d.num);
 30         d.show();
 31         //不能访问私有的成员方法
 32         //d.method();
 33         d.function();
 34     }
 35 }
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 /*
 44     定义一个学生类:
 45         成员变量:name,age
 46         成员方法:show()方法
 47         
 48     我们在使用这个案例的过程中,发现了一个问题:
 49         通过对象去给成员变量赋值,可以赋值一些非法的数据。
 50         这是不合理的。
 51         应该是这个样子的:在赋值之前,先对数据进行判断。
 52         判断到底在哪里做比较合适呢?
 53         StudentDemo类是一个测试类,测试类一般只创建对象,调用方法。    
 54         所以,这个判断应该定义在Student类中。
 55         而我们在成员变量的位置可不可以进行数据判断呢?
 56         是不可以的,因为做数据校验,必须要依靠一些逻辑语句。
 57         逻辑语句是应该定义在方法中的,所以,我们最终决定在Student类中提供一个方法
 58         来对数据进行校验。
 59     
 60     按照我们前面的分析,我们给出了一个方法进行校验。
 61     但是呢,它偏偏不调用方法来赋值,还是直接赋值了,
 62     这样我们的方法就没有起到作用。
 63     我就应该要求你必须使用我的方法,而不能直接调用成员变量赋值。
 64     怎么去强制要求不能直接使用成员变量呢?
 65         针对这种情况,Java就提供了一个关键字 private
 66         
 67     private:私有的。可以修饰成员变量和成员方法。
 68         注意:被private修饰的成员只能在本类中访问。
 69         
 70     其实我讲到现在讲解的是一个封装的思想。
 71     封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。
 72 */
 73 class Student {
 74     //姓名
 75     String name;
 76     //年龄
 77     private int age;
 78     
 79     //写一个方法对数据进行校验
 80     /*
 81         返回值类型:void
 82         参数列表:int a
 83     */
 84     public void setAge(int a) {
 85         if(a < 0 || age > 120) {
 86             System.out.println("你给的年龄有问题");
 87         }else {
 88             age = a;
 89         }
 90     }
 91     
 92     
 93     //show()方法,显示所有成员变量值
 94     public void show() {
 95         System.out.println("姓名:"+name);
 96         System.out.println("年龄:"+age);
 97     }
 98 }
 99 
100 class StudentDemo {
101     public static void main(String[] args) {
102         //创建学生对象
103         Student s = new Student();
104         s.show();
105         System.out.println("--------------");
106         
107         //给成员变量赋值
108         s.name = "林青霞";
109         //s.age = 27;
110         s.setAge(27);
111         s.show();
112         System.out.println("--------------");
113         
114         //给age赋值
115         //s.age = -27; //这个数据是不合理的
116         //通过方法给值
117         s.setAge(-27);
118         s.show();
119         System.out.println("--------------");
120     }
121 }
122 
123 
124 
125 
126 
127 
128 
129 
130 /*
131     封装和private的应用:
132         A:把成员变量用private修饰
133         B:提高对应的getXxx()和setXxx()方法
134 */
135 //定义学生类
136 class Student {
137     //姓名
138     private String name;
139     //年龄
140     private int age;
141     
142     //姓名获取值
143     public String getName() {
144         return name;
145     }
146     
147     //姓名设置值
148     public void setName(String n) {
149         name = n;
150     }
151     
152     //年龄获取值
153     public int getAge() {
154         return age;
155     }
156     
157     //年龄赋值
158     public void setAge(int a) {
159         age = a;
160     }
161 }
162 
163 //测试类
164 class StudentTest {
165     public static void main(String[] args) {
166         //创建学生对象
167         Student s = new Student();
168         
169         //使用成员变量
170         //错误:被私有修饰了,外界不能直接访问了
171         //System.out.println(s.name+"---"+s.age);
172         System.out.println(s.getName()+"---"+s.getAge());
173         
174         //给成员变量赋值
175         //s.name = "林青霞";
176         //s.age = 27;
177         //通过方法给赋值
178         s.setName("林青霞");
179         s.setAge(27);
180         System.out.println(s.getName()+"---"+s.getAge());
181     }
182 }
View Code

05_this关键字

  1 /*
  2     作业:请把手机类写成一个标准类,然后创建对象测试功能。
  3     
  4     手机类:
  5         成员变量:
  6             品牌:String brand;
  7             价格:int price;
  8             颜色:String color;
  9         成员方法:
 10             针对每一个成员变量给出对应的getXxx()/setXxx()方法。
 11         最后定义测试:
 12             创建一个对象,先通过getXxx()方法输出成员变量的值。这一次的结果是:null---0---null
 13             然后通过setXxx()方法给成员变量赋值。再次输出结果。这一次的结果是:三星---2999---土豪金
 14 */
 15 class Phone {
 16     //品牌
 17     private String brand;
 18     //价格
 19     private int price;
 20     //颜色
 21     private String color;
 22     
 23     //getXxx()和setXxx()方法
 24     public String getBrand() {
 25         return brand;
 26     }
 27     
 28     public void setBrand(String brand) {
 29         this.brand = brand;
 30     }
 31     
 32     public int getPrice() {
 33         return price;
 34     }
 35     
 36     public void setPrice(int price) {
 37         this.price = price;
 38     }
 39     
 40     public String getColor() {
 41         return color;
 42     }
 43     
 44     public void setColor(String color) {
 45         this.color = color;
 46     }
 47 }
 48 
 49 class PhoneTest {
 50     public static void main(String[] args) {
 51         //创建手机对象
 52         Phone p = new Phone();
 53         
 54         //直接输出默认值
 55         System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());
 56         
 57         //给成员变量赋值
 58         p.setBrand("三星");
 59         p.setPrice(2999);
 60         p.setColor("土豪金");
 61         //再次输出
 62         System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());
 63     }
 64 }
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 /*
 74     我们曾经曰:起名字要做到见名知意。
 75     
 76     this:是当前类的对象引用。简单的记,它就代表当前类的一个对象。
 77     
 78         注意:谁调用这个方法,在该方法内部的this就代表谁。
 79         
 80     this的场景:
 81         解决局部变量隐藏成员变量
 82 */
 83 //定义学生类
 84 class Student {
 85     //姓名
 86     private String name;
 87     //年龄
 88     private int age;
 89     
 90     //姓名获取值
 91     public String getName() {
 92         return name;
 93     }
 94     
 95     //姓名设置值
 96     public void setName(String name) { //name = "林青霞";
 97         //name = name; //变量的使用规则:就近原则
 98         //这里是类名,目前还没有说过类似的用法,所以这个是有问题的
 99         //这里的调用只能通过对象名
100         //这个对象如果存在,它应该代表的是Student的一个对象。
101         //那么,谁能够代表当前类的对象呢? java就提供了一个关键字 this
102         //Student.name = name;
103         this.name = name;
104     }
105     
106     //年龄获取值
107     public int getAge() {
108         return age;
109     }
110     
111     //年龄赋值
112     public void setAge(int age) {
113         this.age = age;
114     }
115 }
116 
117 //测试类
118 class StudentTest {
119     public static void main(String[] args) {
120         //创建学生对象
121         Student s = new Student();
122         
123         //给成员变量赋值
124         s.setName("林青霞");
125         s.setAge(27);
126         //获取数据
127         System.out.println(s.getName()+"---"+s.getAge());
128     }
129 }
130 
131 
132 
133 
134 
135 
136 
137 
138 /*
139     标准的代码改进版
140     
141     this:哪个对象调用那个方法,this就代表那个对象
142 */
143 class Student {
144     private String name;
145     private int age;
146     
147     public String getName() {
148         return name; //这里其实是隐含了this
149     }
150     
151     public void setName(String name) {
152         this.name = name;
153     }
154     
155     public int getAge() {
156         return age;
157     }
158     
159     public void setAge(int age) {
160         this.age = age;
161     }
162 }
163 
164 class StudentTest2 {
165     public static void main(String[] args) {
166         //创建一个对象
167         Student s1 = new Student();
168         s1.setName("林青霞");
169         s1.setAge(27);
170         System.out.println(s1.getName()+"---"+s1.getAge());
171         
172         //创建第二个对象
173         Student s2 = new Student();
174         s2.setName("刘意");
175         s2.setAge(30);
176         System.out.println(s2.getName()+"---"+s2.getAge());
177     }
178 }
View Code

06_构造方法

  1 /*
  2     构造方法:
  3         给对象的数据进行初始化
  4 
  5     格式:
  6         A:方法名与类名相同
  7         B:没有返回值类型,连void都没有
  8         C:没有具体的返回值
  9 */
 10 class Student {
 11     private String name; //null
 12     private int age; //0
 13     
 14     public Student() {
 15         System.out.println("这是构造方法");
 16     }
 17 }
 18 
 19 class ConstructDemo {
 20     public static void main(String[] args) {
 21         //创建对象
 22         Student s = new Student();
 23         System.out.println(s); //Student@e5bbd6
 24     }
 25 }
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 /*
 43     我们一直在使用构造方法,但是,我们确没有定义构造方法,用的是哪里来的呢?
 44     
 45     构造方法的注意事项:
 46         A:如果我们没有给出构造方法,系统将自动提供一个无参构造方法。
 47         B:如果我们给出了构造方法,系统将不再提供默认的无参构造方法。
 48             注意:这个时候,如果我们还想使用无参构造方法,就必须自己给出。建议永远自己给出无参构造方法
 49         
 50     给成员变量赋值有两种方式:
 51         A:setXxx()
 52         B:构造方法
 53 */
 54 
 55 class Student {
 56     private String name;
 57     private int age;
 58 
 59     public Student() {
 60         //System.out.println("我给了,你还给不");
 61         System.out.println("这是无参构造方法");
 62     }
 63     
 64     //构造方法的重载格式
 65     public Student(String name) {
 66         System.out.println("这是带一个String类型的构造方法");
 67         this.name = name;
 68     }
 69     
 70     public Student(int age) {
 71         System.out.println("这是带一个int类型的构造方法");
 72         this.age = age;
 73     }
 74     
 75     public Student(String name,int age) {
 76         System.out.println("这是一个带多个参数的构造方法");
 77         this.name = name;
 78         this.age = age;
 79     }
 80     
 81     public void show() {
 82         System.out.println(name+"---"+age);
 83     }
 84 }
 85 
 86 class ConstructDemo2 {
 87     public static void main(String[] args) {
 88         //创建对象
 89         Student s = new Student();
 90         s.show();
 91         System.out.println("-------------");
 92         
 93         //创建对象2
 94         Student s2 = new Student("林青霞");
 95         s2.show();
 96         System.out.println("-------------");
 97         
 98         //创建对象3
 99         Student s3 = new Student(27);
100         s3.show();
101         System.out.println("-------------");
102         
103         //创建对象4
104         Student s4 = new Student("林青霞",27);
105         s4.show();
106     }
107 }
View Code

 07_成员方法

  1 /*
  2     标准的手机类练习
  3     
  4     手机类:
  5         成员变量:brand,price,color
  6         构造方法:无参构造
  7         成员方法:getXxx()/setXxx()
  8 */
  9 //定义手机类
 10 class Phone {
 11     //品牌
 12     private String brand;
 13     //价格
 14     private int price;
 15     //颜色
 16     private String color;
 17     
 18     //无参构造方法
 19     public Phone() {}
 20     
 21     //getXxx()和setXxx()方法
 22     public String getBrand() {
 23         return brand;
 24     }
 25     
 26     public void setBrand(String brand) {
 27         this.brand = brand;
 28     }
 29     
 30     public int getPrice() {
 31         return price;
 32     }
 33     
 34     public void setPrice(int price) {
 35         this.price = price;
 36     }
 37     
 38     public String getColor() {
 39         return color;
 40     }
 41     
 42     public void setColor(String color) {
 43         this.color = color;
 44     } 
 45 }
 46 
 47 //手机测试类
 48 class PhoneTest {
 49     public static void main(String[] args) {
 50         //创建对象
 51         Phone p = new Phone();
 52         
 53         //给成员变量赋值
 54         p.setBrand("诺基亚");
 55         p.setPrice(199);
 56         p.setColor("土豪金");
 57         
 58         //获取值
 59         System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());
 60     }
 61 }
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 /*
 74     类的组成:成员变量,成员方法
 75     今天我们又加入了一个新的成员:构造方法。
 76     以后再提类的组成:
 77         成员变量
 78         构造方法
 79         成员方法
 80             根据返回值:
 81                 void类型
 82                 非void类型
 83             形式参数:
 84                 空参方法
 85                 非空参方法
 86 */
 87 class Student {
 88     public String getString() {
 89         return "helloworld";
 90     }
 91 
 92     public void show() {
 93         System.out.println("show");
 94     }
 95     
 96     public void method(String name) {
 97         System.out.println(name);
 98     }
 99     
100     public String function(String s1,String s2) {
101         return s1+s2;
102     }
103 }
104 
105 class StudentDemo {
106     public static void main(String[] args) {
107         //创建对象
108         Student s = new Student();
109         
110         //调用无参无返回值方法
111         s.show();
112         
113         //调用无参有返回值方法
114         String result = s.getString();
115         System.out.println(result);
116         
117         //调用带参无返回值的方法
118         s.method("林青霞");
119         
120         //调用带参带返回值的方法
121         String result2 = s.function("hello","world");
122         System.out.println(result2);
123     }
124 }
125 
126 
127 
128 
129 
130 
131 /*
132     一个标准代码的最终版。
133     学生类:
134         成员变量:
135             name,age
136         构造方法:
137             无参,带两个参
138         成员方法:
139             getXxx()/setXxx()
140             show():输出该类的所有成员变量值
141             
142     给成员变量赋值:
143         A:setXxx()方法
144         B:构造方法
145         
146     输出成员变量值的方式:
147         A:通过getXxx()分别获取然后拼接
148         B:通过调用show()方法搞定
149 */
150 class Student {
151     //姓名
152     private String name;
153     //年龄
154     private int age;
155     //构造方法
156     public Student() {
157     }
158     
159     public Student(String name,int age) {
160         this.name = name;
161         this.age = age;
162     }
163     
164     public String getName() {
165         return name;
166     }
167     
168     public void setName(String name) {
169         this.name = name;
170     }
171     
172     public int getAge() {
173         return age;
174     }
175     
176     public void setAge(int age) {
177         this.age = age;
178     }
179     
180     //输出所有的成员变量值
181     public void show() {
182         System.out.println(name+"---"+age);
183     }
184 }
185 
186 //测试类
187 class StudentTest {
188     public static void main(String[] args) {
189         //方式1给成员变量赋值
190         //无参构造+setXxx()
191         Student s1 = new Student();
192         s1.setName("林青霞");
193         s1.setAge(27);
194         //输出值
195         System.out.println(s1.getName()+"---"+s1.getAge());
196         s1.show();
197         System.out.println("----------------------------");
198         
199         //方式2给成员变量赋值
200         Student s2 = new Student("刘意",30);
201         System.out.println(s2.getName()+"---"+s2.getAge());
202         s2.show();
203     }
204 }
View Code

08_面向对象练习

  1 /*
  2     需求:
  3         定义一个员工类,自己分析出几个成员,
  4         然后给出成员变量,构造方法,getXxx()/setXxx()方法,
  5         以及一个显示所有成员信息的方法。并测试。
  6 
  7     分析:
  8         员工
  9             成员变量:
 10                 员工编号,姓名,年龄
 11             构造方法:
 12                 无参构造方法
 13             成员方法:
 14                 getXxx()/setXxx()
 15                 show();
 16 */
 17 class Employee {
 18     //员工编号
 19     private String employeeId;
 20     //姓名
 21     private String name;
 22     //年龄
 23     private int age;
 24     
 25     //构造方法
 26     public Employee() {}
 27     
 28     //getXxx()/setXxx()
 29     public String getEmployeeId() {
 30         return employeeId;
 31     }
 32     
 33     public void setEmployeeId(String employeeId) {
 34         this.employeeId = employeeId;
 35     }
 36     
 37     public String getName() {
 38         return name;
 39     }
 40     
 41     public void setName(String name) {
 42         this.name = name;
 43     }
 44     
 45     public int getAge() {
 46         return age;
 47     }
 48     
 49     public void setAge(int age) {
 50         this.age = age;
 51     }
 52     
 53     //显示所有成员信息的方法
 54     public void show() {
 55         System.out.println("员工编号是:"+employeeId+"的这个人是:"+name+"的年龄是:"+age);
 56     }
 57 }
 58 
 59 class EmployeeTest {
 60     public static void main(String[] args) {
 61         //创建对象
 62         Employee e = new Employee();
 63         
 64         //给成员变量赋值
 65         e.setEmployeeId("czbk9527");
 66         e.setName("唐伯虎");
 67         e.setAge(18);
 68         
 69         //获取数据
 70         //System.out.println(e.getEmployeeId()+"---"+e.getName()+"---"+e.getAge());
 71     
 72         //我们在Employee类中定义了一个show方法。
 73 //所以,我们改进一下,使用show方法
 74         e.show();
 75     }
 76 }
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 /*
 87     定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。
 88 */
 89 import java.util.Scanner;
 90 
 91 class MyMath {
 92     //加法功能
 93     public int add(int a,int b) {
 94         return a + b;
 95     }
 96     
 97     //减法功能
 98     public int sub(int a,int b) {
 99         return a - b;
100     }
101     
102     //乘法功能
103     public int mul(int a,int b){
104         return a * b;
105     }
106     
107     //除法功能
108     public int div(int a,int b) {
109         return a / b;
110     }
111 }
112 
113 //测试类
114 class MyMathTest {
115     public static void main(String[] args) {
116         //创建键盘录入对象
117         Scanner sc = new Scanner(System.in);
118         
119         System.out.println("请输入第一个操作数:");
120         int firstNumber = sc.nextInt();
121         System.out.println("请输入第二个操作数:");
122         int secondNumber = sc.nextInt();
123         
124         //创建MyMath对象,并使用
125         MyMath mm = new MyMath();
126         
127         System.out.println("加法结果:"+mm.add(firstNumber,secondNumber));
128         System.out.println("减法结果:"+mm.sub(firstNumber,secondNumber));
129         System.out.println("乘法结果:"+mm.mul(firstNumber,secondNumber));
130         System.out.println("除法结果:"+mm.div(firstNumber,secondNumber));
131     }
132 }
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 /*
147     定义一个类Demo,其中定义一个求两个数据和的方法,
148     定义一个测试了Test,进行测试。
149     
150     变量什么时候定义为成员变量:
151         如果这个变量是用来描述这个类的信息的,那么,该变量就应该定义为成员变量。
152         
153     变量到底定义在哪里好呢?
154         变量的范围是越小越好。因为能及时的被回收。
155 */
156 
157 //方式1
158 /*
159 class Demo {
160     public int sum() {
161         int a = 10;
162         int b = 20;
163         int c = a + b;
164         return c;
165     }
166 }
167 */
168 //方式1满足了我们的要求,但是不好。
169 //因为参与操作的数据现在是固定的。
170 
171 //方式2
172 /*
173 class Demo {
174     public int sum(int a,int b) {
175         return a + b;
176     }
177 }
178 */
179 
180 //方式2可以满足我们的要求,但是呢我们学习过来面向对象的思想。
181 //我就再想,a,b可不可以定义为成员变量呢?
182 //如果可以,我们再改进一版
183 class Demo {
184     int a;
185     int b;
186     
187     public int sum() {
188         return a + b;
189     }
190 }
191 //虽然这种方式可以,并且好像是符合了面向对象的思想。
192 //但是不好。
193 //因为我们曾经说过:类是一组相关的属性和行为的集合。
194 //并且类是通过事物转换过来的
195 //而类中的成员变量就是事物的属性
196 //属性是用来描述事物的
197 //同理:成员变量其实是用来描述类的。
198 
199 //测试类
200 class Test {
201     public static void main(String[] args) {
202         //创建对象
203         //方式1测试
204         /*
205         Demo d = new Demo();
206         System.out.println(d.sum());
207         */
208         
209         //方式2测试
210         /*
211         Demo d = new Demo();
212         int a = 10;
213         int b = 20;
214         System.out.println(d.sum(a,b));
215         */
216         
217         //方式3测试
218         Demo d = new Demo();
219         d.a = 10;
220         d.b = 20;
221         System.out.println(d.sum());
222     }
223 }
224 
225 
226 
227 
228 
229 
230 
231 
232 /*
233     定义一个长方形类,定义 求周长和面积的方法,
234     然后定义一个测试了Test2,进行测试。
235 
236     长方形的类:
237         成员变量:
238             长,宽
239         成员方法:
240             求周长:(长+宽)*2;
241             求面积:长*宽
242             
243     注意:
244         import必须出现在所有的class前面。
245 */
246 
247 import java.util.Scanner;
248 
249 class ChangFangXing {
250     //长方形的长
251     private int length;
252     //长方形的宽
253     private int width;
254     
255     public ChangFangXing(){}
256     
257     //仅仅提供setXxx()即可
258     public void setLength(int length) {
259         this.length = length;
260     }
261     
262     public void setWidth(int width) {
263         this.width = width;
264     }
265     
266     //求周长
267     public int getZhouChang() {
268         return (length + width) * 2;
269     }
270     
271     //求面积
272     public int getArea() {
273         return length * width;
274     }
275 }
276 
277 class Test2 {
278     public static void main(String[] args) {
279         //创建键盘录入对象
280         Scanner sc = new Scanner(System.in);
281         
282         System.out.println("请输入长方形的长:");
283         int length = sc.nextInt();
284         System.out.println("请输入长方形的宽:");
285         int width = sc.nextInt();
286         
287         //创建对象
288         ChangFangXing cfx = new ChangFangXing();
289         //先给成员变量赋值
290         cfx.setLength(length);
291         cfx.setWidth(width);
292         
293         System.out.println("周长是:"+cfx.getZhouChang());
294         System.out.println("面积是:"+cfx.getArea());
295     }
296 }
View Code

9_static关键字

  1 /*
  2     main方法的格式讲解:
  3         public static void main(String[] args) {...}
  4         
  5         public:公共的,访问权限是最大的。由于main方法是被jvm调用,所以权限要够大。
  6         static:静态的,不需要创建对象,通过类名就可以。方便jvm的调用。
  7         void:因为我们曾经说过,方法的返回值是返回给调用者,而main方法是被jvm调用。你返回内容给jvm没有意义。
  8         main:是一个常见的方法入口。我见过的语言都是以main作为入口。
  9         String[] args:这是一个字符串数组。值去哪里了?
 10             这个东西到底有什么用啊?怎么给值啊?
 11                 这个东西早期是为了接收键盘录入的数据的。
 12                 格式是:
 13                     java MainDemo hello world java
 14 */
 15 class MainDemo {
 16     public static void main(String[] args) {
 17         //System.out.println(args); //[Ljava.lang.String;@175078b
 18         //System.out.println(args.length); //0
 19         //System.out.println(args[0]); //ArrayIndexOutOfBoundsException
 20         
 21         //接收数据后
 22         System.out.println(args); 
 23         System.out.println(args.length); 
 24         //System.out.println(args[0]); 
 25         for(int x=0; x<args.length; x++) {
 26             System.out.println(args[x]);
 27         }
 28     }
 29 }
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 /*
 41     定义一个人类
 42     
 43     姓名和年龄都是变化的,这个我能接收,因为每个人的姓名和年龄是不同的。
 44     但是,我们现在选取的几个人都是中国人,他们的国籍是一样的。
 45     一样的国籍,我每次创建对象,在堆内存都要开辟这样的空间,
 46     我就觉得有点浪费了。怎么办呢? 
 47         针对多个对象有共同的这样的成员变量值的时候,
 48         Java就提高了一个关键字来修饰:static。
 49 */
 50 class Person {
 51     //姓名
 52     String name;
 53     //年龄
 54     int age;
 55     //国籍
 56     //String country;
 57     static String country;
 58     
 59     public Person(){}
 60     
 61     public Person(String name,int age) {
 62         this.name = name;
 63         this.age = age;
 64     }
 65     
 66     public Person(String name,int age,String country) {
 67         this.name = name;
 68         this.age = age;
 69         this.country = country;
 70     }
 71     
 72     public void show() {
 73         System.out.println("姓名:"+name+",年龄:"+age+",国籍:"+country);
 74     }
 75 }
 76 
 77 class PersonDemo {
 78     public static void main(String[] args) {
 79         //创建对象1
 80         Person p1 = new Person("邓丽君",16,"中国");
 81         p1.show();
 82         
 83         //创建对象2
 84         //Person p2 = new Person("杨幂",22,"中国");
 85         //p2.show();
 86         Person p2 = new Person("杨幂",22);
 87         p2.show();
 88         
 89         //创建对象3
 90         //Person p3 = new Person("凤姐",20,"中国");
 91         //p3.show();
 92         Person p3 = new Person("凤姐",20);
 93         p3.show();
 94         
 95         p3.country = "美国";
 96         p3.show();
 97         
 98         p1.show();
 99         p2.show();
100     }
101 }
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 /*
115     static的特点:(它可以修饰成员变量,还可以修饰成员方法)
116         A:随着类的加载而加载
117             回想main方法。
118         B:优先于对象存在
119         C:被类的所有对象共享
120             举例:咱们班级的学生应该共用同一个班级编号。
121             其实这个特点也是在告诉我们什么时候使用静态?
122                 如果某个成员变量是被所有对象共享的,那么它就应该定义为静态的。
123             举例:
124                 饮水机(用静态修饰)
125                 水杯(不能用静态修饰)
126         D:可以通过类名调用
127             其实它本身也可以通过对象名调用。
128             推荐使用类名调用。
129             
130             静态修饰的内容一般我们称其为:与类相关的,类成员
131 */
132 class Student {
133     //非静态变量
134     int num = 10;
135     
136     //静态变量
137     static int num2 = 20;
138 }
139 
140 class StudentDemo {
141     public static void main(String[] args) {
142         Student s = new Student();
143         System.out.println(s.num);
144         
145         System.out.println(Student.num2);
146         System.out.println(s.num2);
147     }
148 }
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 /*
160     static关键字注意事项
161         A:在静态方法中是没有this关键字的
162             如何理解呢?
163                 静态是随着类的加载而加载,this是随着对象的创建而存在。
164                 静态比对象先存在。
165         B:静态方法只能访问静态的成员变量和静态的成员方法
166                 静态方法:
167                     成员变量:只能访问静态变量
168                     成员方法:只能访问静态成员方法
169                 非静态方法:
170                     成员变量:可以是静态的,也可以是非静态的
171                     成员方法:可是是静态的成员方法,也可以是非静态的成员方法。
172             简单记:
173                 静态只能访问静态。
174 */
175 class Teacher {
176     public int num = 10;
177     public static int num2 = 20;
178     
179     public void show() {
180         System.out.println(num); //隐含的告诉你访问的是成员变量
181         System.out.println(this.num); //明确的告诉你访问的是成员变量
182         System.out.println(num2);
183         
184         //function();
185         //function2();
186     }
187     
188     public static void method() {
189         //无法从静态上下文中引用非静态 变量 num
190         //System.out.println(num);
191         System.out.println(num2);
192         
193         //无法从静态上下文中引用非静态 方法 function()
194         //function();
195         function2();
196     }
197     
198     public void function() {
199     
200     }
201     
202     public static void function2() {
203     
204     }
205 }
206 
207 class TeacherDemo {
208     public static void main(String[] args) {
209         //创建对象
210         Teacher t = new Teacher();
211         t.show();
212         System.out.println("------------");
213         t.method();
214     }
215 }
View Code

小结:

  1 1:成员变量和局部变量的区别(理解)
  2     (1)在类中的位置不同
  3         成员变量:类中方法外
  4         局部变量:方法定义中或者方法声明上
  5     (2)在内存中的位置不同
  6         成员变量:在堆中
  7         局部变量:在栈中
  8     (3)生命周期不同
  9         成员变量:随着对象的创建而存在,随着对象的消失而消失
 10         局部变量:随着方法的调用而存在,随着方法的调用完毕而消失
 11     (4)初始化值不同
 12         成员变量:有默认值
 13         局部变量:没有默认值,必须定义,赋值,然后才能使用
 14         
 15 2:类作为形式参数的问题?(理解)
 16     (1)如果你看到一个方法需要的参数是一个类名,就应该知道这里实际需要的是一个具体的对象。
 17 
 18 3:匿名对象(理解)
 19     (1)没有名字的对象
 20     (2)应用场景
 21         A:调用方法,仅仅只调用一次的时候。
 22         b:可以作为实际参数传递。
 23         
 24 4:封装(理解)
 25     (1)隐藏实现细节,提供公共的访问方式
 26     (2)好处:
 27         A:隐藏实现细节,提供公共的访问方式
 28         B:提高代码的复用性
 29         C:提高代码的安全性
 30     (3)设计原则
 31         把不想让外界知道的实现细节给隐藏起来,提供公共的访问方式
 32     (4)private是封装的一种体现。
 33         封装:类,方法,private修饰成员变量
 34 
 35 5:private关键字(掌握)
 36     (1)私有的意义,可以修饰成员变量和成员方法
 37     (2)特点:
 38         被private修饰的后的成员只能在本类中被访问
 39     (3)private的应用:
 40         以后再写一个类的时候:
 41             把所有的成员变量给private了
 42             提供对应的getXxx()/setXxx()方法
 43 
 44 6:this关键字(掌握)
 45     (1)代表当前类的引用对象
 46         记住:哪个对象调用方法,该方法内部的this就代表那个对象
 47     (2)this的应用场景:
 48         A:解决了局部变量隐藏成员变量的问题
 49         B:其实this还有其他的应用,明天讲解。
 50 
 51 7:构造方法(掌握)
 52     (1)作用:用于对对象的数据进行初始化
 53     (2)格式:
 54         A:方法名和类名相同
 55         B:没有返回值类型,连void都不能有
 56         C:没有返回值
 57         
 58         思考题:构造方法中可不可以有return语句呢?
 59         可以。而是我们写成这个样子就OK了:return;
 60         其实,在任何的void类型的方法的最后你都可以写上:return;
 61     (3)构造方法的注意事项
 62         A:如果我们没写构造方法,系统将提供一个默认的无参构造方法
 63         B:如果我们给出了构造方法,系统将不再提供默认构造方法
 64             如果这个时候,我们要使用无参构造方法,就必须自己给出。
 65             推荐:永远手动自己给出无参构造方法。
 66     (4)给成员变量赋值的方式
 67         A:setXxx()
 68         B:带参构造方法
 69     (5)标准案例
 70         class Student {
 71             private String name;
 72             private int age;
 73             
 74             public Student(){}
 75             
 76             public Student(String name,int age) {
 77                 this.name = name;
 78                 this.age = age;
 79             }
 80             
 81             public String getName() {
 82                 return name;
 83             }
 84             
 85             public void setName(String name) {
 86                 this.name = name;
 87             }
 88             
 89             public int getAge() {
 90                 return age;
 91             }
 92             
 93             public void setAge(int age) {
 94                 this.age = age;
 95             }
 96         }
 97         
 98         测试:
 99         class StudentDemo {
100             public static void main(String[] args) {
101                 //方式1
102                 Student s1 = new Student();
103                 s1.setName("林青霞");
104                 s1.setAge(27);
105                 System.out.println(s1.getName()+"---"+s1.getAge());
106                 
107                 //方式2
108                 Student s2 = new Student("刘意",30);
109                 System.out.println(s2.getName()+"---"+s2.getAge());
110             }
111         }
112         
113 
114 8:代码:Student s = new Student();做了哪些事情?(理解)
115     (1)把Student.class文件加载到内存
116     (2)在栈内存为s开辟空间
117     (3)在堆内存为学生对象申请空间
118     (4)给学生的成员变量进行默认初始化。null,0
119     (5)给学生的成员变量进行显示初始化。林青霞,27
120     (6)通过构造方法给成员变量进行初始化。刘意,30
121     (7)对象构造完毕,把地址赋值给s变量
122         
123 9:面向对象的练习题(掌握)
124     (1)标准的手机类的定义和测试
125     (2)Demo类有求和方法,Test类进行测试。
126         什么时候定义成员变量?
127         当该变量是用来描述一个类的时候。
128     (3)长方形案例
129     (4)员工案例
130     (5)MyMath案例(自己提供加减乘除并测试)
131     
132 10:static关键字(理解)
133     (1)静态的意思。可以修饰成员变量和成员方法。
134     (2)静态的特点:
135         A:随着类的加载而加载
136         B:优先与对象存在
137         C:被类的所有对象共享
138             这其实也是我们判断该不该使用静态的依据。
139             举例:饮水机和水杯的问题思考
140         D:可以通过类名调用
141             既可以通过对象名调用,也可以通过类名调用,建议通过类名调用。
142     (3)静态的内存图
143         静态的内容在方法区的静态区
144     (4)静态的注意事项;
145         A:在静态方法中没有this对象
146         B:静态只能访问静态(代码测试过)
147     (5)静态变量和成员变量的区别
148         A:所属不同
149             静态变量:属于类,类变量
150             成员变量:属于对象,对象变量,实例变量
151         B:内存位置不同
152             静态变量:方法区的静态区
153             成员变量:堆内存
154         C:生命周期不同
155             静态变量:静态变量是随着类的加载而加载,随着类的消失而消失
156             成员变量:成员变量是随着对象的创建而存在,随着对象的消失而消失
157         D:调用不同
158             静态变量:可以通过对象名调用,也可以通过类名调用
159             成员变量:只能通过对象名调用
160     (6)main方法是静态的
161         public:权限最大
162         static:不用创建对象调用
163         void:返回值给jvm没有意义
164         main:就是一个常见的名称。
165         String[] args:可以接收数据,提供程序的灵活性
166             格式:java MainDemo hello world java
167                   java MainDemo 10 20 30
View Code

01_代码块概述和讲解

  1 /*
  2     代码块:在Java中,使用{}括起来的代码被称为代码块。
  3     根据其位置和声明的不同,可以分为
  4         局部代码块:局部位置,用于限定变量的生命周期。
  5         构造代码块:在类中的成员位置,用{}括起来的代码。每次调用构造方法执行前,都会先执行构造代码块。
  6             作用:可以把多个构造方法中的共同代码放到一起,对对象进行初始化。
  7         静态代码块:在类中的成员位置,用{}括起来的代码,只不过它用static修饰了。
  8             作用:一般是对类进行初始化。
  9             
 10     面试题?
 11         静态代码块,构造代码块,构造方法的执行顺序?
 12         静态代码块 -- 构造代码块 -- 构造方法
 13         静态代码块:只执行一次
 14         构造代码块:每次调用构造方法都执行
 15 */
 16 class Code {
 17     static {
 18         int a = 1000;
 19         System.out.println(a);
 20     }
 21 
 22     //构造代码块
 23     {
 24         int x = 100;
 25         System.out.println(x);
 26     }
 27     
 28     //构造方法
 29     public Code(){
 30         System.out.println("code");
 31     }
 32     
 33     //构造方法
 34     public Code(int a){
 35         System.out.println("code");
 36     }
 37     
 38     //构造代码块
 39     {
 40         int y = 200;
 41         System.out.println(y);
 42     }
 43     
 44     //静态代码块
 45     static {
 46         int b = 2000;
 47         System.out.println(b);
 48     }
 49 }
 50 
 51 class CodeDemo {
 52     public static void main(String[] args) {
 53         //局部代码块
 54         {
 55             int x = 10;
 56             System.out.println(x);
 57         }
 58         //找不到符号
 59         //System.out.println(x);
 60         {
 61             int y = 20;
 62             System.out.println(y);
 63         }
 64         System.out.println("---------------");
 65         
 66         Code c = new Code();    
 67         System.out.println("---------------");
 68         Code c2 = new Code();
 69         System.out.println("---------------");
 70         Code c3 = new Code(1);
 71     }
 72 }
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 /*
 87     写程序的执行结果。
 88     
 89     林青霞都60了,我很伤心
 90     我是main方法
 91     Student 静态代码块
 92     Student 构造代码块
 93     Student 构造方法
 94     Student 构造代码块
 95     Student 构造方法
 96 */
 97 class Student {
 98     static {
 99         System.out.println("Student 静态代码块");
100     }
101     
102     {
103         System.out.println("Student 构造代码块");
104     }
105     
106     public Student() {
107         System.out.println("Student 构造方法");
108     }
109 }
110 
111 class StudentDemo {
112     static {
113         System.out.println("林青霞都60了,我很伤心");
114     }
115     
116     public static void main(String[] args) {
117         System.out.println("我是main方法");
118         
119         Student s1 = new Student();
120         Student s2 = new Student();
121     }
122 }
View Code

02_继承

   1 /*
   2     继承概述:
   3         把多个类中相同的内容给提取出来定义到一个类中。
   4         
   5     如何实现继承呢?    
   6         Java提供了关键字:extends
   7         
   8     格式:
   9         class 子类名 extends 父类名 {}
  10         
  11     好处:
  12         A:提高了代码的复用性
  13         B:提高了代码的维护性
  14         C:让类与类之间产生了关系,是多态的前提
  15     
  16     类与类产生了关系,其实也是继承的一个弊端:
  17         类的耦合性增强了。
  18         
  19         开发的原则:低耦合,高内聚。
  20         耦合:类与类的关系
  21         内聚:就是自己完成某件事情的能力
  22 */
  23 
  24 //使用继承前
  25 /*
  26 class Student {
  27     public void eat() {
  28         System.out.println("吃饭");
  29     }
  30     
  31     public void sleep() {
  32         System.out.println("睡觉");
  33     }
  34 }
  35 
  36 class Teacher {
  37     public void eat() {
  38         System.out.println("吃饭");
  39     }
  40     
  41     public void sleep() {
  42         System.out.println("睡觉");
  43     }
  44 }
  45 */
  46 
  47 //使用继承后
  48 class Person {
  49     public void eat() {
  50         System.out.println("吃饭");
  51     }
  52     
  53     public void sleep() {
  54         System.out.println("睡觉");
  55     }
  56 }
  57 
  58 class Student extends Person {}
  59 
  60 class Teacher extends Person {}
  61 
  62 class ExtendsDemo {
  63     public static void main(String[] args) {
  64         Student s = new Student();
  65         s.eat();
  66         s.sleep();
  67         System.out.println("-------------");
  68         
  69         Teacher t = new Teacher();
  70         t.eat();
  71         t.sleep();
  72     }
  73 }
  74 
  75 
  76 
  77 
  78 
  79 
  80 
  81 
  82 
  83 
  84 
  85 
  86 /*
  87     Java中继承的特点:
  88         A:Java只支持单继承,不支持多继承。
  89             有些语言是支持多继承,格式:extends 类1,类2,...
  90         B:Java支持多层继承(继承体系)
  91 */
  92 
  93 /*
  94 class Father {}
  95 class Mother {}
  96 class Son exnteds Father {} //正确的
  97 class Son extends Father,Mother {} // 错误的
  98 */
  99 
 100 class GrandFather {
 101     public void show() {
 102         System.out.println("我是爷爷");
 103     }
 104 }
 105 
 106 class Father extends GrandFather {
 107     public void method(){
 108         System.out.println("我是老子");
 109     }
 110 }
 111 
 112 class Son extends Father {}
 113 
 114 class ExtendsDemo2 {
 115     public static void main(String[] args) {
 116         Son s = new Son();
 117         s.method(); //使用父亲的
 118         s.show(); //使用爷爷的
 119     }
 120 }
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 130 /*
 131     继承的注意事项:
 132         A:子类只能继承父类所有非私有的成员(成员方法和成员变量)
 133         B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法。
 134         C:不要为了部分功能而去继承
 135             class A {
 136                 public void show1(){}
 137                 public void show2(){}
 138             }
 139             
 140             class B {
 141                 public void show2(){}
 142                 public void show3(){}
 143             }
 144             
 145             //我们发现B类中出现了和A类一样的show2()方法,所以,我们就用继承来体现
 146             class B extends A {
 147                 public void show3(){}
 148             }
 149             这样其实不好,因为这样你不但有了show2(),还多了show1()。
 150             有可能show1()不是你想要的。
 151             
 152     那么,我们什么时候考虑使用继承呢?
 153         继承其实体现的是一种关系:"is a"。
 154             Person
 155                 Student
 156                 Teacher
 157             水果
 158                 苹果
 159                 香蕉
 160                 橘子
 161                 
 162         采用假设法。
 163             如果有两个类A,B。只有他们符合A是B的一种,或者B是A的一种,就可以考虑使用继承。
 164 */
 165 class Father {
 166     private int num = 10;
 167     public int num2 = 20;
 168     
 169     //私有方法,子类不能继承
 170     private void method() {
 171         System.out.println(num);
 172         System.out.println(num2);
 173     }
 174     
 175     public void show() {
 176         System.out.println(num);
 177         System.out.println(num2);
 178     }
 179 }
 180 
 181 class Son extends Father {
 182     public void function() {
 183         //num可以在Father中访问private
 184         //System.out.println(num); //子类不能继承父类的私有成员变量
 185         System.out.println(num2);
 186     }
 187 }
 188 
 189 class ExtendsDemo3 {
 190     public static void main(String[] args) {
 191         // 创建对象
 192         Son s = new Son();
 193         //s.method(); //子类不能继承父类的私有成员方法
 194         s.show();
 195         s.function();
 196     }
 197 }
 198 
 199 
 200 
 201 
 202 
 203 
 204 
 205 
 206 
 207 /*
 208     类的组成:
 209         成员变量:
 210         构造方法:
 211         成员方法:
 212     而现在我们又讲解了继承,所以,我们就应该来考虑一下,类的组成部分的各自关系。
 213     
 214     继承中成员变量的关系:
 215         A:子类中的成员变量和父类中的成员变量名称不一样,这个太简单。
 216         B:子类中的成员变量和父类中的成员变量名称一样,这个怎么玩呢?
 217             在子类方法中访问一个变量的查找顺序:
 218                 a:在子类方法的局部范围找,有就使用
 219                 b:在子类的成员范围找,有就使用
 220                 c:在父类的成员范围找,有就使用
 221                 d:如果还找不到,就报错。
 222 */
 223 class Father {
 224     public int num = 10;
 225     
 226     public void method() {
 227         int num = 50;
 228     }
 229 }
 230 
 231 class Son extends Father {
 232     public int num2 = 20;
 233     public int num = 30;
 234     
 235     public void show() {
 236         int num = 40;
 237         System.out.println(num);
 238         System.out.println(num2);
 239         // 找不到符号
 240         System.out.println(num3);
 241     }
 242 }
 243 
 244 class ExtendsDemo4 {
 245     public static void main(String[] args) {
 246         //创建对象
 247         Son s = new Son();
 248         s.show();
 249     }
 250 }
 251 
 252 
 253 
 254 
 255 
 256 
 257 
 258 
 259 /*
 260     问题是:
 261         我不仅仅要输出局部范围的num,还要输出本类成员范围的num。怎么办呢?
 262         我还想要输出父类成员范围的num。怎么办呢?
 263             如果有一个东西和this相似,但是可以直接访问父类的数据就好了。
 264             恭喜你,这个关键字是存在的:super。
 265             
 266     this和super的区别?
 267         分别是什么呢?
 268             this代表本类对应的引用。
 269             super代表父类存储空间的标识(可以理解为父类引用,可以操作父类的成员)
 270 
 271         怎么用呢?
 272             A:调用成员变量
 273                 this.成员变量 调用本类的成员变量
 274                 super.成员变量 调用父类的成员变量
 275             B:调用构造方法
 276                 this(...)    调用本类的构造方法
 277                 super(...)    调用父类的构造方法
 278             C:调用成员方法
 279                 this.成员方法 调用本类的成员方法
 280                 super.成员方法 调用父类的成员方法
 281 */
 282 class Father {
 283     public int num = 10;
 284 }
 285 class Son extends Father {
 286     public int num = 20;
 287     
 288     public void show() {
 289         int num = 30;
 290         System.out.println(num);
 291         System.out.println(this.num);
 292         System.out.println(super.num);
 293     }
 294 }
 295 class ExtendsDemo5 {
 296     public static void main(String[] args) {
 297         Son s = new Son();
 298         s.show();
 299     }
 300 }
 301 
 302 
 303 
 304 /*
 305     继承中构造方法的关系
 306         A:子类中所有的构造方法默认都会访问父类中空参数的构造方法
 307         B:为什么呢?
 308             因为子类会继承父类中的数据,可能还会使用父类的数据。
 309             所以,子类初始化之前,一定要先完成父类数据的初始化。
 310             
 311             注意:子类每一个构造方法的第一条语句默认都是:super();
 312 */
 313 class Father {
 314     int age;
 315 
 316     public Father() {
 317         System.out.println("Father的无参构造方法");
 318     }
 319     
 320     public Father(String name) {
 321         System.out.println("Father的带参构造方法");
 322     }
 323 }
 324 
 325 class Son extends Father {
 326     public Son() {
 327         //super();
 328         System.out.println("Son的无参构造方法");
 329     }
 330     
 331     public Son(String name) {
 332         //super();
 333         System.out.println("Son的带参构造方法");
 334     }
 335 }    
 336 
 337 class ExtendsDemo6 {
 338     public static void main(String[] args) {
 339         //创建对象
 340         Son s = new Son();
 341         System.out.println("------------");
 342         Son s2 = new Son("林青霞");
 343     }
 344 }
 345 
 346 
 347 
 348 
 349 
 350 
 351 
 352 /*
 353     如果父类没有无参构造方法,那么子类的构造方法会出现什么现象呢?
 354         报错。
 355     如何解决呢?    
 356         A:在父类中加一个无参构造方法
 357         B:通过使用super关键字去显示的调用父类的带参构造方法
 358         C:子类通过this去调用本类的其他构造方法
 359             子类中一定要有一个去访问了父类的构造方法,否则父类数据就没有初始化。
 360             
 361     注意事项:
 362         this(...)或者super(...)必须出现在第一条语句上。
 363         如果不是放在第一条语句上,就可能对父类的数据进行了多次初始化,所以必须放在第一条语句上。
 364 */
 365 class Father {
 366     /*
 367     public Father() {
 368         System.out.println("Father的无参构造方法");
 369     }
 370     */
 371     
 372     public Father(String name) {
 373         System.out.println("Father的带参构造方法");
 374     }
 375 }
 376 
 377 class Son extends Father {
 378     public Son() {
 379         super("随便给");
 380         System.out.println("Son的无参构造方法");
 381         //super("随便给");
 382     }
 383     
 384     public Son(String name) {
 385         //super("随便给");
 386         this();
 387         System.out.println("Son的带参构造方法");
 388     }
 389 }
 390 class ExtendsDemo7 {
 391     public static void main(String[] args) {
 392         Son s = new Son();
 393         System.out.println("----------------");
 394         Son ss = new Son("林青霞");
 395     }
 396 }
 397 
 398 
 399 
 400 
 401 
 402 
 403 
 404 
 405 
 406 
 407 
 408 
 409 
 410 
 411 /*
 412     继承中成员方法的关系:
 413         A:子类中的方法和父类中的方法声明不一样,这个太简单。
 414         B:子类中的方法和父类中的方法声明一样,这个该怎么玩呢?
 415             通过子类对象调用方法:
 416                 a:先找子类中,看有没有这个方法,有就使用
 417                 b:再看父类中,有没有这个方法,有就使用
 418                 c:如果没有就报错。
 419 */
 420 class Father {
 421     public void show() {
 422         System.out.println("show Father");
 423     }
 424 }
 425 
 426 class Son extends Father {
 427     public void method() {
 428         System.out.println("method Son");
 429     }
 430     
 431     public void show() {
 432         System.out.println("show Son");
 433     }
 434 }
 435 
 436 class ExtendsDemo8 {
 437     public static void main(String[] args) {
 438         //创建对象
 439         Son s = new Son();
 440         s.show();
 441         s.method();
 442         //s.fucntion(); //找不到符号
 443     }
 444 }
 445 
 446 
 447 
 448 
 449 
 450 
 451 
 452 
 453 
 454 /*
 455     方法重写:子类中出现了和父类中方法声明一模一样的方法。
 456     
 457     方法重载:
 458         本类中出现的方法名一样,参数列表不同的方法。与返回值无关。
 459 
 460     子类对象调用方法的时候:
 461         先找子类本身,再找父类。
 462         
 463     方法重写的应用:
 464         当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法。
 465         这样,即沿袭了父类的功能,又定义了子类特有的内容。
 466         
 467     案例:
 468         A:定义一个手机类。
 469         B:通过研究,我发明了一个新手机,这个手机的作用是在打完电话后,可以听天气预报。
 470         按照我们基本的设计,我们把代码给写出来了。
 471         但是呢?我们又发现新手机应该是手机,所以,它应该继承自手机。
 472         其实这个时候的设计,并不是最好的。
 473         因为手机打电话功能,是手机本身就具备的最基本的功能。
 474         所以,我的新手机是不用在提供这个功能的。
 475         但是,这个时候,打电话功能就没有了。这个不好。
 476         最终,还是加上这个功能。由于它继承了手机类,所以,我们就直接使用父类的功能即可。
 477         那么,如何使用父类的功能呢?通过super关键字调用
 478 */
 479 class Phone {
 480     public void call(String name) {
 481         System.out.println("给"+name+"打电话");
 482     }
 483 }
 484 
 485 class NewPhone extends Phone {
 486     public void call(String name) {
 487         //System.out.println("给"+name+"打电话");
 488         super.call(name);
 489         System.out.println("可以听天气预报了");
 490     }
 491 }
 492 
 493 class ExtendsDemo9 {
 494     public static void main(String[] args) {
 495         NewPhone np = new NewPhone();
 496         np.call("林青霞");
 497     }
 498 }
 499 
 500 
 501 
 502 
 503 
 504 
 505 
 506 
 507 /*
 508     方法重写的注意事项
 509         A:父类中私有方法不能被重写
 510             因为父类私有方法子类根本就无法继承
 511         B:子类重写父类方法时,访问权限不能更低
 512             最好就一致
 513         C:父类静态方法,子类也必须通过静态方法进行重写
 514             其实这个算不上方法重写,但是现象确实如此,至于为什么算不上方法重写,多态中我会讲解
 515             
 516         子类重写父类方法的时候,最好声明一模一样。
 517 */
 518 class Father {
 519     //private void show() {}
 520     
 521     /*
 522     public void show() {
 523         System.out.println("show Father");
 524     }
 525     */
 526     
 527     void show() {
 528         System.out.println("show Father");
 529     }
 530     /*
 531     public static void method() {
 532         
 533     }
 534     */
 535     
 536     public void method() {
 537         
 538     }
 539 }
 540 
 541 class Son extends Father {
 542     //private void show() {}
 543 
 544     /*
 545     public void show() {
 546         System.out.println("show Son");
 547     }
 548     */
 549     
 550     public void show() {
 551         System.out.println("show Son");
 552     }
 553     
 554     
 555     public static void method() {
 556     
 557     }
 558     
 559     /*
 560     public void method() {
 561     
 562     }
 563     */
 564 }
 565 
 566 class ExtendsDemo10 {
 567     public static void main(String[] args) {
 568         Son s = new Son();
 569         s.show();
 570     }
 571 }
 572 
 573 
 574 
 575 
 576 
 577 
 578 
 579 
 580 /*
 581     看程序写结果:
 582         A:成员变量    就近原则
 583         B:this和super的问题
 584             this访问本类的成员
 585             super访问父类的成员
 586         C:子类构造方法执行前默认先执行父类的无参构造方法
 587         D:一个类的初始化过程
 588             成员变量进行初始化
 589                 默认初始化
 590                 显示初始化
 591                 构造方法初始化
 592                 
 593     结果:
 594         fu
 595         zi
 596         30
 597         20
 598         10
 599 */
 600 class Fu{
 601     public int num = 10;
 602     public Fu(){
 603         System.out.println("fu");
 604     }
 605 }
 606 class Zi extends Fu{
 607     public int num = 20;
 608     public Zi(){
 609         System.out.println("zi");
 610     }
 611     public void show(){
 612         int num = 30;
 613         System.out.println(num); //30
 614         System.out.println(this.num); //20
 615         System.out.println(super.num); //10
 616     }
 617 }
 618 class ExtendsTest {
 619     public static void main(String[] args) {
 620         Zi z = new Zi();
 621         z.show();
 622     }
 623 }
 624 
 625 
 626 
 627 
 628 
 629 
 630 
 631 
 632 
 633 
 634 /*
 635     看程序写结果:
 636         A:一个类的静态代码块,构造代码块,构造方法的执行流程
 637             静态代码块 > 构造代码块 > 构造方法
 638         B:静态的内容是随着类的加载而加载
 639             静态代码块的内容会优先执行
 640         C:子类初始化之前先会进行父类的初始化
 641         
 642     结果是:
 643         静态代码块Fu
 644         静态代码块Zi
 645         构造代码块Fu
 646         构造方法Fu
 647         构造代码块Zi
 648         构造方法Zi
 649 */
 650 class Fu {
 651     static {
 652         System.out.println("静态代码块Fu");
 653     }
 654 
 655     {
 656         System.out.println("构造代码块Fu");
 657     }
 658 
 659     public Fu() {
 660         System.out.println("构造方法Fu");
 661     }
 662 }
 663 
 664 class Zi extends Fu {
 665     static {
 666         System.out.println("静态代码块Zi");
 667     }
 668 
 669     {
 670         System.out.println("构造代码块Zi");
 671     }
 672 
 673     public Zi() {
 674         System.out.println("构造方法Zi");
 675     }
 676 }
 677 
 678 class ExtendsTest2 {
 679     public static void main(String[] args) {
 680         Zi z = new Zi();
 681     }
 682 }
 683 
 684 
 685 
 686 
 687 
 688 
 689 
 690 
 691 
 692 
 693 
 694 
 695 
 696 
 697 /*
 698     学生案例和老师案例讲解
 699     
 700     学生:
 701         成员变量;姓名,年龄
 702         构造方法:无参,带参
 703         成员方法:getXxx()/setXxx()
 704     老师:
 705         成员变量;姓名,年龄
 706         构造方法:无参,带参
 707         成员方法:getXxx()/setXxx()
 708 */
 709 //定义学生类
 710 class Student {
 711     //姓名
 712     private String name;
 713     //年龄
 714     private int age;
 715     
 716     public Student() {
 717     }
 718 
 719     public Student(String name,int age) {
 720         this.name = name;
 721         this.age = age;
 722     }
 723     
 724     public String getName() {
 725         return name;
 726     }
 727     
 728     public void setName(String name) {
 729         this.name = name;
 730     }
 731     
 732     public int getAge() {
 733         return age;
 734     }
 735     
 736     public void setAge(int age) {
 737         this.age = age;
 738     }
 739 }
 740 
 741 //定义老师类
 742 class Teacher {
 743     //姓名
 744     private String name;
 745     //年龄
 746     private int age;
 747     
 748     public Teacher() {
 749     }
 750 
 751     public Teacher(String name,int age) {
 752         this.name = name;
 753         this.age = age;
 754     }
 755     
 756     public String getName() {
 757         return name;
 758     }
 759     
 760     public void setName(String name) {
 761         this.name = name;
 762     }
 763     
 764     public int getAge() {
 765         return age;
 766     }
 767     
 768     public void setAge(int age) {
 769         this.age = age;
 770     }
 771 }
 772 
 773 class ExtendsTest3 {
 774     public static void main(String[] args) {
 775         //创建学生对象并测试
 776         //方式1
 777         Student s1 = new Student();
 778         s1.setName("林青霞");
 779         s1.setAge(27);
 780         System.out.println(s1.getName()+"---"+s1.getAge());
 781         
 782         //方式2
 783         Student s2 = new Student("林青霞",27);
 784         System.out.println(s2.getName()+"---"+s2.getAge());
 785         
 786         //对应的老师测试我不想了,留给你们自己练习。
 787     }
 788 }
 789 
 790 
 791 
 792 
 793 
 794 
 795 
 796 
 797 
 798 
 799 
 800 /*
 801     学生案例和老师案例讲解
 802     
 803     学生:
 804         成员变量;姓名,年龄
 805         构造方法:无参,带参
 806         成员方法:getXxx()/setXxx()
 807     老师:
 808         成员变量;姓名,年龄
 809         构造方法:无参,带参
 810         成员方法:getXxx()/setXxx()
 811         
 812     看上面两个类的成员,发现了很多相同的东西,所以我们就考虑抽取一个共性的类:
 813     人:
 814         成员变量;姓名,年龄
 815         构造方法:无参,带参
 816         成员方法:getXxx()/setXxx()
 817         
 818         学生 继承 人
 819         老师 继承 人
 820 */
 821 //定义人类
 822 class Person {
 823     //姓名
 824     private String name;
 825     //年龄
 826     private int age;
 827     
 828     public Person() {
 829     }
 830 
 831     public Person(String name,int age) { //"林青霞",27
 832         this.name = name;
 833         this.age = age;
 834     }
 835     
 836     public String getName() {
 837         return name;
 838     }
 839     
 840     public void setName(String name) {
 841         this.name = name;
 842     }
 843     
 844     public int getAge() {
 845         return age;
 846     }
 847     
 848     public void setAge(int age) {
 849         this.age = age;
 850     }
 851 }
 852 
 853 //定义学生类
 854 class Student extends Person {
 855     public Student() {}
 856     
 857     public Student(String name,int age) { //"林青霞",27
 858         //this.name = name;
 859         //this.age = age;
 860         super(name,age);
 861     }
 862 }
 863 
 864 //定义老师类
 865 class Teacher extends Person {
 866 
 867 }
 868 
 869 class ExtendsTest4 {
 870     public static void main(String[] args) {
 871         //创建学生对象并测试
 872         //方式1
 873         Student s1 = new Student();
 874         s1.setName("林青霞");
 875         s1.setAge(27);
 876         System.out.println(s1.getName()+"---"+s1.getAge());
 877         
 878         //方式2
 879         Student s2 = new Student("林青霞",27);
 880         System.out.println(s2.getName()+"---"+s2.getAge());
 881         
 882         //补齐老师类中的代码并进行测试。
 883     }
 884 }
 885 
 886 
 887 
 888 
 889 
 890 
 891 
 892 
 893 
 894 
 895 
 896 
 897 
 898 
 899 
 900 
 901 
 902 /*
 903     猫狗案例讲解
 904     
 905     先找到具体的事物,然后发现具体的事物有共性,才提取出一个父类。
 906     
 907     猫:
 908         成员变量:姓名,年龄,颜色
 909         构造方法:无参,带参
 910         成员方法:
 911             getXxx()/setXxx()
 912             eat()
 913             palyGame()
 914     狗:
 915         成员变量:姓名,年龄,颜色
 916         构造方法:无参,带参
 917         成员方法:
 918             getXxx()/setXxx()
 919             eat()
 920             lookDoor()
 921             
 922     共性:
 923         成员变量:姓名,年龄,颜色
 924         构造方法:无参,带参
 925         成员方法:
 926             getXxx()/setXxx()
 927             eat()
 928             
 929     把共性定义到一个类中,这个类的名字叫:动物。
 930     动物类:
 931         成员变量:姓名,年龄,颜色
 932         构造方法:无参,带参
 933         成员方法:
 934             getXxx()/setXxx()
 935             eat()
 936             
 937         猫:    
 938             构造方法:无参,带参
 939             成员方法:palyGame()
 940         狗:
 941             构造方法:无参,带参
 942             成员方法:lookDoor()
 943 */
 944 //定义动物类
 945 class Animal {
 946     //姓名
 947     private String name;
 948     //年龄
 949     private int age;
 950     //颜色
 951     private String color;
 952     
 953     public Animal() {}
 954     
 955     public Animal(String name,int age,String color) {
 956         this.name = name;
 957         this.age = age;
 958         this.color = color;
 959     }
 960     
 961     public String getName() {
 962         return name;
 963     }
 964     
 965     public void setName(String name) {
 966         this.name = name;
 967     }
 968     
 969     public int getAge() {
 970         return age;
 971     }
 972     
 973     public void setAge(int age) {
 974         this.age = age;
 975     }
 976     
 977     public String getColor() {
 978         return color;
 979     }
 980     
 981     public void setColor(String color) {
 982         this.color = color;
 983     }
 984     
 985     public void eat() {
 986         System.out.println("不要睡了,该吃饭了");
 987     }
 988 }
 989 
 990 //定义猫类
 991 class Cat extends Animal {
 992     public Cat() {}
 993     
 994     public Cat(String name,int age,String color) {
 995         super(name,age,color);
 996     }
 997     
 998     public void playGame() {
 999         System.out.println("猫玩英雄联盟");
1000     }
1001 }
1002 
1003 //定义狗类
1004 class Dog extends Animal {
1005     public Dog() {}
1006     
1007     public Dog(String name,int age,String color) {
1008         super(name,age,color);
1009     }
1010     
1011     public void lookDoor() {
1012         System.out.println("狗看家");
1013     }
1014 }
1015 
1016 //测试类
1017 class ExtendsTest5 {
1018     public static void main(String[] args) {
1019         //测试猫
1020         //方式1
1021         Cat c1 = new Cat();
1022         c1.setName("Tom");
1023         c1.setAge(3);
1024         c1.setColor("白色");
1025         System.out.println("猫的名字是:"+c1.getName()+";年龄是:"+c1.getAge()+";颜色是:"+c1.getColor());
1026         c1.eat();
1027         c1.playGame();
1028         System.out.println("---------------");
1029         
1030         //方式2
1031         Cat c2 = new Cat("杰瑞",5,"土豪金");
1032         System.out.println("猫的名字是:"+c2.getName()+";年龄是:"+c2.getAge()+";颜色是:"+c2.getColor());
1033         c2.eat();
1034         c2.playGame();
1035         
1036         //作业:测试狗
1037     }
1038 }
1039 
1040 
1041 
1042 
1043 
1044 /*
1045     看程序写结果:
1046         A:成员变量的问题
1047             int x = 10; //成员变量是基本类型
1048             Student s = new Student(); //成员变量是引用类型
1049         B:一个类的初始化过程
1050             成员变量的初始化
1051                 默认初始化
1052                 显示初始化
1053                 构造方法初始化
1054         C:子父类的初始化(分层初始化)
1055             先进行父类初始化,然后进行子类初始化。        
1056     结果:
1057         YXYZ    
1058     问题:
1059         虽然子类中构造方法默认有一个super()
1060         初始化的时候,不是按照那个顺序进行的。
1061         而是按照分层初始化进行的。
1062         它仅仅表示要先初始化父类数据,再初始化子类数据。
1063 */
1064 class X {
1065     Y b = new Y();
1066     X() {
1067         System.out.print("X");
1068     }
1069 }
1070 
1071 class Y {
1072     Y() {
1073         System.out.print("Y");
1074     }
1075 }
1076 
1077 public class Z extends X {
1078     Y y = new Y();
1079     Z() {
1080         //super
1081         System.out.print("Z");
1082     }
1083     public static void main(String[] args) {
1084         new Z(); 
1085     }
1086 }
View Code

小结:

  1 继承概述:
  2 首先我来写两个代码:
  3     //定义学生类
  4     class Student {
  5         String name;
  6         int age;
  7         
  8         public Student(){}
  9         
 10         //getXxx()/setXxx()
 11     
 12         public void eat() {
 13             System.out.println("吃饭");
 14         }
 15     }
 16     
 17     //定义老师类
 18     class Teacher {
 19         String name;
 20         int age;
 21         
 22         public Teacher(){}
 23         
 24         //getXxx()/setXxx()
 25         
 26         public void eat() {
 27             System.out.println("吃饭");
 28         }
 29     }
 30 我们观察上面两个代码:
 31     发现name,age成员变量,以及getXxx()/setXxx(),还有eat()等都是相同的。
 32     如果我们后来继续定义类,举例:工人类,军人类。他们是不是也具备这些内容。
 33     那么,我们每一次定义这样的类的时候,都要把这些重复的内容都重新定义一遍。
 34     麻烦不?麻烦。所以,我们要考虑改进?
 35 如何改进呢?
 36     我这想的:我能不能把这些相同的内容给定义到一个独立的类中。
 37     然后,让这多个类和这个独立的类产生一个关系,有了这个关系后,
 38     这多个类就可以具备这个独立的类的功能。
 39     为了实现这个效果,java就提供了一个技术:继承。
 40     
 41     父亲:
 42         4个儿子
 43 继承怎么表示呢?继承的格式是什么样子的呢?
 44     class Fu {}
 45     
 46     class Zi extends Fu {
 47         
 48     } 
 49     
 50 我们就回头修改我们的代码:
 51     class Person {
 52         String name;
 53         int age;
 54         
 55         public Person(){}
 56         
 57         //getXxx()/setXxx()
 58     
 59         public void eat() {
 60             System.out.println("吃饭");
 61         }
 62     }
 63     
 64     class Student extends Person {
 65         public Student(){}
 66     }
 67     
 68     class Teacher extends Person {
 69         public Teacher(){}
 70     }
 71 
 72 两个面试题:
 73 1:方法重写和方法重载的区别?方法重载能改变返回值类型吗?
 74 
 75 方法重写:
 76     在子类中,出现和父类中一模一样的方法声明的现象。
 77     
 78 方法重载:
 79     同一个类中,出现的方法名相同,参数列表不同的现象。
 80 
 81 
 82 方法重载能改变返回值类型,因为它和返回值类型无关。
 83 
 84 
 85 Override:方法重写
 86 Overload:方法重载
 87 
 88 2:this关键字和super关键字分别代表什么?以及他们各自的使用场景和作用。
 89 
 90 this:代表当前类的对象引用
 91 super:代表父类存储空间的标识。(可以理解为父类的引用,通过这个东西可以访问父类的成员)
 92 
 93 场景:
 94     成员变量:
 95         this.成员变量
 96         super.成员变量
 97     构造方法:
 98         this(...)
 99         super(...)
100     成员方法:
101         this.成员方法
102         super.成员方法
103 
104 
105 
106 知识点:
107 1:如何制作帮助文档(了解)
108     (1)写一个类
109     (2)加入文档注释
110     (3)通过javadoc工具生成即可
111         javadoc -d 目录 -author -version ArrayTool.java
112 
113 2:通过JDK提供的API学习了Math类(掌握)
114     (1)API(Application Programming Interface)
115         应用程序编程接口(帮助文档)
116     (2)如何使用呢?
117         请参照
118             day08\code\02_如何使用JDK提供的帮助文档\如何使用帮助文档.txt
119     (3)Math类
120         A:是针对数学进行操作的类
121         B:没有构造方法,因为它的成员都是静态的
122         C:产生随机数
123             public static double random(): [0.0,1.0)
124         D:如何产生一个1-100之间的随机数
125             int number = (int)(Math.random()*100)+1;
126         E:猜数字小游戏
127 
128 3:代码块(理解)
129     (1)用{}括起来的代码。
130     (2)分类:
131         A:局部代码块
132             用于限定变量的生命周期,及早释放,提高内存利用率。
133         B:构造代码块
134             把多个构造方法中相同的代码可以放到这里,每个构造方法执行前,首先执行构造代码块。
135         C:静态代码块
136             对类的数据进行初始化,仅仅只执行一次。
137     (3)静态代码块,构造代码块,构造方法的顺序问题?
138         静态代码块 > 构造代码块 > 构造方法
139     
140 4:继承(掌握)
141     (1)把多个类中相同的成员给提取出来定义到一个独立的类中。然后让这多个类和该独立的类产生一个关系,
142        这多个类就具备了这些内容。这个关系叫继承。
143     (2)Java中如何表示继承呢?格式是什么呢?
144         A:用关键字extends表示
145         B:格式:
146             class 子类名 extends 父类名 {}
147     (3)继承的好处:
148         A:提高了代码的复用性
149         B:提高了代码的维护性
150         C:让类与类产生了一个关系,是多态的前提
151     (4)继承的弊端:
152         A:让类的耦合性增强。这样某个类的改变,就会影响其他和该类相关的类。
153             原则:低耦合,高内聚。
154             耦合:类与类的关系
155             内聚:自己完成某件事情的能力
156         B:打破了封装性
157     (5)Java中继承的特点
158         A:Java中类只支持单继承
159         B:Java中可以多层(重)继承(继承体系)
160     (6)继承的注意事项:
161         A:子类不能继承父类的私有成员
162         B:子类不能继承父类的构造方法,但是可以通过super去访问
163         C:不要为了部分功能而去继承
164     (7)什么时候使用继承呢?
165         A:继承体现的是:is a的关系。
166         B:采用假设法
167     (8)Java继承中的成员关系
168         A:成员变量
169             a:子类的成员变量名称和父类中的成员变量名称不一样,这个太简单
170             b:子类的成员变量名称和父类中的成员变量名称一样,这个怎么访问呢?
171                 子类的方法访问变量的查找顺序:
172                     在子类方法的局部范围找,有就使用。
173                     在子类的成员范围找,有就使用。
174                     在父类的成员范围找,有就使用。
175                     找不到,就报错。
176         B:构造方法
177             a:子类的构造方法默认会去访问父类的无参构造方法
178                 是为了子类访问父类数据的初始化
179             b:父类中如果没有无参构造方法,怎么办?
180                 子类通过super去明确调用带参构造
181                 子类通过this调用本身的其他构造,但是一定会有一个去访问了父类的构造
182                 让父类提供无参构造
183         C:成员方法
184             a:子类的成员方法和父类中的成员方法名称不一样,这个太简单
185             b:子类的成员方法和父类中的成员方法名称一样,这个怎么访问呢?
186                 通过子类对象访问一个方法的查找顺序:
187                     在子类中找,有就使用
188                     在父类中找,有就使用
189                     找不到,就报错
190     (9)两个面试题:
191         A:Override和Overload的区别?Overload是否可以改变返回值类型?
192         B:this和super的区别和各自的作用?
193     (10)数据初始化的面试题
194         A:一个类的初始化过程
195         B:子父类的构造执行过程
196         C:分层初始化
197     (11)案例:
198         A:学生和老师案例
199             继承前
200             继承后
201         B:猫狗案例的分析和实现
View Code

 

posted @ 2018-08-21 20:32  XiaoYulong  阅读(203)  评论(0)    收藏  举报