快捷键 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 a.sout —> System.out.print(a); sout —> System.out.print(); ctrl+alt+t 选择循环 alt+Enter 强制类型转换 右键generate,自动生成构造函数和get、set方法 a.fori+Enter 自动生成for 循环 ctrl + alt + V 自动生成左边的接受变量 Alt+Shift+Enter 实现方法重写(抽象类尤甚)
static 定义 静态,可以修饰成员变量、成员方法。
成员变量
成员变量的执行原理
1 2 3 4 5 6 7 8 package com.itheima.staticdemo;public class Student { static String name; int 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 package com.itheima.staticdemo;public class Test { public static void main (String[] args) { Student.name = "崔巉" ; Student s1 = new Student (); s1.name = "左右" ; Student s2 = new Student (); s2.name = "齐静春" ; System.out.println(Student.name); System.out.println(s1.name); System.out.println(s2.name); s1.age = 18 ; s2.age = 22 ; System.out.println(s1.age); System.out.println(s2.age); } }
类变量的应用场景
1 2 3 4 5 6 7 8 9 10 11 12 package com.itheima.staticdemo;public class User { public static int number; public User () { number++; } }
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.staticdemo;public class Test { public static void main (String[] args) { User u1 = new User (); User u2 = new User (); User u3 = new User (); User u4 = new User (); System.out.println(User.number); } }
成员方法
成员方法的执行原理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.itheima.staticmethod;public class Student { double score; public static void printHelloWorld () { System.out.println("Hello World!" ); System.out.println("Hello World!" ); } public void PrintPass () { System.out.println("成绩:" + (score >= 60 ? "及格" : "不及格" )); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.itheima.staticmethod;public class Test { public static void main (String[] args) { Student.printHelloWorld(); Student s1 = new Student (); s1.printHelloWorld(); s1.PrintPass(); } }
类方法的常见应用场景
注: 代码不作演示,关键在于工具类要用类方法 ,以便给开发人员共同使用 。
好处:
不需要再 new 一个对象,可以节省内存
直接用 类名.方法名 来调用,代码更简洁
提高代码复用性,提升了开发效率
在工具类中,可以将构造器私有化,防止用对象调用,多占内存。步骤:private XxxxUtil(){}
注意事项:
类方法中可以直接访问类的成员,不可以直接访问实例成员(也就是非 static 的) ★★★★★
若需要在类方法中访问实例成员,一种常见的做法是将实例作为参数传递给类方法。
实例方法中既可以直接访问类成员,也可以直接访问实例成员
实例方法中可以出现 this 关键字,类方法中不可以出现 this 关键字
main 方法
在 Java 中,main 方法 也是一个类方法 ,java 虚拟机(即 JVM)是用 类名.main(…) 执行的,main 方法是程序的入口方法 ,它有固定的格式 ,所以String[] args 不可以省去 。
String[] args 这个字符串数组是保存 运行 main 函数时输入的参数 的,例如 main 函数所在的类名为 Test 那么你在 cmd 运行 java Test a b c 时,args[0] = a , args[1]=b, args[2]=c 你就可以在你的程序中调用你输入的这些变量了。
args 的全称是 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 package com.itheima.block;public class Student { static int number = 80 ; static String schoolName; static { System.out.println("静态代码块执行了~~" ); schoolName = "黑马" ; } { System.out.println("实例代码块执行了~~" ); System.out.println("有人实例对象了" + this ); } public Student () { System.out.println("无参构造器执行了~~" ); } public Student (String name) { System.out.println("有参构造器执行了~~" ); } }
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 package com.itheima.block;public class Test { public static void main (String[] args) { System.out.println(Student.number); System.out.println(Student.number); System.out.println(Student.number); System.out.println(Student.schoolName); System.out.println("--------------------" ); Student s1 = new Student (); Student s2 = new Student ("张三" ); } }
继承 定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.itheima.extend;public class A { public int i; public void print1 () { System.out.println("===print1===" ); } private int j; private void print2 () { System.out.println("===print2===" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.itheima.extend;public class B extends A { public int k; private int u; public void print3 () { System.out.println(i); print1(); } }
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.extend;public class Test { public static void main (String[] args) { B b = new B (); System.out.println(b.i); System.out.println(b.k); } }
好处:
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.extend2;public class People { private String name; public String getName () { return name; } public void setName (String name) { this .name = name; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.itheima.extend2;public class Teacher extends People { private String skill; public String getSkill () { return skill; } public void setSkill (String skill) { this .skill = skill; } public void printInfo () { System.out.println(this .getName() + "具备的技能:" + this .getSkill()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.itheima.extend2;public class Test { public static void main (String[] args) { Teacher t = new Teacher (); t.setName("张三" ); t.setSkill("跆拳道" ); System.out.println(t.getName()); System.out.println(t.getSkill()); t.printInfo(); } }
好处: 减少了重复代码的编写,增加了代码的复用性。
权限修饰符
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 package com.itheima.modifier;public class Father { private void privateMethod () { System.out.println("=== Private ===" ); } void Method () { System.out.println("=== 缺省(package-private) ===" ); } protected void protectedMethod () { System.out.println("=== Protected ===" ); } public void publicMethod () { System.out.println("=== Public ===" ); } public void test () { privateMethod(); Method(); protectedMethod(); publicMethod(); } }
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.modifier;public class Demo { public static void main (String[] args) { Father f = new Father (); f.Method(); f.protectedMethod(); f.publicMethod(); } }
1 2 3 4 5 6 7 8 9 10 11 12 package com.itheima.other_modifier;import com.itheima.modifier.Father;public class Son extends Father { public void test () { protectedMethod(); publicMethod(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.itheima.other_modifier;import com.itheima.modifier.Father;public class Demo { public static void main (String[] args) { Father f = new Father (); f.publicMethod(); Son s=new Son (); s.publicMethod(); } }
单继承
object object 类是 java 所有类的祖宗类。我们写的任何一个类,其实都是 object 的子类或子孙类。
override
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.override;public class A { public void print1 () { System.out.println("A1" ); } public void print2 (int a) { System.out.println("A2" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.itheima.override;public class B extends A { @Override public void print1 () { System.out.println("B1" ); } @Override public void print2 (int a) { System.out.println("B2" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.override;public class Test { public static void main (String[] args) { B b = new B (); b.print1(); b.print2(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 package com.itheima.override;public class Student { private String name; private int age; public Student () { } public Student (String name, int age) { this .name = name; this .age = age; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } @Override public String toString () { return "Student{" + "name='" + name + '\'' + ", age=" + 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 package com.itheima.override;import java.util.ArrayList;public class Demo { public static void main (String[] args) { Student s = new Student ("左右" , 120 ); System.out.println(s.toString()); System.out.println(s); ArrayList list = new ArrayList (); list.add("java" ); list.add("python" ); System.out.println(list); } }
子类访问其他成员 1、在子类方法中访问其他成员(成员变量、成员方法),是依照就近原则 的。
先子类局部范围找。
接着子类成员范围找。
然后父类成员范围找,如果父类范围还没有找到则报错。
2、如果子父类中,出现了重名的成员会优先使用子类的,如果此时一定要在子类中使用父类的怎么办?
可以通过super 关键字 ,指定访问父类的成员:super.父类成员变量/父类成员方法
1 2 3 4 5 6 7 8 9 package com.itheima.visit;public class Father { String name = "父类名称" ; public void print1 () { System.out.println("===父类的print1执行===" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.itheima.visit;public class Son extends Father { String name = "子类名称" ; public void printName () { System.out.println(name); String name = "局部名称" ; System.out.println(name); System.out.println(this .name); System.out.println(super .name); } @Override public void print1 () { System.out.println("===子类的print1执行===" ); } public void Method () { print1(); super .print1(); } }
1 2 3 4 5 6 7 8 9 package com.itheima.visit;public class Test { public static void main (String[] args) { Son s = new Son (); s.printName(); s.Method(); } }
子类构造器
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 package com.itheima.constructor;class Father { public Father () { System.out.println("===父类的 无参构造器 执行了===" ); } public Father (String name) { System.out.println("===父类的 有参构造器 执行了===" ); } } class Son extends Father { public Son () { System.out.println("===子类的 无参构造器 执行了===" ); } public Son (String name) { System.out.println("===子类的 有参构造器 执行了===" ); } } public class Test { public static void main (String[] args) { Son s = new Son (); System.out.println("============================" ); Son s1 = new Son ("白景" ); } }
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 package com.itheima.constructor;class Father { public Father (String name) { System.out.println("===父类的 有参构造器 执行了===" ); } } class Son extends Father { public Son () { super ( ); System.out.println("===子类的 无参构造器 执行了===" ); } public Son (String name) { super (name); System.out.println("===子类的 有参构造器 执行了===" ); } } public class Test { public static void main (String[] args) { Son s = new Son (); System.out.println("============================" ); Son s1 = new Son ("白景" ); } }
子类构造器调用父类构造器的常见应用场景
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 package com.itheima.constructor;public class People { private String name; private int age; public People () { } public People (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; } }
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 package com.itheima.constructor;public class Teacher extends People { private String skill; public Teacher () { } public Teacher (String name, int age, String skill) { super (name, age); this .skill = skill; } public Teacher (String skill) { this .skill = skill; } public String getSkill () { return skill; } public void setSkill (String skill) { this .skill = skill; } }
1 2 3 4 5 6 7 8 9 10 package com.itheima.constructor;public class Demo { public static void main (String[] args) { Teacher t = new Teacher ("李宝瓶" , 18 , "写论文" ); System.out.println(t.getName()); System.out.println(t.getAge()); System.out.println(t.getSkill()); } }
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 package com.itheima.constructor;public class Test1 { public static void main (String[] args) { Student s = new Student ("李二" , 99 ); System.out.println(s.getName()); System.out.println(s.getAge()); System.out.println(s.getScore()); } } class Student { private String name; private int age; private int score; public Student () { } public Student (String name, int age) { this (name, age, 60 ); } public Student (String name, int age, int score) { this .name = name; this .age = age; this .score = score; } 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 int getScore () { return score; } public void setScore (int score) { this .score = score; } }
多态 定义
1 2 3 4 5 6 7 8 9 package com.itheima.polymorphism;public class People { public String name = "父类的成员变量" ; public void run () { System.out.println("人可以跑~" ); } }
1 2 3 4 5 6 7 8 9 10 package com.itheima.polymorphism;public class Teacher extends People { public String name = "老师的成员变量" ; @Override public void run () { System.out.println("老师跑得很慢~" ); } }
1 2 3 4 5 6 7 8 9 10 package com.itheima.polymorphism;public class Student extends People { public String name = "学生的成员变量" ; @Override public void run () { System.out.println("学生跑得贼快~" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 package com.itheima.polymorphism;public class Test { public static void main (String[] args) { People p1 = new Teacher (); p1.run(); System.out.println(p1.name); People p2 = new Student (); p2.run(); System.out.println(p2.name); } }
好处
1 2 3 4 5 6 7 8 9 package com.itheima.polymorphism;public class People { public String name = "父类的成员变量." ; public void run () { System.out.println("人可以跑~" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.itheima.polymorphism;public class Teacher extends People { public String name = "老师的成员变量." ; @Override public void run () { System.out.println("老师跑得很慢~" ); } public void teach () { System.out.println("老师会教知识~" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.itheima.polymorphism;public class Student extends People { public String name = "学生的成员变量." ; @Override public void run () { System.out.println("学生跑得贼快~" ); } public void study () { System.out.println("学生要学习~" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.itheima.polymorphism;public class Test { public static void main (String[] args) { People p1 = new Teacher (); p1.run(); Student s = new Student (); go(s); Teacher t = new Teacher (); go(t); } public static void go (People p) { } }
类型转换
注: People类、Teacher类、Student类
同上,不再赘述。
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 package com.itheima.polymorphism;public class Test { public static void main (String[] args) { People p1 = new Teacher (); p1.run(); Teacher t1 = (Teacher) p1; t1.teach(); if (p1 instanceof Teacher) { Teacher t2 = (Teacher) p1; t2.teach(); } else { Student s1 = (Student) p1; s1.study(); } Student s = new Student (); go(s); } public static void go (People p) { if (p instanceof Teacher) { Teacher t = (Teacher) p; t.teach(); } else { Student s = (Student) p; s.study(); } } }
final
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 package com.itheima.final1;final class A {}; class C { public final void print () { System.out.println("C" ); } } class D extends C { } public class Test { public static final String ROLE_NAME = "陈平安" ; public static void main (String[] args) { final double pi = 3.14 ; final int [] arr = {11 , 22 , 33 }; arr[1 ] = 99 ; } public static void buy (final double money) { } }
回顾一下 C++中 const 的知识点。
1 2 3 4 5 6 7 8 9 const int *p1 = &a;p1 = &b; int *const p2 = &a;*p2 = 100 ;
常量
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.final1;public class Test1 { public static final String SCHOOL_NAME = "北京大学" ; public static void main (String[] args) { System.out.println(SCHOOL_NAME); System.out.println(SCHOOL_NAME); System.out.println(SCHOOL_NAME); System.out.println(SCHOOL_NAME); System.out.println(SCHOOL_NAME); } }
编译后的.class 文件内容。
抽象 定义
注意事项及特点
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 package com.itheima.abstract1;public abstract class A { private String name; private static String schoolName; public abstract void A () ; public A () { } public A (String name) { this .name = name; } public String getName () { return name; } public void setName (String name) { this .name = name; } public static String getSchoolName () { return schoolName; } public static void setSchoolName (String schoolName) { A.schoolName = schoolName; } }
1 2 3 4 5 6 7 8 9 package com.itheima.abstract1;public class B extends A { @Override public void A () { } }
1 2 3 4 5 6 7 8 package com.itheima.abstract1;public class Test { public static void main (String[] args) { } }
应用场景和好处
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.itheima.abstract2;public abstract class Animal { private String name; public abstract void shout () ; public String getName () { return name; } public void setName (String name) { this .name = name; } }
1 2 3 4 5 6 7 package com.itheima.abstract2;public class Cat extends Animal { public void shout () { System.out.println(getName() + "会喵喵喵地叫~" ); } }
1 2 3 4 5 6 7 package com.itheima.abstract2;public class Dog extends Animal { public void shout () { System.out.println(getName() + "会汪汪汪地叫~~" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 package com.itheima.abstract2;public class Test { public static void main (String[] args) { Animal cat = new Cat (); cat.setName("眯眯" ); cat.shout(); Animal dog = new Dog (); dog.setName("墩墩" ); dog.shout(); } }
注:不用抽象类也能实现这些功能,只是这样更专业,能更好地支持多态。
接口 定义
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.interface1;public interface A { String SCHOOL_NAME = "黑马程序员" ; void test () ; }
1 2 3 4 5 6 7 8 9 10 package com.itheima.interface1;public class Test { public static void main (String[] args) { } }
1 2 3 4 5 6 7 package com.itheima.interface1;public interface B { void testb1 () ; void testb2 () ; }
1 2 3 4 5 6 7 package com.itheima.interface1;public interface C { void testc1 () ; void testc2 () ; }
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 package com.itheima.interface1;public class D implements B , C { @Override public void testb1 () { } @Override public void testb2 () { } @Override public void testc1 () { } @Override public void testc2 () { } }
1 2 3 4 5 6 7 8 package com.itheima.interface1;public class Test { public static void main (String[] args) { D d = new D (); d.testb1(); } }
优点
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 package com.itheima.interface1;public class Test1 { public static void main (String[] args) { Singer s = new Student (); s.sing(); System.out.println("---------" ); Dancer d = new Student (); d.dance(); } } class Student implements Singer , Dancer { @Override public void sing () { System.out.println("学习唱歌~" ); } @Override public void dance () { System.out.println("学习跳舞~" ); } } interface Singer { void sing () ; } interface Dancer { void dance () ; }
案例练习
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 package com.itheima.interface_demo;public class Student { private String name; private String gender; private double score; public Student () { } public Student (String name, String gender, double score) { this .name = name; this .gender = gender; this .score = score; } public String getName () { return name; } public void setName (String name) { this .name = name; } public String getGender () { return gender; } public void setGender (String gender) { this .gender = gender; } public double getScore () { return score; } public void setScore (double score) { this .score = score; } }
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 package com.itheima.interface_demo;import java.util.ArrayList;public class ClassManager { private ArrayList<Student> students = new ArrayList <>(); private StudentOperator studentOperator = new StudentOperatorImpl2 (); public ClassManager () { students.add(new Student ("陈清都" , "男" , 100 )); students.add(new Student ("阿良" , "男" , 92 )); students.add(new Student ("董三更" , "男" , 97 )); students.add(new Student ("陈曦" , "男" , 89 )); students.add(new Student ("左右" , "男" , 93 )); students.add(new Student ("陆芝" , "女" , 88 )); } public void printInfo () { studentOperator.printAllInfo(students); } public void printAvgScores () { studentOperator.printAvgScores(students); } }
1 2 3 4 5 6 7 8 9 package com.itheima.interface_demo;import java.util.ArrayList;public interface StudentOperator { void printAllInfo (ArrayList<Student> students) ; void printAvgScores (ArrayList<Student> students) ; }
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 package com.itheima.interface_demo;import java.util.ArrayList;public class StudentOperatorImpl1 implements StudentOperator { @Override public void printAllInfo (ArrayList<Student> students) { System.out.println("-------全班全部学生信息-------" ); for (int i = 0 ; i < students.size(); i++) { Student s = students.get(i); System.out.println("姓名:" + s.getName() + "\t性别:" + s.getGender() + "\t成绩:" + s.getScore()); } } @Override public void printAvgScores (ArrayList<Student> students) { System.out.println("-------全班学生平均成绩-------" ); double AllScore = 0 ; for (int i = 0 ; i < students.size(); i++) { Student s = students.get(i); AllScore = AllScore + s.getScore(); } System.out.println("平均成绩:" + AllScore / students.size()); } }
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 package com.itheima.interface_demo;import java.util.ArrayList;public class StudentOperatorImpl2 implements StudentOperator { @Override public void printAllInfo (ArrayList<Student> students) { System.out.println("-------全班男女学生信息-------" ); int SumMale = 0 ; for (int i = 0 ; i < students.size(); i++) { Student s = students.get(i); System.out.println("姓名:" + s.getName() + "\t性别:" + s.getGender() + "\t成绩:" + s.getScore()); if (s.getGender().equals("男" )) { SumMale++; } } System.out.println("男生人数:" + SumMale + "\t女生人数:" + (students.size() - SumMale)); } @Override public void printAvgScores (ArrayList<Student> students) { System.out.println("-------全班截尾平均成绩-------" ); double [] avgScores = new double [students.size()]; for (int i = 0 ; i < students.size(); i++) { Student s = students.get(i); avgScores[i] = s.getScore(); } for (int i = 1 ; i < avgScores.length; i++) { boolean flag = false ; for (int j = i; j > 0 ; j--) { if (avgScores[j] < avgScores[j - 1 ]) { double tmp = avgScores[j]; avgScores[j] = avgScores[j - 1 ]; avgScores[j - 1 ] = tmp; flag = true ; } } if (!flag) { break ; } } double AllScore = 0 ; for (int i = 1 ; i < avgScores.length - 1 ; i++) { AllScore = AllScore + avgScores[i]; } System.out.println("\n最高分:" + avgScores[avgScores.length - 1 ]); System.out.println("最低分:" + avgScores[0 ]); System.out.println("平均成绩:" + AllScore / (avgScores.length - 2 )); } }
1 2 3 4 5 6 7 8 9 package com.itheima.interface_demo;public class Test { public static void main (String[] args) { ClassManager classManager = new ClassManager (); classManager.printInfo(); classManager.printAvgScores(); } }
JDK8 开始新增的方法 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 package com.itheima.interface_jdk8;public interface A { default void test1 () { System.out.println("===默认方法===" ); test2(); } private void test2 () { System.out.println("===私有方法===" ); } static void test3 () { System.out.println("===静态方法===" ); } }
1 2 3 4 package com.itheima.interface_jdk8;public class B implements A {}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.itheima.interface_jdk8;public class Test { public static void main (String[] args) { B b = new B (); b.test1(); A.test3(); } }
接口的多继承 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 package com.itheima.interface_attention;public class Test { public static void main (String[] args) { } } interface A { void test1 () ; }; interface B { void test2 () ; }; interface C { void test3 () ; }; interface D extends A , B, C {}; class E implements D { @Override public void test1 () { } @Override public void test2 () { } @Override public void test3 () { } }
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 package com.itheima.interface_attention;public class Test1 { public static void main (String[] args) { Zi z = new Zi (); z.run(); M m = new M (); m.run(); } } interface I { void test1 () ; }; interface J { String test1 () ; }; class Fu { public void run () { System.out.println("我是亲爹~" ); } } interface K { default void run () { System.out.println("我是干爹~" ); } } class Zi extends Fu implements K {} interface X { default void run () { System.out.println("我是X~" ); } } interface Y { default void run () { System.out.println("我是Y~" ); } } class M implements X , Y { @Override public void run () { X.super .run(); } }
内部类
成员内部类
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 package com.itheima.inner_class;public class Outer { private double score = 99 ; public static String subject; public void demo () { System.out.println(score); System.out.println(subject); } public class Inner { private String name; private double grade = 88 ; public static String schoolName; public void test () { System.out.println(grade); System.out.println(subject); double grade = 77 ; System.out.println(grade); System.out.println(this .grade); System.out.println(Outer.this .score); } public String getName () { return name; } public void setName (String name) { this .name = name; } } }
1 2 3 4 5 6 7 8 9 package com.itheima.inner_class;public class Test { public static void main (String[] args) { Outer.Inner inner = new Outer ().new Inner (); inner.setName("李宝瓶" ); inner.test(); } }
静态内部类
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 package com.itheima.inner_class2;public class Outer { private double score = 99 ; public static String subject; public static void demo () { System.out.println(subject); } public static class Inner { private String name; public static String schoolName; public String getName () { return name; } public void setName (String name) { this .name = name; } public static void test () { System.out.println(subject); } } }
1 2 3 4 5 6 7 8 package com.itheima.inner_class2;public class Test { public static void main (String[] args) { Outer.Inner inner = new Outer .Inner(); inner.test(); } }
局部内部类 比较鸡肋,不作要求,无需了解。
匿名内部类(重点)
基础写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.itheima.inner_class3;public class Test { public static void main (String[] args) { Animal a = new Cat (); a.shout(); } } class Cat extends Animal { @Override public void shout () { System.out.println("眯眯喵喵叫~" ); } } abstract class Animal { public abstract void shout () ; }
匿名内部类写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.itheima.inner_class3;public class Test { public static void main (String[] args) { Animal a = new Animal () { @Override public void shout () { System.out.println("眯眯喵喵叫~" ); } }; a.shout(); } } abstract class Animal { public abstract void shout () ; }
反编译 Test$1.class 文件
所以这样就可以不用描述是怎样一个子类,Cat 或是 Dog,直接把它的属性给填进去(有实无名)。【体会一下什么叫匿名】
1 2 3 4 5 6 7 8 9 10 package com.itheima.inner_class3;class Test$1 extends Animal { Test$1 () { } public void shout () { System.out.println("眯眯喵喵叫~" ); } }
应用场景
案例 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 package com.itheima.inner_class4;public class Test { public static void main (String[] args) { go(new Swimming () { @Override public void swim () { System.out.println("我要学游泳~" ); } }); } public static void go (Swimming s) { s.swim(); } } interface Swimming { void swim () ; }
案例 2
部分代码涉及后续 GUI 内容,稍作了解即可,核心是了解匿名内部类的使用场景
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 package com.itheima.inner_class5;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Test { public static void main (String[] args) { JFrame win = new JFrame ("登陆界面" ); JPanel panel = new JPanel (); win.add(panel); JButton btn = new JButton ("登录" ); panel.add(btn); btn.addActionListener(new ActionListener () { @Override public void actionPerformed (ActionEvent e) { JOptionPane.showMessageDialog(win, "登录成功~" ); } }); btn.addActionListener(e -> JOptionPane.showMessageDialog(win, "再点一下~" )); win.setSize(500 , 500 ); win.setLocationRelativeTo(null ); win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); win.setVisible(true ); } }
枚举 定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.itheima.enum1;public enum A { X, Y, Z; private String name; A() { } A(String name) { this .name = name; } public String getName () { return name; } public void setName (String name) { this .name = name; } }
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 package com.itheima.enum1;public class Test { public static void main (String[] args) { A a1 = A.X; System.out.println(a1); System.out.println(A.Y); A a2 = A.Z; A[] as = A.values(); System.out.println(as[0 ]); A a3 = A.valueOf("Z" ); System.out.println(a3.name()); System.out.println(a3.ordinal()); A a4 = A.values()[1 ]; System.out.println(a4); } }
javap : Java class 文件分解器,可以反编译,也可以查看 java 编译器生成的字节码。用于分解 class 文件。
抽象枚举 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 package com.itheima.enum1;public enum B { X { @Override public void show () { } }, Y("2024" ) { @Override public void show () { System.out.println("My CardID:" + getCardID()); } }; private String CardID; B() { } B(String cardID) { CardID = cardID; } public String getCardID () { return CardID; } public void setCardID (String cardID) { CardID = cardID; } public abstract void show () ; }
1 2 3 4 5 6 7 8 9 10 package com.itheima.enum1;public class Test1 { public static void main (String[] args) { B b1 = B.Y; b1.show(); } }
应用场景:做信息标志和分类
对比 1:
1 2 3 4 5 6 package com.itheima.enum2;public class Constant { public static final int BOY = 0 ; public static final int GIRL = 1 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.itheima.enum2;public class Test { public static void main (String[] args) { show(1 ); show(Constant.BOY); } public static void show (int gender) { switch (gender) { case Constant.BOY: System.out.println("Boy" ); break ; case Constant.GIRL: System.out.println("Girl" ); break ; } } }
对比 2:
1 2 3 4 5 package com.itheima.enum2;public enum Constant { BOY, GIRL; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.itheima.enum2;public class Test { public static void main (String[] args) { show(Constant.BOY); } public static void show (Constant gender) { switch (gender) { case BOY: System.out.println("Boy" ); break ; case GIRL: System.out.println("Girl" ); break ; } } }
泛型
对比 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.itheima.generics;import java.util.ArrayList;public class Test { public static void main (String[] args) { ArrayList list = new ArrayList (); list.add("a" ); list.add("b" ); list.add(2024 ); list.add(new cat ()); for (int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); String e = (String) list.get(i); System.out.println(e); } } } class cat {}
对比 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 package com.itheima.generics;import java.util.ArrayList;public class Test { public static void main (String[] args) { ArrayList<String> list = new ArrayList <>(); list.add("a" ); list.add("b" ); for (int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); String e = (String) list.get(i); System.out.println(e); } ArrayList<cat> list1 = new ArrayList <>(); list1.add(new cat ()); for (int i = 0 ; i < list1.size(); i++) { System.out.println(list1.get(i)); } } } class cat {}
泛型类
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 package com.itheima.generics_class;public class MyArrayList <E> { private Object[] arr = new Object [10 ]; private int size; public boolean add (E e) { arr[size++] = e; return true ; } public E get (int index) { return (E) arr[index]; } }
1 2 3 4 5 6 7 package com.itheima.generics_class;public class MyClass2 <E, T> { public void put (E e, T t) { } }
1 2 3 4 5 package com.itheima.generics_class;public class MyClass3 <E extends Animal > {}
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 package com.itheima.generics_class;public class Test { public static void main (String[] args) { MyArrayList<String> list = new MyArrayList <>(); list.add("A" ); list.add("B" ); String e = list.get(0 ); System.out.println(e); MyClass2<Cat, String> c2 = new MyClass2 <>(); MyClass3<Animal> c4 = new MyClass3 <>(); MyClass3<Dog> c3 = new MyClass3 <>(); } } class Animal {} class Cat {} class Dog extends Animal {}
泛型接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.itheima.generic_interface;public class Test { public static void main (String[] args) { } } class Teacher {} class Student {}
1 2 3 4 5 6 7 8 9 10 package com.itheima.generic_interface;import java.util.ArrayList;public interface Data <T> { void addData (T data) ; ArrayList<T> getByName (String name) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.itheima.generic_interface;import java.util.ArrayList;public class TeacherData implements Data <Teacher> { @Override public void addData (Teacher data) { } @Override public ArrayList<Teacher> getByName (String name) { return null ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.itheima.generic_interface;import java.util.ArrayList;public class StudentData implements Data <Student> { @Override public void addData (Student data) { } @Override public ArrayList<Student> getByName (String name) { return null ; } }
泛型方法
注:自己定义的类型变量才是泛型方法,用现成的类型变量不算。
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 package com.itheima.generics_method;import java.util.ArrayList;public class Test { public static void main (String[] args) { String rs = test("Java" ); System.out.println(rs); Dog d = test(new Dog ()); System.out.println(d); ArrayList<Car> cars = new ArrayList <>(); cars.add(new BMW ()); cars.add(new Benz ()); drive(cars); ArrayList<BMW> bmws = new ArrayList <>(); bmws.add(new BMW ()); bmws.add(new BMW ()); drive(bmws); ArrayList<Benz> benzs = new ArrayList <>(); benzs.add(new Benz ()); benzs.add(new Benz ()); drive(benzs); System.out.println(benzs.size()); } public static <T> T test (T t) { return t; } public static <T extends Car > void drive (ArrayList<T> cars) { } public static void drive1 (ArrayList<?> cars) { } public static void drive2 (ArrayList<? extends Car> cars) { } public static void drive3 (ArrayList<? super Car> cars) { } } class Dog {} class Car {} class BMW extends Car {} class Benz extends Car {}
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 package com.itheima.gengrics_attention;import java.util.ArrayList;public class Test { public static void main (String[] args) { ArrayList<String> list = new ArrayList <>(); list.add("A" ); list.add("B" ); list.add("C" ); String rs = list.get(0 ); System.out.println(rs); ArrayList<Integer> list1 = new ArrayList <>(); list1.add(1 ); ArrayList<Double> list2 = new ArrayList <>(); list2.add(1.0 ); ArrayList<Float> list3 = new ArrayList <>(); list3.add(1.0f ); } }
反编译后的文件
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 package com.itheima.gengrics_attention;import java.io.PrintStream;import java.util.ArrayList;public class Test { public Test () { } public static void main (String args[]) { ArrayList list = new ArrayList (); list.add("A" ); list.add("B" ); list.add("C" ); String rs = (String)list.get(0 ); System.out.println(rs); ArrayList list1 = new ArrayList (); list1.add(Integer.valueOf(1 )); ArrayList list2 = new ArrayList (); list2.add(Double.valueOf(1.0D )); ArrayList list3 = new ArrayList (); list3.add(Float.valueOf(1.0F )); } }