01、面向过程与面向对象 何谓“面向对象”的编程思想? 首先解释一下“思想”。 先问你个问题:你想做个怎样的人? 可能你会回答:我想做个好人,孝敬父母,尊重长辈,关爱亲朋… 你看,这就是思想。这是你做人的思想,或者说,是你做人的原则。做人有做人的原则,编程也有编程的原则。这些编程的原则呢,就是编程思想。
面向过程(POP) 与面向对象(OOP)
面向对象:Object Oriented Programming 面向过程:Procedure Oriented Programming 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
面向对象的思想概述
程序员从面向过程的执行者转化成了面向对象的指挥者 面向对象分析方法分析问题的思路和步骤:根据问题需要,选择问题所针对的现实世界中的实体。 从实体中寻找解决问题相关的属性和功能,这些属性和功能就形成了概念世界中的类。 把抽象的实体用计算机语言进行描述,形成计算机世界中类的定义。即借助某种程序语言,把类构造成计算机能够识别和处理的数据结构。 将类实例化成计算机世界中的对象。对象是计算机世界中解决问题的最终工具。 02、 类和对象 2.1、Java 类及类的成员 现实世界的生物体,大到鲸鱼,小到蚂蚁,都是由最基本的细胞构成的。同理,Java 代码世界是由诸多个不同功能的类构成的。
现实生物世界中的细胞又是由什么构成的呢?细胞核、细胞质、… 那么,Java 中用类 class 来描述事物也是如此。常见的类的成员有:
2.2、类与对象的创建及使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); p1.name = "Tom" ; p1.age = 25 ; p1.isMale = true ; System.out.println(p1.name); p1.eat(); p1.sleep(); p1.talk("chinese" ); Person p2 = new Person (); System.out.println(p2.name); System.out.println(p2.isMale); Person p3 = p1; System.out.println(p3.name); p3.age = 10 ; System.out.println(p1.age); } }class Person { String name; int age; boolean isMale; public void eat () { System.out.println("吃饭" ); } public void sleep () { System.out.println("睡觉" ); } public void talk (String language) { System.out.println("人可以说话,使用的是:" + language); } }
2.3、对象的创建和使用:内存解析
堆(Heap),此内存区域的唯一目的就是存放对象实例,几乎所有的对象实例都在这里分配内存。这一点在 Java 虚拟机规范中的描述是:所有的对象实例以及数组都要在堆上分配。 通常所说的栈(Stack),是指虚拟机栈。虚拟机栈用于存储局部变量等。局部变量表存放了编译期可知长度的各种基本数据类型(boolean、byte、char、short、int、float、long、double)、对象引用(reference 类型,它不等同于对象本身,是对象在堆内存的首地址)。方法执行完,自动释放。 方法区(MethodArea),用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据。 1、案例 1
1 2 3 4 5 6 7 Person p1= newPerson(); p1.name = "Tom" ; p1.isMale = true ;Person p2 = new Person (); sysout(p2.name);Person p3 = p1; p3.age = 10 ;
2、案例 2
1 2 3 4 5 Person p1= newPerson(); p1.name = "胡利民" ; p1.age = 23 ;Person p2 = new Person (); p2.age = 10 ;
03、类的成员之一:属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 public class UserTest { public static void main (String[] args) { User u1 = new User (); System.out.println(u1.name); System.out.println(u1.age); System.out.println(u1.isMale); u1.talk("俄语" ); } }class User { String name; public int age; boolean isMale; public void talk (String language) { System.out.println("我们使用" + language + "进行交流。" ); } public void eat () { String food = "石头饼" ; System.out.println("北方人喜欢吃:" + food); } }
1、练习1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class School { public static void main (String[] args) { Student stu = new Student (); stu.name = "小明" ; stu.age = 16 ; Teacher tea = new Teacher (); tea.name = "王老师" ; tea.age = 27 ; tea.say(stu.name,stu.age); stu.say(tea.name, tea.age); } }class Student { String name; int age; String major; String interests; void say (String name, int age) { System.out.println("这个学生是:" +name+"年龄是:" +age); } }class Teacher { String name; int age; String teachAge; String course; void say (String name, int age) { System.out.println("这个老师是:" +name+"年龄是:" +age); } }
04、 类的成员之二:方法 4.1、类中方法的声明和使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 public class CustomerTest { public static void main (String[] args) { Customer cust1 = new Customer (); cust1.eat(); cust1.sleep(8 ); } }class Customer { String name; int age; boolean isMale; public void eat () { System.out.println("客户吃饭" ); return ; } public void sleep (int hour) { System.out.println("休息了" + hour + "个小时" ); eat(); } public String getName () { if (age > 18 ){ return name; }else { return "Tom" ; } } public String getNation (String nation) { String info = "我的国籍是:" + nation; return info; } public void info () { } }
1、练习1
创建一个Person类,其定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class Person { String name; int age; int sex; public void study () { System.out.println("studying" ); } public void showAge () { System.out.println("age:" + age); } public int addAge (int i) { age += i; return age; } }
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); p1.name = "Tom" ; p1.age = 18 ; p1.sex = 1 ; p1.study(); p1.showAge(); int newAge = p1.addAge(2 ); System.out.println(p1.name + "的年龄为" + newAge); System.out.println(p1.age); Person p2 = new Person (); p2.showAge(); p2.addAge(10 ); p2.showAge(); p1.showAge(); } }
2、练习2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class CircleTest { public static void main (String[] args) { Circle c1 = new Circle (); c1.radius = 2.1 ; c1.findArea(); double area = c1.findArea(3.4 ); System.out.println(area); } }class Circle { double radius; public void findArea () { double area = Math.PI * radius * radius; System.out.println("面积为:" + area); } public double findArea (Double r) { double area = 3.14 * r * r; return area; } }
3、练习3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public class ExerTest { public static void main (String[] args) { ExerTest esr = new ExerTest (); System.out.println("面积为:" + esr.method(6 ,5 )); } public int method (int m,int n) { for (int i = 0 ;i < m;i++){ for (int j = 0 ;j < n;j++){ System.out.print("* " ); } System.out.println(); } return m * n; } }
4、练习四
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public class StudentTest { public static void main (String[] args) { Student[] stu = new Student [20 ]; for (int i = 0 ;i <stu.length;i++){ stu[i] = new Student (); stu[i].number = i + 1 ; stu[i].state = (int )(Math.random() * (6 - 1 + 1 ) + 1 ); stu[i].score = (int )(Math.random() * (100 - 0 + 1 )); } for (int i = 0 ;i < stu.length;i++){ System.out.println(stu[i].info()); } System.out.println("*********以下是问题1*********" ); for (int i = 0 ;i < stu.length;i++){ if (stu[i].state == 3 ){ System.out.println(stu[i].info()); } } System.out.println("********以下是问题2**********" ); for (int i = 0 ;i < stu.length - 1 ;i++){ for (int j = 0 ;j <stu.length - 1 - i;j++){ if (stu[j].score >stu[j+1 ].score){ Student temp = stu[j]; stu[j] = stu[j+1 ]; stu[j+1 ] = temp; } } } for (int i = 0 ;i < stu.length;i++){ System.out.println(stu[i].info()); } } }class Student { int number; int state; int score; public String info () { return "学号:" + number + ",年级:" + state + ",成绩:" + score; } }
4-1、练习四优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 public class StudentTest2 { public static void main (String[] args) { Student2[] stu = new Student2 [20 ]; for (int i = 0 ;i <stu.length;i++){ stu[i] = new Student2 (); stu[i].number = i + 1 ; stu[i].state = (int )(Math.random() * (6 - 1 + 1 ) + 1 ); stu[i].score = (int )(Math.random() * (100 - 0 + 1 )); } StudentTest2 test = new StudentTest2 (); test.print(stu); System.out.println("*********以下是问题1*********" ); test.searchState(stu, 3 ); System.out.println("********以下是问题2**********" ); test.sort(stu); for (int i = 0 ;i < stu.length;i++){ System.out.println(stu[i].info()); } } public void print (Student2[] stu) { for (int i = 0 ;i < stu.length;i++){ System.out.println(stu[i].info()); } } public void searchState (Student2[] stu,int state) { for (int i = 0 ;i < stu.length;i++){ if (stu[i].state == state){ System.out.println(stu[i].info()); } } } public void sort (Student2[] stu) { for (int i = 0 ;i < stu.length - 1 ;i++){ for (int j = 0 ;j <stu.length - 1 - i;j++){ if (stu[j].score >stu[j+1 ].score){ Student2 temp = stu[j]; stu[j] = stu[j+1 ]; stu[j+1 ] = temp; } } } } }class Student2 { int number; int state; int score; public String info () { return "学号:" + number + ",年级:" + state + ",成绩:" + score; } }
4.2、理解“万事万物皆对象” 4.3、对象数组的内存解析 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Student[] stus= newStudent[5 ]; stus[0 ] = new Student (); sysout(stus[0 ].state); sysout(stus[1 ]); sysout(stus[1 ].number); stus[1 ] = new Student (); sysout(stus[1 ].number);class Student { int number; int state = 1 ; int score; }
4.4、匿名对象的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public class InstanceTest { public static void main (String[] args) { Phone p = new Phone (); System.out.println(p); p.sendEmail(); p.playGame(); new Phone ().price = 1999 ; new Phone ().showPrice(); PhoneMall mall = new PhoneMall (); mall.show(new Phone ()); } }class PhoneMall { public void show (Phone phone) { phone.sendEmail(); phone.playGame(); } }class Phone { double price; public void sendEmail () { System.out.println("发邮件" ); } public void playGame () { System.out.println("打游戏" ); } public void showPrice () { System.out.println("手机价格为:" + price); } }
4.5、自定义数组的工具类 1、工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 public class ArrayUtil { public int getMax (int [] arr) { int maxValue = arr[0 ]; for (int i = 1 ; i < arr.length; i++) { if (maxValue < arr[i]) { maxValue = arr[i]; } } return maxValue; } public int getMin (int [] arr) { int minValue = arr[0 ]; for (int i = 1 ; i < arr.length; i++) { if (minValue > arr[i]) { minValue = arr[i]; } } return minValue; } public int getSum (int [] arr) { int sum = 0 ; for (int i = 0 ; i < arr.length; i++) { sum += arr[i]; } return sum; } public int getAvg (int [] arr) { int avgValue = getSum(arr) / arr.length; return avgValue; } public void reverse (int [] arr) { for (int i = 0 ; i < arr.length / 2 ; i++) { int temp = arr[i]; arr[i] = arr[arr.length - i - 1 ]; arr[arr.length - i - 1 ] = temp; } } public int [] copy(int [] arr) { int [] arr1 = new int [arr.length]; for (int i = 0 ; i < arr1.length; i++) { arr1[i] = arr[i]; } return null ; } public void sort (int [] arr) { 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 temp = arr[j]; arr[j] = arr[j + 1 ]; arr[j + 1 ] = temp; } } } } public void print (int [] arr) { System.out.print("[" ); for (int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + "," ); } System.out.println("]" ); } public int getIndex (int [] arr, int dest) { for (int i = 0 ; i < arr.length; i++) { if (dest==arr[i]) { return i; } } return -1 ; } }
2、测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class ArrayUtilTest { public static void main (String[] args) { ArrayUtil util = new ArrayUtil (); int [] arr = new int []{32 ,5 ,26 ,74 ,0 ,96 ,14 ,-98 ,25 }; int max = util.getMax(arr); System.out.println("最大值为:" + max); System.out.println("查找:" ); int index = util.getIndex(arr, 5 ); if (index > 0 ){ System.out.println("找到了,索引地址:" + index); }else { System.out.println("没找到" ); } } }
4.6、方法的重载(overload) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public class OverLoadTest { public static void main (String[] args) { OverLoadTest test = new OverLoadTest (); test.getSum(1 , 2 ); } public void getSum (int i,int j) { System.out.println("1" ); } public void getSum (double d1,double d2) { System.out.println("2" ); } public void getSum (String s,int i) { System.out.println("3" ); } public void getSum (int i,String s) { } }
1、举例
1 2 3 4 5 6 7 8 9 1. 判断:与void show (int a,char b,double c) {}构成重载的有: a)void show (int x,char y,double z) {} b)int show (int a,double c,char b) {} c) void show (int a,double c,char b) {} d) boolean show (int c,char b) {} e) void show (double c) {} f) double show (int x,char y,double z) {} g) void shows () {double c}
2、编程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 public class OverLoadever { public static void main (String[] args) { OverLoadever test = new OverLoadever (); test.mOL(5 ); test.mOL(6 , 4 ); test.mOL("fg" ); int num1 = test.max(18 , 452 ); System.out.println(num1); double num2 = test.max(5.6 , -78.6 ); System.out.println(num2); double num3 = test.max(15 , 52 , 42 ); System.out.println(num3); } public void mOL (int i) { System.out.println(i*i); } public void mOL (int i,int j) { System.out.println(i*j); } public void mOL (String s) { System.out.println(s); } public int max (int i,int j) { return (i > j) ? i : j; } public double max (double i,double j) { return (i > j) ? i : j; } public double max (double d1,double d2,double d3) { double max = (d1 > d2) ? d1 : d2; return (max > d3) ? max : d3; } }
4.7、可变个数的形参 JavaSE 5.0 中提供了Varargs(variable number of arguments)机制,允许直接定义能和多个实参相匹配的形参
。从而,可以用一种更简单的方式,来传递个数可变的实参。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public class MethodArgs { public static void main (String[] args) { MethodArgs test = new MethodArgs (); test.show(12 ); test.show(new String [] { "AA" , "BB" , "CC" }); } public void show (int i) { } public void show (String... strs) { System.out.println("show(String ...strs)" ); for (int i = 0 ; i < strs.length; i++) { System.out.println(strs[i]); } } public void show (int i, String... strs) { } }
4.8、方法参数的值传递机制(重点!!!) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public class ValueTransferTest { public static void main (String[] args) { System.out.println("**********基本数据类型:***********" ); int m = 10 ; int n = m; System.out.println("m = " + m + ", n = " + n); n = 20 ; System.out.println("m = " + m + ", n = " + n); System.out.println("***********引用数据类型:********" ); Order o1 = new Order (); o1.orderId = 1001 ; Order o2 = o1; System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId); o2.orderId = 1002 ; System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId); } }class Order { int orderId; }
4.8.1、针对基本数据类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class ValueTransferTest1 { public static void main (String[] args) { int m = 10 ; int n = 20 ; System.out.println("m = " + m + ", n = " + n); ValueTransferTest1 test = new ValueTransferTest1 (); test.swap(m, n); System.out.println("m = " + m + ", n = " + n); } public void swap (int m,int n) { int temp = m; m = n; n = temp; } }
4.8.2、针对引用数据类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public class ValueTransferTest2 { public static void main (String[] args) { Data data = new Data (); data.m = 10 ; data.n = 20 ; System.out.println("m = " + data.m + ", n = " + data.n); ValueTransferTest2 test = new ValueTransferTest2 (); test.swap(data); System.out.println("m = " + data.m + ", n = " + data.n); } public void swap (Data data) { int temp = data.m; data.m = data.n; data.n = temp; } }class Data { int m; int n; }
4.8.3、练习1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class TransferTest3 { public static void main (String args[]) { TransferTest3 test=new TransferTest3 (); test.first(); } public void first () { int i=5 ; Value v=new Value (); v.i=25 ; second(v,i); System.out.println(v.i); } public void second (Value v,int i) { i=0 ; v.i=20 ; Value val=new Value (); v=val; System.out.println(v.i+" " +i); } }class Value { int i= 15 ; }
4.8.4、练习2
1 2 3 4 5 6 7 public static void method (int a,int b) { a = a * 10 ; b = b * 20 ; System.out.println(a); System.out.println(b); System.exit(0 ); }
4.8.5、练习3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 for (int i= 0 ;i < arr.length;i++){ arr[i] = arr[i] / arr[0 ]; }for (int i = arr.length –1 ;i >= 0 ;i--){ arr[i] = arr[i] / arr[0 ]; }int temp = arr[0 ];for (int i= 0 ;i < arr.length;i++){ arr[i] = arr[i] / temp; }
4.8.6、练习4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class ArrayPrint { public static void main (String[] args) { int [] arr = new int []{1 ,2 ,3 }; System.out.println(arr); char [] arr1 = new char []{'a' ,'b' ,'c' }; System.out.println(arr1); } }
4.8.7、练习5:将对象作为参数传递给方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Circle { double radius; public double findArea () { return radius * radius * Math.PI; } }
PassObject类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class PassObject { public static void main (String[] args) { PassObject test = new PassObject (); Circle c = new Circle (); test.printAreas(c, 5 ); System.out.println("no radius is:" + c.radius); } public void printAreas (Circle c,int time) { System.out.println("Radius\t\tAreas" ); for (int i = 1 ;i <= time ;i++){ c.radius = i; System.out.println(c.radius + "\t\t" + c.findArea()); } c.radius = time + 1 ; } }
4.9、递归(recursion)方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 public class RecursionTest { public static void main (String[] args) { int sum = 0 ; for (int i = 1 ; i <= 100 ; i++) { sum += i; } System.out.println("sum = " + sum); RecursionTest test = new RecursionTest (); int sum1 = test.getSum(100 ); System.out.println("sum1 = " + sum1); } public int getSum (int n) { if (n == 1 ) { return 1 ; } else { return n + getSum(n - 1 ); } } public int getSum1 (int n) { if (n == 1 ) { return 1 ; } else { return n * getSum1(n - 1 ); } } }
1、练习1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class RecursionTest { public static void main (String[] args) { int value = test.f(10 ); System.out.println(value); } public int f (int n) { if (n == 0 ){ return 1 ; }else if (n == 1 ){ return 4 ; }else { return 2 *f(n-1 ) + f(n-2 ); } } public int f1 (int n) { if (n == 20 ){ return 1 ; }else if (n == 21 ){ return 4 ; }else { return 2 *f1(n-1 ) + f1(n-2 ); } } }
2、练习2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Recursion2 { public static void main (String[] args) { Recursion2 test = new Recursion2 (); int value = test.f(10 ); System.out.println(value); } public int f (int n) { if (n == 1 || n == 2 ) { return 1 ; } else { return f(n - 1 ) + f(n - 2 ); } } }
05、面向对象特征之一:封装与隐藏 1、封装性的引入与体现
为什么需要封装?封装的作用和含义? 我要用洗衣机,只需要按一下开关和洗涤模式就可以了。有必要了解洗衣机内部的结构吗?有必要碰电动机吗? 我要开车,…
2、我们程序设计追求“高内聚,低耦合”。
高内聚:类的内部数据操作细节自己完成,不允许外部干涉; 低耦合:仅对外暴露少量的方法用于使用。
3、隐藏对象内部的复杂性,只对外公开简单的接口。
便于外界调用,从而提高系统的可扩展性、可维护性。通俗的说,把该隐藏的隐藏起来,该暴露的暴露出来。这就是封装性的设计思想。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 public class AnimalTest { public static void main (String[] args) { Animal a = new Animal (); a.name = "大黄" ; a.show(); a.setLegs(-6 ); a.show(); System.out.println(a.name); System.out.println(a.getLegs()); } }class Animal { String name; private int age; private int legs; public void setLegs (int l) { if (l >= 0 && l % 2 == 0 ){ legs = l; }else { legs = 0 ; } } public int getLegs () { return legs; } public void eat () { System.out.println("动物进食" ); } public void show () { System.out.println("name = " + name + ",age = " + age + ",legs = " + legs); } public int getAge () { return age; } public void setAge (int a) { age = a; } }
5.1、四种权限修饰符的理解与测试 Java 权限修饰符public、protected、default(缺省)、private
置于类的成员定义前,用来限定对象对该类成员的访问权限。
图片: https://uploader.shimo.im/f/BnOxeu6anBqhLyCW.png 对于 class 的权限修饰只可以用 public 和 default(缺省)。
public 类可以在任意地方被访问。 default 类只可以被同一个包内部的类访问。 1、Order 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Order { private int orderPrivate; int orderDefault; public int orderPublic; private void methodPrivate () { orderPrivate = 1 ; orderDefault = 2 ; orderPublic = 3 ; } void methodDefault () { orderPrivate = 1 ; orderDefault = 2 ; orderPublic = 3 ; } public void methodPublic () { orderPrivate = 1 ; orderDefault = 2 ; orderPublic = 3 ; } }
2、OrderTest 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class OrderTest { public static void main (String[] args) { Order order = new Order (); order.orderDefault = 1 ; order.orderPublic = 2 ; order.methodDefault(); order.methodPublic(); } }
相同项目不同包的 OrderTest 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import github.Order;public class OrderTest { public static void main (String[] args) { Order order = new Order (); order.orderPublic = 2 ; order.methodPublic(); } }
5.2、封装性的练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class Person { private int age; public void setAge (int a) { if (a < 0 || a > 130 ){ System.out.println("传入的数据据非法" ); return ; } age = a; } public int getAge () { return age; } public int doAge (int a) { age = a; return age; } }
3、测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); p1.setAge(12 ); System.out.println("年龄为:" + p1.getAge()); } }
06、 构造器(构造方法) 6.1、构造器的理解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 public class PersonTest { public static void main (String[] args) { Person p = new Person (); p.eat(); Person p1 = new Person ("Tom" ); System.out.println(p1.name); } }class Person { String name; int age; public Person () { System.out.println("Person()......" ); } public Person (String n) { name = n; } public Person (String n,int a) { name = n; age = a; } public void eat () { System.out.println("人吃饭" ); } public void study () { System.out.println("人学习" ); } }
1、练习 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Person { private int age; public Person () { age = 18 ; } }public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); System.out.println("年龄为:" + p1.getAge()); } }
2、练习 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public class Person { private int age; private String name; public Person () { age = 18 ; } public Person (String n,int a) { name = n; age = a; } public void setName (String n) { name = n; } public String getName () { return name; } public void setAge (int a) { if (a < 0 || a > 130 ){ System.out.println("传入的数据据非法" ); return ; } age = a; } public int getAge () { return age; } }public class PersonTest { public static void main (String[] args) { Person p2 = new Person ("Tom" ,21 ); System.out.println("name = " + p2.getName() + ",age = " + p2.getAge()); } }
3、练习 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public class TriAngle { private double base; private double height; public TriAngle () { } public TriAngle (double b,double h) { base = b; height = h; } public void setBase (double b) { base = b; } public double getBase () { return base; } public void setHeight (double h) { height = h; } public double getHeight () { return height; } }public class TriAngleTest { public static void main (String[] args) { TriAngle t1 = new TriAngle (); t1.setBase(2.0 ); t1.setHeight(2.5 ); System.out.println("base : " + t1.getBase() + ",height : " + t1.getHeight()); TriAngle t2 = new TriAngle (5.1 ,5.6 ); System.out.println("面积 : " + t2.getBase() * t2.getHeight() / 2 ); } }
6.2、总结属性赋值的过程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class UserTest { public static void main (String[] args) { User u = new User (); System.out.println(u.age); User u1 = new User (2 ); u1.setAge(3 ); System.out.println(u1.age); } }class User { String name; int age = 1 ; public User () { } public User (int a) { age = a; } public void setAge (int a) { age = a; } }
6.3、JavaBean 的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Customer { private int id; private String name; public Customer () { } public void setId (int i) { id = i; } public int getId () { return id; } public void setName (String n) { name = n; } public String getName () { return name; } }
6.4、UML 类图
表示 public 类型,-表示 private 类型,#表示 protected 类型 方法的写法: 方法的类型(+、-) 方法名(参数名:参数类型):返回值类型 07、关键字:this 的使用 7.1、this 调用属性、方法、构造器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 public class PersonTest { public static void main (String[] args) { Person p1 = new Person (); p1.setAge(1 ); System.out.println(p1.getAge()); p1.eat(); System.out.println(); Person p2 = new Person ("jerry" ,20 ); System.out.println(p2.getAge()); } }class Person { private String name; private int age; public Person () { this .eat(); String info = "Person 初始化时,需要考虑如下的 1,2,3,4...(共 40 行代码)" ; System.out.println(info); } public Person (String name) { this (); this .name = name; } public Person (int age) { this (); this .age = age; } public Person (String name,int age) { this (age); this .name = name; } public void setNmea (String name) { this .name = name; } public String getName () { return this .name; } public void setAge (int age) { this .age = age; } public int getAge () { return this .age; } public void eat () { System.out.println("人吃饭" ); this .study(); } public void study () { System.out.println("学习" ); } }
7.2、this 的练习
1、Boy 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class Boy { private String name; private int age; public void setName (String name) { this .name = name; } public String getName () { return name; } public void setAge (int ahe) { this .age = age; } public int getAge () { return age; } public Boy (String name, int age) { this .name = name; this .age = age; } public void marry (Girl girl) { System.out.println("我想娶" + girl.getName()); } public void shout () { if (this .age >= 22 ){ System.out.println("可以考虑结婚" ); }else { System.out.println("好好学习" ); } } }
2、Girl 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class Girl { private String name; private int age; public String getName () { return name; } public void setName (String name) { this .name = name; } public Girl () { } public Girl (String name, int age) { this .name = name; this .age = age; } public void marry (Boy boy) { System.out.println("我想嫁给" + boy.getName()); } public int compare (Girl girl) { return this .age - girl.age; } }
3、测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class BoyGirlTest { public static void main (String[] args) { Boy boy = new Boy ("罗密欧" ,21 ); boy.shout(); Girl girl = new Girl ("朱丽叶" , 18 ); girl.marry(boy); Girl girl1 = new Girl ("祝英台" , 19 ); int compare = girl.compare(girl1); if (compare > 0 ){ System.out.println(girl.getName() + "大" ); }else if (compare < 0 ){ System.out.println(girl1.getName() + "大" ); }else { System.out.println("一样的" ); } } }
2、练习2
Account 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 public class Account { private int id; private double balance; private double annualInterestRate; public void setId (int id) { } public double getBalance () { return balance; } public void setBalance (double balance) { this .balance = balance; } public double getAnnualInterestRate () { return annualInterestRate; } public void setAnnualInterestRate (double annualInterestRate) { this .annualInterestRate = annualInterestRate; } public int getId () { return id; } public void withdraw (double amount) { if (balance < amount){ System.out.println("余额不足,取款失败" ); return ; } balance -= amount; System.out.println("成功取出" + amount); } public void deposit (double amount) { if (amount > 0 ){ balance += amount; System.out.println("成功存入" + amount); } } public Account (int id, double balance, double annualInterestRate) { this .id = id; this .balance = balance; this .annualInterestRate = annualInterestRate; } }
Customer 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class Customer { private String firstName; private String lastName; private Account account; public Customer (String f, String l) { this .firstName = f; this .lastName = l; } public String getFirstName () { return firstName; } public String getLastName () { return lastName; } public Account getAccount () { return account; } public void setAccount (Account account) { this .account = account; } }
CustomerTest 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class CustomerTest { public static void main (String[] args) { Customer cust = new Customer ("Jane" , "Smith" ); Account acct = new Account (1000 ,2000 ,0.0123 ); cust.setAccount(acct); cust.getAccount().deposit(100 ); cust.getAccount().withdraw(960 ); cust.getAccount().withdraw(2000 ); System.out.println("Customer[" + cust.getLastName() + cust.getFirstName() + "] has a account: id is " + cust.getAccount().getId() + ",annualInterestRate is " + cust.getAccount().getAnnualInterestRate() * 100 + "%, balance is " + cust.getAccount().getBalance()); } }
3、练习3
Account 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class Account { private double balance; public double getBalance () { return balance; } public Account (double init_balance) { this .balance = init_balance; } public void deposit (double amt) { if (amt > 0 ){ balance += amt; System.out.println("存钱成功" ); } } public void withdraw (double amt) { if (balance >= amt){ balance -= amt; System.out.println("取钱成功" ); }else { System.out.println("余额不足" ); } } }
Customer 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Customer { private String firstName; private String lastName; private Account account; public String getFirstName () { return firstName; } public String getLastName () { return lastName; } public Account getAccount () { return account; } public void setAccount (Account account) { this .account = account; } public Customer (String f, String l) { this .firstName = f; this .lastName = l; } }
Bank 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class Bank { private int numberOfCustomers; private Customer[] customers; public Bank () { customers = new Customer [10 ]; } public void addCustomer (String f,String l) { Customer cust = new Customer (f,l); customers[numberOfCustomers++] = cust; } public int getNumberOfCustomers () { return numberOfCustomers; } public Customer getCustomers (int index) { if (index >= 0 && index < numberOfCustomers){ return customers[index]; } return null ; } }
BankTest 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class BankTest { public static void main (String[] args) { Bank bank = new Bank (); bank.addCustomer("Jane" , "Smith" ); bank.getCustomers(0 ).setAccount(new Account (2000 )); bank.getCustomers(0 ).getAccount().withdraw(500 ); double balance = bank.getCustomers(0 ).getAccount().getBalance(); System.out.println("客户: " + bank.getCustomers(0 ).getFirstName() + "的账户余额为:" + balance); System.out.println("***************************" ); bank.addCustomer("万里" , "杨" ); System.out.println("银行客户的个数为: " + bank.getNumberOfCustomers()); } }
8、 关键字:package、import 的使用 8.1、关键字—package 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class PackageImportTest { }
JDK 中主要的包介绍
1 2 3 4 5 6 7 1. java.lang----包含一些 Java 语言的核心类,如 String、Math、Integer、System 和 Thread,提供常用功能2. java.net----包含执行与网络相关的操作的类和接口。3. java.io----包含能提供多种输入/输出功能的类。4. java.util----包含一些实用工具类,如定义系统特性、接口的集合框架类、使用与日期日历相关的函数。5. java.text----包含了一些 java 格式化相关的类6. java.sql----包含了 java 进行 JDBC 数据库编程的相关类/接口7. java.awt----包含了构成抽象窗口工具集(abstractwindowtoolkits)的多个类,这些类被用来构建和管理应用程序的图形用户界面(GUI)。B/S C/S
8.2、MVC 设计模式 MVC 是常用的设计模式之一,将整个程序分为三个层次:视图模型层,控制器层,数据模型层 。这种将程序输入输出、数据处理,以及数据的展示分离开来的设计模式使程序结构变的灵活而且清晰,同时也描述了程序各个对象间的通信方式,降低了程序的耦合性。
8.3、关键字—import 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 import java.util.*;import account2.Bank;public class PackageImportTest { public static void main (String[] args) { String info = Arrays.toString(new int []{1 ,2 ,3 }); Bank bank = new Bank (); ArrayList list = new ArrayList (); HashMap map = new HashMap (); Scanner s = null ; System.out.println("hello" ); UserTest us = new UserTest (); } }