目录
- 面向对象之三大特性之三:多态
- 多态的概述、多态的形式
- 多态的好处
- 多态下引用数据类型的类型转换
- 多态的综合案例
- 内部类
- 内部类概述
- 内部类之一:静态内部类
- 内部类之二:成员内部类
- 内部类之三:局部内部类
- 内部类之四:匿名内部类概述[重点]
- 匿名内部类常见使用形式
- 匿名内部类真实使用场景演示
- 常用API
- Object
- toString方法
- equals方法
- Objects
- StringBuilder
- Math
- System
- BigDecimal
面向对象之三大特性之三:多态
多态的概述、多态的形式
多态的好处
多态下引用数据类型的类型转换
多态的综合案例
package com.zixin.d4_polymorphic_test;
public interface USB {
void connect();
void unconnect();
}
package com.zixin.d4_polymorphic_test;
public class KeyBoard implements USB{
public String name;
public KeyBoard(String name) {
this.name = name;
}
@Override
public void connect() {
System.out.println(name + "成功连接了电脑~");
}
@Override
public void unconnect() {
System.out.println(name + "成功从电脑中拔出了~");
}
public void keyDown(){
System.out.println(name+"敲击了:来了,老弟,666,没毛病");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zixin.d4_polymorphic_test;
public class Mouse implements USB{
private String name;
public Mouse(String name) {
this.name = name;
}
@Override
public void connect() {
System.out.println(name + "成功连接了电脑~");
}
@Override
public void unconnect() {
System.out.println(name + "成功从电脑中拔出了~");
}
public void dbClick(){
System.out.println(name+"双击点亮小红心,一键三连~~");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zixin.d4_polymorphic_test;
public class Computer {
private String name;
public Computer(String name) {
this.name = name;
}
public void start(){
System.out.println(name + "开机了~~");
}
public void installUSB(USB usb){
usb.connect();
if(usb instanceof KeyBoard){
KeyBoard k = (KeyBoard) usb;
k.keyDown();
}else if(usb instanceof Mouse){
Mouse m = (Mouse) usb;
m.dbClick();
}
usb.unconnect();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zixin.d4_polymorphic_test;
public class Test {
public static void main(String[] args) {
Computer c =new Computer("外星人");
c.start();
USB u =new KeyBoard("燕双飞");
c.installUSB(u);
USB u1 = new Mouse("罗技鼠标");
c.installUSB(u1);
}
}
内部类
内部类概述
内部类之一:静态内部类
package com.zixin.d5_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 void show(){
}
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;
}
}
}
package com.zixin.d5_innerclass_static;
public class Test {
public static void main(String[] args) {
Outer.Inner in = new Outer.Inner();
in.setName("张三");
in.show();
}
}
内部类之二:成员内部类
内部类之三:局部内部类
内部类之四:匿名内部类概述[重点]
匿名内部类常见使用形式
package com.zixin.d8_innerclass_anonymous;
public class Test {
public static void main(String[] args) {
Animal a =new Animal() {
@Override
public void run() {
System.out.println("老虎跑得快~~~");
}
};
a.run();
}
}
//class Tiger extends Animal{
// @Override
// public void run() {
// System.out.println("老虎跑得快~~");
// }
//}
abstract class Animal{
public abstract void run();
}
package com.zixin.d8_innerclass_anonymous;
public class Test2 {
public static void main(String[] args) {
Swimming s =new Swimming() {
@Override
public void swim() {
System.out.println("学生游泳很开心");
}
};
go(s);
go(new Swimming() {
@Override
public void swim() {
System.out.println("老师游泳很快");
}
});
}
public static void go(Swimming s){
System.out.println("开始。。。");
s.swim();
System.out.println("结束。。。");
}
}
interface Swimming{
void swim();
}
匿名内部类真实使用场景演示
常用API
Object
toString方法
package com.zixin.d9_api_object;
public class Student { // extends Object
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.zixin.d9_api_object;
public class Test {
public static void main(String[] args) {
Student s = new Student("周雄",'男',19);
System.out.println(s.toString());
System.out.println(s);
}
}
equals方法
package com.zixin.d9_api_object;
import java.util.Objects;
public class Student { // extends Object
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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return sex == student.sex && age == student.age && Objects.equals(name, student.name);
}
// @Override
// public boolean equals(Object o){
// if(o instanceof Student){
// Student s2 = (Student) o;
//
// return this.name.equals(s2.name) && this.age == s2.age && this.sex==s2.sex;
// }else{
// return false;
// }
// }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
'}';
}
}
package com.zixin.d9_api_object;
public class Test2 {
public static void main(String[] args) {
Student s = new Student("周雄",'男',19);
Student s2 = new Student("周雄",'男',19);
System.out.println(s.equals(s2));
System.out.println(s==s2);
}
}
Objects
StringBuilder
package com.zixin.d10_api_objects;
public class StringBuilderDemo1 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("a");
sb.append(false);
sb.append(3.3);
System.out.println(sb);
StringBuilder sb1 = new StringBuilder();
sb1.append("a").append("b").append(2);
System.out.println(sb1);
sb1.reverse().append(110);
System.out.println(sb1);
System.out.println(sb1.length());
String rs = sb1.toString();
}
}
package com.zixin.d10_api_objects;
public class StringBuilderTest2 {
public static void main(String[] args) {
int [] arr1 =null;
System.out.println(toString(arr1));
int[] arr2={1,2,3,5};
System.out.println(toString(arr2));
}
public static String toString(int[] arr){
if(arr !=null){
StringBuilder sb = new StringBuilder("[");
for(int i =0;i<arr.length;i++){
sb.append(arr[i]).append(i== arr.length-1?"":",");
}
sb.append("]");
return sb.toString();
}
else{
return null;
}
}
}
Math
System
package com.zixin.d13_system;
import java.sql.Array;
import java.sql.SQLOutput;
import java.util.Arrays;
public class SystemDemo {
public static void main(String[] args) {
System.out.println("程序开始。。。");
// System.exit(0);
long time = System.currentTimeMillis();
System.out.println(time);
long startTime = System.currentTimeMillis();
for(int i=0;i<100000;i++){
System.out.println("输出:"+i);
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime/1000.0+"s");
int []arr1 = new int[]{10,20,30,40,50,60,70};
int [] arr2 = new int[6];
System.arraycopy(arr1,3,arr2,2,3);
System.out.println(Arrays.toString(arr2));
System.out.println("程序结束");
}
}
BigDecimal
package com.zixin.d14_bigdecimal;
import java.math.BigDecimal;
public class BigDecimalDemo {
public static void main(String[] args) {
System.out.println(0.09+0.01);
double a = 0.1;
double b = 0.2;
double c = a + b;
System.out.println(c);
System.out.println("- ------------");
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
BigDecimal c1 = a1.subtract(b1);
System.out.println(c1);
double rs = c1.doubleValue();
System.out.println(rs);
System.out.println("--------");
BigDecimal a11 = BigDecimal.valueOf(10.0);
BigDecimal b11 = BigDecimal.valueOf(3.0);
BigDecimal c11 = a11.divide(b11,2,BigDecimal.ROUND_HALF_UP);
System.out.println(c11);
}
}