三、数组,Debug
4、数组的内存图
(3)数组使用的常见问题
5、Debug工具
四、方法
方法的概论
1、方法的定义、调用
(1)方法完整的定义形式、调用
(2)方法的其他定义形式、调用
2、方法使用的常见问题
3、方法案例
(1)定义方法的技巧,计算1–n的和并返回
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
System.out.println("1到"+n+"的和为"+sum(n));
}
public static int sum(int a){
int sum=0;
for (int i = 1; i <=a ; i++) {
sum+=i;
}
return sum;
}
}
(2)判断奇偶数
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
check(n);
}
public static void check (int n){
if(n%2==0){
System.out.println(n+"是偶数");
}
else{
System.out.println(n+"是奇数");
}
}
(3) 数组求最值
public static void main(String[] args) {
int []n={23,13,33,43,53,63,73};
System.out.println(max(n));
}
public static int max(int[] n) {
int max =n[0];
for (int i = 0; i < n.length; i++) {
if (n[i] > max) {
max = n[i];
}
}
return max;
}
4、方法调用的内存图
5、方法的参数传递机制
(1)基本类型的参数传递
(2)引用类型的参数传递
6、方法的参数传递案例
(1)打印整形数组内容
public static void main(String[] args) {
// 需求:定义方法,可以打印任意整型数组的内容:[12, 32, 23]
// 4、定义数组,再调用方法
int[] arr = {12, 32, 23};
printArray(arr);
System.out.println("-----------------");
int[] arr2 = {};
printArray(arr2);
System.out.println("-----------------");
int[] arr3 = null;
printArray(arr3);
}
/**
1、定义一个方法:参数:整型数组类型的变量 返回值类型申明:void
*/
public static void printArray(int[] arr){
if(arr != null){
// 2、把数组内容打印出来。
System.out.print("[");
// 3、开始遍历数组中的每个数据
for (int i = 0; i < arr.length; i++) {
// 如果发现是最后一个元素不加逗号
// if(i == arr.length - 1){
// System.out.print(arr[i]);
// }else {
// System.out.print(arr[i] + ", ");
// }
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
}
System.out.println("]");
}else {
System.out.println("当前数组对象不存在,其地址是:null");
}
}
(2)从数组中查询元素的索引并返回
public static void main(String[] args) {
// 需求:从整型数组中查询某个数据的索引返回,不存在该数据返回-1
// 3、定义数组,调用方法
int[] arr = {11, 22, 33, 66, 87, 19};
int index = searchIndex(arr, 17);
System.out.println("您查询的数据的索引是:" + index);
}
/**
1、定义一个方法:参数接收数组,要查询的数据,返回值:整型
*/
public static int searchIndex(int[] arr, int data){
// 2、开始找出这个数据的索引
for (int i = 0; i < arr.length; i++) {
if(arr[i] == data){
return i;
}
}
return -1; // 查无此元素!
}
(3)两个数组内容是否相等
public static boolean compare(int[] arr1,int[] arr2) {
if(arr1.length==arr2.length){
for (int i = 0; i < arr1.length ; i++) {
if(arr1[i]!=arr2[i])
return false;
}
return true;
}
else {
return false;
}
}
7、方法重载
(1)方法重载的形式和作用
另外一种简化形式
public static void main(String[] args) {
fire();
fire("岛国");
fire("岛国", 1000);
}
public static void fire(){
fire("米国");
}
public static void fire(String location){
fire(location, 1);
}
public static void fire(String location, int number){
System.out.println("默认发射"+number+"枚武器给"+location+"~~~");
}
(2)方法重载的识别技巧
8、单独使用return关键字
五、编程训练
1、买飞机票
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您计划出行的月份");
int season = sc.nextInt();
System.out.println("请选择您计划乘坐的舱位类型及价格");
String airclass = sc.next();
double money = sc.nextInt();
if (season >=5 && season <=10) {
switch (airclass) {
case "头等舱":
money = money * 0.9;
break;
case "经济舱":
money = money * 0.85;
break;
}
} else {
switch (airclass) {
case "头等舱":
money = money * 0.7;
break;
case "经济舱":
money = money * 0.65;
break;
}
}
System.out.println("您应支付"+money);
public static void main(String[] args) {
// 3、录入购买信息,调用方法得到最终结果
Scanner sc = new Scanner(System.in);
System.out.println("机票原价:");
double price = sc.nextDouble();
System.out.println("月份:");
int month = sc.nextInt();
System.out.println("仓位类型(头等舱、经济舱):");
String type = sc.next();
double rs = calc(price, month, type);
System.out.println("您当前购买机票的价格是:" + rs);
}
/**
1、定义一个方法:形参(原价、月份、头等舱经济舱) 返回值类型申明:double
*/
public static double calc(double money, int month, String type){
// 2、判断月份是淡季还是旺季
if(month >= 5 && month <= 10){
// 旺季
switch (type){
case "经济舱":
money *= 0.85;
break;
case "头等舱":
money *= 0.9;
break;
default:
System.out.println("您输入的仓位不正确~~");
money = -1; // 当前无法计算价格了!
}
}else if(month == 11 || month == 12 || month >= 1 && month <= 4){
switch (type){
case "经济舱":
money *= 0.65;
break;
case "头等舱":
money *= 0.7;
break;
default:
System.out.println("您输入的仓位不正确~~");
money = -1; // 当前无法计算价格了!
}
}else {
System.out.println("月份有问题");
money = -1;
}
return money;
}
2、找素数
public static void main(String[] args) {
// 1、定义一个循环,找到101-200之间的全部数据
for (int i = 101; i <= 200 ; i++) {
// i = 101 102 103 ... 199 200
// 信号位:标记
boolean flag = true; // 一开始认为当前数据是素数。
// 2、判断当前遍历的这个数据是否是素数
for (int j = 2; j < i / 2; j++) {
if(i % j == 0){
flag = false; // 假设失败了,i不是素数
break; // 没有必要继续判定下去了!
}
}
// 3、根据判定的结果选择是否输出这个数据,是素数则输出
if(flag){
System.out.print(i + "\t");
}
}
3、开发验证码
public static void main(String[] args) {
System.out.println("请问您想生成几位验证码");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String code=number(n);
System.out.println("您获取的验证码为"+code);
}
public static String number(int n){
Random r=new Random();
String code="";
for (int i = 0; i <n ; i++) {
int a=r.nextInt(3);
switch(a){
case 0:
char ch= (char) (r.nextInt(26)+65);
code+=ch;
break;
case 1:
char ch1= (char) (r.nextInt(26)+97);
code+=ch1;
break;
case 2:
code+=r.nextInt(10);
}
}
return code;
}
4、数组元素的复制
public static void main(String[] args) {
int[] arr1 = {11, 22, 33, 44};
// int[] arr2 = arr1; // 没有完成了数组复制。
int[] arr2 = new int[arr1.length];
copy(arr1 , arr2);
printArray(arr1);
printArray(arr2);
}
public static void printArray(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
}
System.out.println("]");
}
public static void copy(int[] arr1, int[] arr2){
// 正式完成元素的复制
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
public static void main(String[] args) {
int[] arr = new int[]{11, 22, 33, 44, 55};
int[] arr1= new int[arr.length];
copy(arr,arr1);
}
public static void copy(int[] arr1, int[] arr2) {
System.out.print("{");
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
System.out.print(i == arr2.length - 1 ? arr2[i] : arr2[i] + ",");
}
System.out.print("}");
}
5、评委打分
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int []score=new int[10];
for (int i = 1; i <= score.length ; i++) {
System.out.println("请输入第"+i+"位评委的打分");
score[i]= sc.nextInt();
}
}
public static double calculate(int []n) {
int max=n[0];
int min=n[0];
int sum=0;
for (int i = 0; i < n.length ; i++) {
if(n[i]>max){
max=n[i];
}
if(n[i]<min){
min=n[i];
}
sum+=n[i];
}
double ave=(sum-max-min)*0.1/n.length;
return ave;
}
6、数字加密
public static void main(String[] args) {
// 1、定义一个数组存储需要加密的数据
int[] arr = new int[]{1, 9, 8, 3};
// 2、遍历数组中的每个数据,按照规则进行修改
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5) % 10;
}
// 3、把数组中的元素进行反转操作。
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
// 交换 i 和 j位置处的值,即可反转
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
// 4、遍历数组中的每个元素输出即可
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
7、双色球
public static int[] createLuckNumber(){
// a、定义一个动态初始化的数组,存储7个数字
int[] numbers = new int[7]; // [12, 23, 0, 0, 0, 0, | 0]
// i
// b、遍历数组,为每个位置生成对应的号码。(注意:遍历前6个位置,生成6个不重复的红球号码,范围是1-33)
Random r = new Random();
for (int i = 0; i < numbers.length - 1; i++) {
// 为当前位置找出一个不重复的1-33之间的数字
while (true) {
int data = r.nextInt(33) + 1; // 1-33 ====> (0-32) + 1
// c、注意:必须判断当前随机的这个号码之前是否出现过,出现过要重新随机一个,直到不重复为止,才可以存入数组中去。
// 定义一个flag变量,默认认为data是没有重复的
boolean flag = true;
for (int j = 0; j < i; j++) {
if(numbers[j] == data) {
// data当前这个数据之前出现过,不能用
flag = false;
break;
}
}
if(flag) {
// data这个数据之前没有出现过,可以使用了
numbers[i] = data;
break;
}
}
}
// d、为第7个位置生成一个1-16的号码作为蓝球号码
numbers[numbers.length - 1] = r.nextInt(16) + 1;
return numbers;
}
六、面向对象基础
概述
1、设计对象并使用
(1)设计类,创建对象并使用
(2)定义类的几个注意事项
2、对象在内存图中的运行机制
(1)多个对象的内存图
(2)两个变量指向同一个变量内存图
JAVA存在自动垃圾回收器,会定期进行清理。
3、模拟购物车模块
(1)需求分析,架构搭建
public static void main(String[] args) {
// 1、定义商品类,用于后期创建商品对象。
// 2、定义购物车对象,使用一个数组对象表示
Goods []shopcar=new Goods[100];
// 3、搭建操作架构
while(true){
System.out.println("请您选择如下命令进行操作:");
System.out.println("查询商品:query");
System.out.println("添加商品:add");
System.out.println("修改数量:update");
System.out.println("结算:pay");
Scanner sc=new Scanner(System.in);
String order= sc.next();
switch (order) {
case "query":
querygoods(shopcar);
break;
case "add":
addgoods(shopcar,sc);
break;
case "update":
updategoods(shopcar);
break;
case "pay":
paygoods(shopcar);
break;
default:
System.out.println("您输入的指令有误");
break;
}
}
}
(2)添加商品到购物车,查看商品信息
private static void addgoods(Goods[] shopcar ,Scanner sc) {
// 1、录入用户输入的商品信息
System.out.println("请输入您想添加的商品名称");
String name= sc.next();
System.out.println("请输入您想添加的商品编号");
String id=sc.next();
System.out.println("请输入您想添加的商品数量");
int buynumber= sc.nextInt();
System.out.println("请输入您想添加的商品价格");
double price= sc.nextDouble();
// 2、把这个购买商品的信息封装成一个商品对象
Goods goods=new Goods();
goods.buynumber=buynumber;
goods.id=id;
goods.name=name;
goods.price=price;
// 3、把这个商品对象添加到购物车数组中去
for (int i = 0; i < shopcar.length ; i++) {
if(shopcar[i]==null){
shopcar[i]=goods;
break;
}
}
}
private static void querygoods(Goods[] shopcar) {
System.out.println("商品信息如下");
for (int i = 0; i < shopcar.length ; i++) {
Goods g=shopcar[i];
if(shopcar[i]!=null){
System.out.println(g.id+"\t"+g.name+"\t"+g.price+"\t"+g.buynumber);
}
else{
break;
}
}
(3)修改购买数量
private static void updategoods(Goods[] shopcar,Scanner sc) {
while(true) {
System.out.println("请您输入商品的id");
String id = sc.next();
Goods g = getnumber(shopcar, id);
if(g==null){
System.out.println("对不起,没有该商品");
}
else {
System.out.println("请您输入修改后的商品数量");
g.buynumber= sc.nextInt();
System.out.println("修改完成,"+g.name+"的数量更改为"+g.buynumber);
break;
}
}
}
private static Goods getnumber(Goods[] shopcar, String id) {
for (int i = 0; i < shopcar.length ; i++) {
Goods g=shopcar[i];
if(g!=null){
if((g.id).equals(id)) {
return g;
}
}
else{
return null;
}
}
return null;
}
(4)结算金额
private static void paygoods(Goods[] shopcar) {
double money=0;
for (int i = 0; i < shopcar.length ; i++) {
Goods g = shopcar[i];
if(shopcar[i]!=null) {
money += (g.price * g.buynumber);
}
else{
break;
}
}
System.out.println("商品总金额为"+money);
}
4、构造器
学习构造器的目的:
- 真正知道对象具体是通过调用什么代码得到的
- 能够掌握为对象赋值的简便写法
- 为以后学习面向对象编程做支撑
public class Car {
String name;
int speed;
public Car (){
System.out.println("无参数构造器");
}
public Car(String a,int b){
System.out.println("有参数构造器");
name=a;
speed=b;
}
}
public static void main(String[] args) {
Car c = new Car();
c.name = "宝马";
c.speed = 40;
System.out.println(c.name);
System.out.println(c.speed);
Car c1 = new Car("宝马", 60);
System.out.println(c1.name);
System.out.println(c1.speed);
}
5、this关键字
没有给对象中的name赋值
6、封装
(1)封装思想概述
(2)如何更好的封装(private)
public class People {
private int age;
public void setAge(int age) {
if(age>0&&age<100){
this.age=age;
}
else{
System.out.println("您输入的年龄有问题");
}
}
public int getAge() {
return age;
}
}
public static void main(String[] args) {
People a=new People();
a.setAge(23);
System.out.println(a.getAge());
}
7、标准JavaBean
8、成员变量和局部变量的区别
9、案例
七、String
1、String概述
2、String类创建对象的两种方式
// 方式一:直接使用双引号得到字符串对象
String name = "我爱你中国";
System.out.println(name);
// 方式二:
// 1、public String(): 创建一个空白字符串对象,不含有任何内容 (几乎不用)
String s1 = new String(); // s1 = ""
System.out.println(s1);
// 2、public String(String): 根据传入的字符串内容,来创建字符串对象(几乎不用)
String s2 = new String("我是中国人");
System.out.println(s2);
// 3、public String(char[] c): 根据字符数组的内容,来创建字符串对象
char[] chars = {'a' , 'b' , '中', '国'};
String s3 = new String(chars);
System.out.println(s3);
// 4、public String(byte[] b): 根据字节数组的内容,来创建字符串对象
byte[] bytes = {97, 98, 99, 65, 66, 67};
String s4 = new String(bytes);
System.out.println(s4);
3、String类常用API
(1)字符串内容比较
System.out.println(sysCode.equals(code1)); // false
System.out.println(sysCode.equalsIgnoreCase(code1)); // true
(2)遍历、替换、截取、分割操作
// 1、public int length(): 获取字符串的长度
String name = "我爱你中国love";
System.out.println(name.length());
// 2、public char charAt(int index): 获取某个索引位置处的字符
char c = name.charAt(1);
System.out.println(name.charAt(1));
System.out.println("------------遍历字符串中的每个字符--------------");
for (int i = 0; i < name.length(); i++) {
char ch = name.charAt(i);
System.out.println(name.charAt(i));
}
System.out.println("----------------------------------------------");
// 3、public char[] toCharArray():: 把字符串转换成字符数组
char[] chars = name.toCharArray();
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
System.out.println(ch);
}
// 4、public String substring(int beginIndex, int endIndex) :截取内容,(包前不包后的)
String name2 = "Java是最厉害的编程语言!";
// 01234567 89
String rs = name2.substring(0, 9);
System.out.println(rs);
String rs1 = name2.substring(4, 9);
System.out.println(rs1);
// 5、public String substring(int beginIndex):从当前索引一直截取到末尾
String rs2 = name2.substring(4);
System.out.println(rs2);
// 6、public String replace(CharSequence target, CharSequence replacement)
String name3 = "金三胖是最厉害的80后,金三胖棒棒的!我好爱金三胖";
String rs3 = name3.replace("金三胖", "***");
System.out.println(rs3);
// 7、public boolean contains(CharSequence s)
System.out.println(name3.contains("金三胖")); // true
System.out.println(name3.contains("金二胖")); // false
// 8、public boolean startsWith(String prefix)
System.out.println(name3.startsWith("金三胖"));
System.out.println(name3.startsWith("金三胖是最厉害的"));
System.out.println(name3.startsWith("金三胖是最厉害的2"));
// 9、public String[] split(String s): 按照某个内容把字符串分割成字符串数组返回。
String name4 = "王宝强,贾乃亮,陈羽凡";
String[] names = name4.split(",");
for (int i = 0; i < names.length; i++) {
System.out.println("选择了:" + names[i]);
}
4、String案例
(1)开发验证码
public static void main(String[] args) {
String data="abcdefghijklmnopqrstuvwxyz123456789";
String code=new String();
Random r=new Random();
for (int i = 0; i < 5; i++) {
int index=r.nextInt(data.length());
code+=data.charAt(index);
}
System.out.println(code);
}
(2)模拟用户登录
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String id0="sushiisagreatpoet";
String past0="thatsforsure";
while (true) {
System.out.println("请您输入您的账号");
String id=sc.next();
if(id.equals(id0)){
break;
}
else{
System.out.println("您输入的账号有误");
}
}
for (int i = 1; i <=5 ; i++) {
System.out.println("请您输入您的密码");
String past=sc.next();
if(past.equals(past0)){
System.out.println("登录成功");
break;
}
else{
if(i<5){
System.out.println("您输入的密码有误,您还可以输入"+(5-i)+"次");}
else System.out.println("密码错误次数过多,您的账号已锁定");
}
}
}
(3)手机号码屏蔽
public static void main(String[] args) {
// 1、键盘录入一个手机号码
Scanner sc = new Scanner(System.in);
System.out.println("请您输入您的手机号码:");
String tel = sc.next();
// 2、截取号码的前三位,后四位 18665666520
String before = tel.substring(0, 3); // 0 1 2
String after = tel.substring(7); // 从索引7开始截取到手机号码的末尾
String s = before + "****" + after;
System.out.println(s);
}
八、ArrayList
集合概述
1、ArrayList集合入门
// 1、创建ArrayList集合的对象
ArrayList list = new ArrayList();
// 2、添加数据
list.add("Java");
// 3、给指定索引位置插入元素
list.add(1, "苏轼");
2、泛型
ArrayList<String> list = new ArrayList<>(); // JDK 1.7开始,泛型后面的类型申明可以不写
list.add("Java");
list.add("MySQL");
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(23);
list2.add(100);
ArrayList<Object>
3、ArrayList常用API,遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Java");
list.add("MySQL");
list.add("MyBatis");
list.add("HTML");
// 1、public E get(int index):获取某个索引位置处的元素值
String e = list.get(3);
System.out.println(e);
// 2、public int size():获取集合的大小(元素个数)
System.out.println(list.size());
// 3、完成集合的遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 4、public E remove(int index):删除某个索引位置处的元素值,并返回被删除的元素值
System.out.println(list); // [Java, Java, MySQL, MyBatis, HTML]
String e2 = list.remove(2);
System.out.println(e2);
System.out.println(list);
// 5、public boolean remove(Object o):直接删除元素值,删除成功返回true,删除失败返回false
System.out.println(list.remove("MyBatis"));
System.out.println(list);
ArrayList<String> list1 = new ArrayList<>();
list1.add("Java");
list1.add("王宝强");
list1.add("Java");
list1.add("MySQL");
System.out.println(list1);
// 只会删除第一次出现的这个元素值,后面的不删除
System.out.println(list1.remove("Java"));
System.out.println(list1);
// 6、public E set(int index,E element):修改某个索引位置处的元素值。
String e3 = list1.set(0 , "贾乃亮");
System.out.println(e3);
System.out.println(list1);
4、集合案例
(1)遍历并删除元素值
方法一
for (int i = scores.size() - 1; i >= 0 ; i--) {
int score = scores.get(i);
// 2、判断这个分数是否低于80分,如果低于则从集合中删除它
if(score < 80){
scores.remove(i);
}
}
方法二
for (int i = 0; i < scores.size(); i++) {
int score = scores.get(i);
// 2、判断这个分数是否低于80分,如果低于则从集合中删除它
if(score < 80){
scores.remove(i);
i--;
}
}
System.out.println(scores);
(2) 存储自定义类型的变量
影片信息在程序中的表示
public static void main(String[] args) {
// 1、定义一个电影类
// 2、创建对象
Movies a=new Movies("肖申克的救赎",9.7,"zhangsan");
Movies b=new Movies("霸王别姬",9.7,"李四");
Movies c=new Movies("阿甘正传",9.5,"lisi");
// 3、创建一个电影类型的集合,储存三部电影信息
ArrayList<Movies> movie=new ArrayList<>();
movie.add(a);
movie.add(b);
movie.add(c);
// 4、遍历电影类型的集合中的每一个电影对象,访问它的信息
for (int i = 0; i < movie.size() ; i++) {
Movies m=movie.get(i);
System.out.println(m.getName());
System.out.println(m.getScore());
System.out.println(m.getActor());
System.out.println("------------------");
}
}
(3)元素搜索
九、 static
1、static的作用,修饰成员变量的用法
public static int onLineNumber = 161;
// 实例成员变量
private String name;
private int age;
public static void main(String[] args) {
// 1、类名.静态成员变量
User.onLineNumber++;
// 注意:同一个类中访问静态成员变量,类名可以省略不写
System.out.println(onLineNumber);
// 2、对象.实例成员变量
// System.out.println(name);
User u1 = new User();
u1.name = "猪八戒";
u1.age = 36;
System.out.println(u1.name);
System.out.println(u1.age);
// 对象.静态成员变量(不推荐这样访问)
u1.onLineNumber++;
User u2 = new User();
u2.name = "孙悟空";
u2.age = 38;
System.out.println(u2.name);
System.out.println(u2.age);
// 对象.静态成员变量(不推荐这样访问)
u2.onLineNumber++;
System.out.println(onLineNumber);
2、static修饰成员变量的内存原理
3、static修饰成员方法的基本用法
4、static修饰成员方法的内存原理
5、static的注意事项
6、static的应用知识
(1)工具类
(2)代码块
静态代码块的案例:
斗地主
public class unt {
// 1、准备一个容器,存储54张牌对象,这个容器建议使用静态的集合。静态的集合只加载一次。
public static ArrayList<String> poke=new ArrayList();
// 2、在游戏启动之前需要准备好54张牌放进去,使用静态代码块进行初始化
// 3、加载54张牌进去。
// 4、准备4种花色:类型确定,个数确定了
static{
String []color= {"♠", "♥", "♣", "♦"};
// 5、定义点数
String []number={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
// 6、先遍历点数、再组合花色
for (int i = 0; i < number.length; i++) {
for (int j = 0; j < color.length; j++) {
poke.add(number[i] + color[j]);
}
}
poke.add("小🃏");
poke.add("大🃏");
}
public static void main(String[] args) {
System.out.println("新牌:" +poke);
}
}
单例设计模式:
设计模式
饿汉单例设计模式
public class SingleInstance {
private SingleInstance(){
}
public static SingleInstance a=new SingleInstance();
}
--------------
public static void main(String[] args) {
SingleInstance s1=SingleInstance.a;
SingleInstance S2=SingleInstance.a;
System.out.println(s1==S2);
}
懒汉单例模式
public class SingleInstance2 {
/**
2、定义一个静态的成员变量用于存储一个对象,一开始不要初始化对象,因为人家是懒汉
*/
private static SingleInstance2 instance;
/**
1、私有构造器啊
*/
private SingleInstance2(){
}
/**
3、提供一个方法暴露,真正调用这个方法的时候才创建一个单例对象
*/
public static SingleInstance2 getInstance(){
if(instance == null){
// 第一次来拿对象,为他做一个对象
instance = new SingleInstance2();
}
return instance;
}
}
----------------
public static void main(String[] args) {
// 得到一个对象
SingleInstance2 s1 = SingleInstance2.getInstance();
SingleInstance2 s2 = SingleInstance2.getInstance();
System.out.println(s1 == s2);
}
十、继承
1、继承概述
2、继承的设计规范
public class People {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
-----------------
public class Poet extends People{
public void write(){
System.out.println(getName()+"在写诗");
}
}
-----------------
public class Poet extends People{
public void write(){
System.out.println(getName()+"在写诗");
}
}
3、继承的特点
JAVA只支持单继承,不支持多继承
4、继承后:成员变量,成员方法的访问特点
public static void main(String[] args) {
Wolf w = new Wolf();
System.out.println(w.name); // 子类的
w.showName();
}
}
class Animal{
public String name = "父类动物";
}
class Wolf extends Animal{
public String name = "子类动物";
public void showName(){
String name = "局部名称";
System.out.println(name); // 局部的
System.out.println(this.name); // 子类name
System.out.println(super.name); // 父类name
}
5、方法重写
public static void main(String[] args) {
SmartPhone huawei=new SmartPhone();
huawei.name="小明";
huawei.time=18;
huawei.mail();
huawei.call();
}
public class Phone {
String name;
int time;
public void mail(){
System.out.println("给"+name+"发送短信");
}
public void call(){
System.out.println("在"+time+"时打电话");
}
}
public class SmartPhone extends Phone {
@Override
public void mail(){
super.mail();
System.out.println("给"+name+"发送视频");
}
@Override
public void call(){
super.call();
System.out.println("进行视频通话");
}
}
6、继承后,子类构造器的特点
7、继承后子类构造器访问父类有参构造器
public static void main(String[] args) {
SmartPhone huawei=new SmartPhone("小明",18);
huawei.mail();
huawei.call();
}
public class Phone {
String name;
int time;
public Phone(String name, int time) {
this.name = name;
this.time = time;
}
public void mail(){
System.out.println("给"+name+"发送短信");
}
public void call(){
System.out.println("在"+time+"时打电话");
}
}
public class SmartPhone extends Phone {
public SmartPhone(String name, int time) {
super(name, time);
}
}
8、this、super使用总结