父类
package com.peihj.polymorphic_advantage;public abstract class Animals { public String name = "父类动物"; public abstract void run();}
子类狗
package com.peihj.polymorphic_advantage;public class dog extends Animals{ public String name = "狗名称"; @Override public void run() { System.out.println("跑到快"); } public void watch(){ System.out.println("再看门"); }}
子类乌龟
package com.peihj.polymorphic_advantage;public class tortoise extends Animals{ public String name = "乌龟"; @Override public void run() { System.out.println("跑的特别慢"); } public void layeggs(){ System.out.println("再下蛋"); }}
测试类
package com.peihj.polymorphic_advantage;public class Test { public static void main(String[] args) { Animals t = new dog(); // 编译看左边,运行看右边。// t.run();// System.out.println(t.name); // 编译看左边,运行也看左边。(多态侧重行为多态) go(t);// dog dog = (dog) t;// 强制类型转化,从父类到子类// dog.watch();// 可以调用独有功能 trans(t); Animals t1 = new tortoise();// t1.run();// System.out.println(t1.name); go(t1);// tortoise tortoise = (tortoise) t1;// tortoise.layeggs(); trans(t1); } public static void go(Animals a){ System.out.println("比赛开始"); a.run(); System.out.println("比赛结束"); } public static void trans(Animals a){ if (a instanceof tortoise){ tortoise t = (tortoise) a; // 从父类类型到子类必须强制类型转换 t.layeggs(); }else if (a instanceof dog){ dog d = (dog) a; d.watch(); } }}
运行结果
USB接口代码
package com.peihj.duotai;public interface USB { // 接入,接出 void connect(); void unconnect();}
鼠标代码
package com.peihj.duotai;public class mouse implements USB{ private String name; public mouse() { } public mouse(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void dbclick(){ System.out.println(name+"按了一下左键"); } @Override public void connect() { System.out.println(name+"成功连接了电脑"); } @Override public void unconnect() { System.out.println(name+"取消连接了电脑"); }}
键盘代码
package com.peihj.duotai;public class Keyboard implements USB { private String name; public Keyboard() { } public Keyboard(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void click(){ System.out.println(name+"敲击了电脑"); } @Override public void connect() { System.out.println(name+"成功连接了电脑。"); } @Override public void unconnect() { System.out.println(name+"从电脑拔出。"); }}
电脑代码
package com.peihj.duotai;public class computer { private String name; public computer() { } public computer(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void start(){ System.out.println(name+"启动了----"); } public void install(USB usb){ // 多态对象:可能是电脑也可以是鼠标 usb.connect(); // 独有功能先判断再调用 if (usb instanceof Keyboard){ ((Keyboard) usb).click(); }else if(usb instanceof mouse){ ((mouse) usb).dbclick(); } usb.unconnect(); }}
测试类
package com.peihj.duotai;public class Test { public static void main(String[] args) { Keyboard k = new Keyboard("戴尔键盘"); mouse m = new mouse("罗技鼠标"); computer c = new computer("外星人电脑"); c.start(); c.install(k); System.out.println("==============================="); c.install(m); }}
结果演示
package com.peihj.innerclass_static;public class Outer { public static class inner{ private String name; private int age; public static String SchoolName; public inner() { } public inner(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static String getSchoolName() { return SchoolName; } public static void setSchoolName(String schoolName) { SchoolName = schoolName; } }}
package com.peihj.innerclass_static;public class TEST { public static void main(String[] args) { Outer.inner in = new Outer.inner(); in.setName("乔巴"); in.setAge(25); System.out.println(in.getAge()); System.out.println(in.getName()); }}
静态内部类小结 成员内部类
package com.peihj.innerclass_static;public class Outer1 { public static int num = 111; private String hobby = "唱歌"; public class inner1{ private String name; private int age; public static int a; // JDK16之后开始支持静态成员了 public void show(){ System.out.println("名称:"+name); System.out.println("年龄:"+age); System.out.println("爱好:"+hobby); System.out.println("数量:"+num); } public inner1() { } public inner1(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static int getA() { return a; } public static void setA(int a) { inner1.a = a; } }}
package com.peihj.innerclass_static;public class TEST { public static void main(String[] args) { Outer1.inner1 IN = new Outer1().new inner1(); IN.show(); }}
成员内部类代码演示2package com.peihj.innerclass_static;public class Outer1 { public static int num = 111; private String hobby; public Outer1(String hobby) { this.hobby = hobby; } public class inner1{ private String name; private int age; public static int a; // JDK16之后开始支持静态成员了 public void show(){ System.out.println("名称:"+name); System.out.println("年龄:"+age); System.out.println("爱好:"+hobby); System.out.println("数量:"+num); } public inner1() { } public inner1(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static int getA() { return a; } public static void setA(int a) { inner1.a = a; } }}
package com.peihj.innerclass_static;public class TEST { public static void main(String[] args) { Outer1.inner1 IN = new Outer1("跳舞").new inner1("小王",25); IN.show(); System.out.println("===================="); }}
成员内部类小结 面试代码package com.peihj.innerclass_static;public class TRY { public static void main(String[] args) { }}class people{ private int heartbeat = 150; public class heart{ private int heartbeat = 110; public void show(){ int heartbeat = 78; System.out.println(heartbeat); // 78 System.out.println(this.heartbeat); // 110 System.out.println(people.this.heartbeat); // 150 } } }
局部内部类 匿名内部类(重点) 匿名内部类演示代码package com.peihj.interclass_anonymous;public class test { public static void main(String[] args) { Animals t = new tiger(); t.run(); // 匿名内部类,简化代码写法 Animals t1 = new Animals() { @Override void run() { System.out.println("跑的贼快"); } }; t1.run(); }}class tiger extends Animals{ @Override void run() { System.out.println("跑的贼快;"); }}abstract class Animals{ abstract void run();}
结果演示
package com.peihj.interclass_anonymous;public class test2 { public static void main(String[] args) { swimming s = new Student(); go(s); swimming s1 = new swimming() { @Override public void swim() { System.out.println("学生再快乐的自由泳"); } }; s1.swim(); } public static void go(swimming s){ System.out.println("开始"); s.swim(); System.out.println("结束"); }}class teacher implements swimming{ @Override public void swim() { System.out.println("老师游泳熟读非常快"); }}class Student implements swimming{ @Override public void swim() { System.out.println("学生游泳速度中等"); }}// 定义一个接口interface swimming{ void swim();}
匿名内部类真实使用场景 常用API 什么是api object类的作用 toString toString代码(快捷键:tos)package com.peihj.API;public class Student { private String name; private char sex; private int age; public Student() { } public Student(String name, char sex, int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", sex=" + sex + ", age=" + age + '}'; }}
测试类
package com.peihj.API;public class test { public static void main(String[] args) { Student student = new Student("乔巴",'男',25); String ts = student.toString(); System.out.println(ts); }}
结果输出
package com.peihj.API;public class Student { private String name; private char sex; private int age; public Student() { } public Student(String name, char sex, int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", sex=" + sex + ", age=" + age + '}'; }}
测试
package com.peihj.API;public class test { public static void main(String[] args) { Student student = new Student("乔巴",'男',25); Student student1 = new Student("乔巴",'男',25); System.out.println((student == student1)); System.out.println(student.equals(student1)); }}
结果演示
equals改写快捷键:equ+回车+回车
package com.peihj.API;public class test { public static void main(String[] args) { Student student = new Student("乔巴",'男',25); Student student1 = new Student("乔巴",'男',25); System.out.println((student == student1)); System.out.println(student.equals(student1)); }}
改写后比较内容
结果演示
package com.peihj.API;import java.util.Objects;public class test2 { public static void main(String[] args) { String s1 = null; String s2 = new String("peihj"); // System.out.println(s1.equals(s2)); // Cannot invoke "String.equals(Object)" because "s1" is null System.out.println(Objects.equals(s1, s2)); // 更安全,结果也是对的 System.out.println("======================="); System.out.println(Objects.isNull(s1)); System.out.println(s1 == null); System.out.println(Objects.isNull(s2)); System.out.println(s2 == null); }}
结果演示
package com.peihj.API;public class StringBuildDemo { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("a"); sb.append("b"); sb.append("c"); sb.append("我爱你中国"); sb.append("false"); System.out.println(sb); System.out.println("======"); // 支持链式编程 sb.append("a").append("b").append("c").append("111111"); System.out.println(sb); // 反转 System.out.println(sb.reverse().append("2222222")); // 长度 System.out.println(sb.length()); }}
必须先转换成字符串
package com.peihj.API;public class StringBuildDemo { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("123").append("456"); String rs = sb.toString(); check(rs); } public static void check(String data){ System.out.println(data); }}
结果演示
package com.peihj.API;public class Test3 { public static void main(String[] args) { int[] arr = {10,20,30,40,50}; int[] arr1 = null; int[] arr2 = {}; String a = ToString(arr); String b =ToString(arr1); String c = ToString(arr2); System.out.println(a); System.out.println(b); System.out.println(c); } public static String ToString(int[] arr){ // 定义StringBuilder接收数组内容 StringBuilder sb = new StringBuilder("["); if (arr != null){ for (int i = 0; i < arr.length; i++) { sb.append(arr[i]).append(i == arr.length-1 ? "":","); } sb.append("]"); String SB = sb.toString(); return SB; }else { return null; } }}
Math库 Math库程序演示package com.peihj.API;public class MathDemo { public static void main(String[] args) { // 取绝对值返回正数 System.out.println(Math.abs(10)); // 10 System.out.println(Math.abs(-10)); // 10 // 向上取整 System.out.println(Math.ceil(4.000000001)); // 5.0 System.out.println(Math.ceil(4.9999999999)); // 5.0 // 向下取整 System.out.println(Math.floor(4.99999999)); // 4.0 // 求指数次方 System.out.println(Math.pow(2,3)); // 8 // 四舍五入 System.out.println(Math.round(4.49999)); // 4 System.out.println(Math.round(4.500001)); // 5 // random 左闭右开 System.out.println(Math.random()); //0.0 --- 1.0 // 扩展:取3--9之间的随机数 int data = (int)(Math.random()*7) + 3; System.out.println(data); }}
System 类概述
package com.peihj.API;public class SystemDemo { public static void main(String[] args) { System.out.println("程序开始"); // System.exit(0); // jvm终止 // 计算机认为时间有起源,返回1970.1-1 00:00:00 到现在的总毫秒值,时间毫秒值 long time = System.currentTimeMillis(); System.out.println(time); long starttime = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { System.out.println("第"+i+"次"); } long endtime= System.currentTimeMillis(); System.out.println(((endtime - starttime) / 1000.0)); System.out.println("程序结束"); }}
BigDecimal
package com.peihj.API;import java.math.BigDecimal;import java.math.RoundingMode;public class BigDecimalDemo { public static void main(String[] args) { System.out.println("======================================="); double a = 0.1; double b = 0.2; double c = a + b; // 0.30000000000000004 System.out.println(c); System.out.println("========================================"); // 包装浮点型数据成为大数据对象 BigDecimal a1= BigDecimal.valueOf(a); BigDecimal b1= BigDecimal.valueOf(b); BigDecimal c1 = a1.add(b1);// BigDecimal c2 = a1.subtract(b1);// BigDecimal c3 = a1.multiply(b1);// BigDecimal c4 = a1.divide(b1); System.out.println(c1); // 0.3 System.out.println("============================================"); // 目的:rs double rs = c1.doublevalue(); System.out.println(rs); System.out.println("============================================"); // 注意:BigDecimal是一定要精度运算的 BigDecimal a11 = BigDecimal.valueOf(10.0); BigDecimal b11 = BigDecimal.valueOf(3.0); // BigDecimal c11 = a11.divide(b11); // Non-terminating decimal expansion; no exact representable decimal result. BigDecimal c11 = a11.divide(b11,2, RoundingMode.HALF_UP); // 保留2位小数 System.out.println(c11); }}
参考https://www.bilibili.com/video/BV1Cv411372m?p=120&t=101.3